2012-12-26 112 views
0

你好我正在使用Direct2D,而我正在使用徑向漸變畫筆,但我被困在一個地方。C++ Direct2D徑向漸變畫筆

我的徑向漸變畫筆代碼

結構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功能虛擬的,這樣我就可以在ovverride我的其他類此功能。我無法理解我在哪裏做錯了請糾正我在哪裏應該在我的代碼中進行更正。

回答

0

這個問題的答案是,鼠標移動功能來渲染目標初始化之前所以要避免這種失敗使用這種方法

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

現在的代碼將運行良好