您好我使用的是Direct2D,我正在使用Radial Gradient Brush,但我被困在一个地方。
我的径向渐变画笔代码
struct SampleWindow:DesktopWindow
{
//FOr Radial Gradient Brush
ComPtr<ID2D1RadialGradientBrush> radialBrush;
void CrateDeviceResources()
{
D2D1_GRADIENT_STOP stops[] =
{
{0.0f, COLOR_WHITE},
{1.0f, COLOR_BLUE}
};
ComPtr<ID2D1GradientStopCollection> collection;
m_target->CreateGradientStopCollection(stops, _countof(stops),collection.ReleaseAndGetAddressOf());
D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES props = {};
m_target->CreateRadialGradientBrush(props,collection.Get(),radialBrush.ReleaseAndGetAddressOf());
}
void Draw()
{
auto size = m_target->GetSize();
radialBrush -> SetCenter(Point2F(size.width / 2.0f, size.height / 2.0f));
radialBrush -> SetRadiusX(size.width / 2.0f);
radialBrush -> SetRadiusY(size.height / 2.0f);
auto rect = RectF(0.0f, 0.0f, size.width, size.height);
m_target -> FillRectangle(rect,radialBrush.Get());
}
void MouseMoved(int x, int y, WPARAM)
{
auto centere = radialBrush->GetCenter();
radialBrush->SetGradientOriginOffset(Point2F(x - centere.x, y - centere.y));
Invalidate();
}
}
在使用此行时功能鼠标移动
auto centere = radialBrush->GetCenter();
我的节目打破它告诉我
Access violation Exception
DesktopWindow类代码是:
BEGIN_MSG_MAP()
MESSAGE_HANDLER(WM_PAINT, PaintHandler)
MESSAGE_HANDLER(WM_DESTROY, DestroyHandler)
MESSAGE_HANDLER(WM_SIZE, SizeHandler)
MESSAGE_HANDLER(WM_DISPLAYCHANGE, DisplayChangeHandler)
MESSAGE_HANDLER(WM_MOUSEMOVE, MouseMovedHandler)
END_MSG_MAP()
LRESULT MouseMovedHandler(UINT, WPARAM wParam, LPARAM lParam,BOOL &)
{
MouseMoved(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),wParam);
return 0;
}
virtual void MouseMoved(int x, int y, WPARAM)
{
}
我将MouseMoved函数设为虚拟,以便我可以在其他类中替换此函数。我无法理解我在哪里做错了请纠正我应该在哪里修改我的代码。
答案 0 :(得分:0)
这个问题的答案是鼠标移动功能在渲染目标初始化之前到来,以避免这种失败使用此方法
LRESULT MouseMovedHandler(UINT, WPARAM wParam, LPARAM lParam,BOOL &)
{
if (m_target)
{
MouseMoved(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),wParam);
}
return 0;
}
现在代码运行正常