To continue with the previous two topics, the third way to enable the CDC to maintain its output graphics is to use compatible DC.
Ideas
First, use the current drawing DC to create a compatible DC.
Compatible with DC is equivalent to a reference to the DC associated with it. It is like a canvas where users draw images. When the screen weight is painted, copy the canvas directly to the current DC to maintain the image. Speaking of a canvas, DC is compatible with a bitmap of the same size as the current customer's region.
The Code is as follows:
Void CGraphicView: OnLButtonUp (UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc (this );
CBrush * pBrush = CBrush: FromHandle (HBRUSH) GetStockObject (NULL_BRUSH ));
Dc. SelectObject (pBrush );
// If the compatible DC has been created, no more
If (! M_dcCompatible.m_hDC)
{
M_dcCompatible.CreateCompatibleDC (& dc );
CRect rect;
GetClientRect (& rect );
CBitmap bitmap ;,
// Construct a bitmap with the same size as the current customer Zone
Bitmap. CreateCompatibleBitmap (& dc, rect. Width (), rect. Height ());
M_dcCompatible.SelectObject (& bitmap );
M_dcCompatible.BitBlt (0, 0, rect. Width (), rect. Height (), & dc, 0, 0, SRCCOPY );
M_dcCompatible.SelectObject (pBrush );
}
Switch (m_nDrawType)
{
Case 1:
M_dcCompatible.SetPixel (point, RGB (0, 0 ));
Break;
Case 2:
M_dcCompatible.MoveTo (m_ptOrigin );
M_dcCompatible.LineTo (point );
Break;
Case 3:
M_dcCompatible.Rectangle (CRect (m_ptOrigin, point ));
Break;
Case 4:
M_dcCompatible.Ellipse (CRect (m_ptOrigin, point ));
Break;
}
CScrollView: OnLButtonUp (nFlags, point );
}
When the window needs to be re-painted, the compatible DC is used to build the image. The Code is as follows:
Void CGraphicView: OnDraw (CDC * pDC)
{
CGraphicDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
CRect rect;
GetClientRect (& rect );
// Use the copy method to re-draw the image
PDC-> BitBlt (, rect. Width (), rect. Height (), & m_dcCompatible, SRCCOPY );
}
This method is simple and easy to understand.
These three methods have finally been written, which is a summary of your own learning!