0

您好,我正在使用 Direct2D,我正在使用径向渐变画笔,但我被困在一个地方。

我的径向渐变笔刷代码

结构样本窗口:桌面窗口

{
    //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 函数设为虚拟,以便我可以在其他类中覆盖此函数。我无法理解我在哪里做错了请纠正我我应该在哪里更正我的代码。

4

1 回答 1

0

这个问题的答案是鼠标移动功能在渲染目标初始化之前出现所以为了避免这种失败使用这个方法

LRESULT MouseMovedHandler(UINT, WPARAM wParam, LPARAM lParam,BOOL &)
    {
        if (m_target)
        {
            MouseMoved(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),wParam);
        }
        return 0;
    }

现在代码将运行良好

于 2012-12-26T15:16:55.443 回答