简体   繁体   中英

How to draw an ELLIPSE on C++ MFC using D2D and the class CD2DEllipse

I need draw an ELLIPSE on C++ MFC using D2D and the class CD2DEllipse, I want that if I change the size of the window, the ellipse change size too.

I want that the pointer of the object see the class...and so I declare it to the header:

//hpp
class CmyclassView : public CView
{
...
CD2DEllipse* pE;
...
}

case A:

I have to initialize the ellipse object CD2DEllipse every time that I resize the view...WM_PAINT -> OnDraw2D ... but I put the ellipse on the heap because of "new"...and where go the previous ellipse...

If I close the application the d_str have to call "delete pE; " ?

// cpp
afx_msg LRESULT CDXALGOView::OnDraw2D(WPARAM wParam, LPARAM lParam)
{
C_pRT = (CHwndRenderTarget*)lParam;
ASSERT_VALID(C_pRT);
pE = new CD2DEllipse(D2D1::Ellipse(xyC,r,r));  <<-------
return TRUE;
}

case B: or is best create the object only one time in the constructor, the update the parameters on the ondraw, and when I close cthe application I delete the object c_str

{
 pE = new CD2DEllipse(D2D1::Ellipse(xyC,r,r));
}
afx_msg LRESULT CDXALGOView::OnDraw2D(WPARAM wParam, LPARAM lParam)
{
 C_pRT = (CHwndRenderTarget*)lParam;
 ASSERT_VALID(C_pRT);
 pE.point = xyC;  <<-------
 pE.radiusX = r;
 pE.radiusY = r;
 return TRUE;
}

Just create the object on the stack, when you need it:

C_pRT->DrawEllipse( CD2DEllipse(D2D1::Ellipse(xyC, r, r)), someBrush, lineWidth );

You may break up that statement for better readability:

CD2DEllipse ellipse( D2D1::Ellipse(xyC, r, r) ); 
C_pRT->DrawEllipse( ellipse, someBrush, lineWidth );

In general, there is rarely a need for new in modern C++. Most of the times, you just create objects on the stack and let their destructors do the cleanup automatically. If you actually have to allocate something on the heap, use one of the smart pointers provided by the standard library. These take care of calling delete automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM