After reading sun Xin's book, I would like to summarize the persistence function of the drawn image. In general, there are three ways to maintain a specific graph function. They are as follows:
- After drawing a graph, you can use an array of objects to save the drawn style and coordinate. You can only re-draw the stored graphic data when the screen weight is painted.
- The metadata file is used. The device context of the metadata file is used to save the drawn image. The metadata file is played every time the screen weight is painted to maintain the image.
- This method uses a compatible DC, which uses a compatible Bitmap (equivalent to a canvas). You can perform operations on all the drawing operations on this canvas, this canvas also saves all the user's painting operations. When the form is repainted, the current window redraws DC to directly copy the canvas with the drawn image to the current DC, in this way, the image persistence function is achieved.
For these three cases, I have provided the reference code and will only take notes later.
The first most common method:
Type 1: Save the redrawing data
// Customize a class to provide redrawing data
Class CGraph
{
Public:
CGraph ();
CGraph (UINT m_nDrawType, CPoint m_ptOrigin, CPoint m_ptEnd );
Virtual ~ CGraph ();
Public:
UINT m_nDrawType; // painting type
CPoint m_ptOrigin; // image Origin
CPoint m_ptEnd; // graphic endpoint
};
Next, save the drawing data after each drawing.
Void CGraphicView: OnLButtonUp (UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc (this );
CBrush * ptBrush = CBrush: FromHandle (HBRUSH) GetStockObject (NULL_BRUSH ));
Dc. SelectObject (ptBrush );
// Draw the image
Switch (m_nDrawType)
{
Case 1:
Dc. SetPixel (point, RGB (0, 0 ));
Break;
Case 2:
Dc. MoveTo (m_ptOrigin );
Break;
Case 3:
Dc. Rectangle (CRect (m_ptOrigin, point ));
Break;
Case 4:
Dc. Ellipse (CRect (m_ptOrigin, point ));
Break;
}
// Save the graphic data. Before saving, convert the set point to a logical point.
OnPrepareDC (& dc );
Dc. DPtoLP (& m_ptOrigin );
Dc. DPtoLP (& point );
// Allocate a space in the heap to save the redrawing data.
CGraph * g = new CGraph (m_nDrawType, m_ptOrigin, point); // must use a point to CGraph
// M_ptArray is a member variable of the CPtrArray type.
M_ptrArray.Add (g );
CScrollView: OnLButtonUp (nFlags, point );
}
The last step is to re-draw these images when redrawing the window.
// Before the OnDraw function is called, OnPrepareDC is called to convert the logic point to the device point.
Void CGraphicView: OnDraw (CDC * pDC)
{
CGraphicDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
// Redraw
CClientDC dc (this );
CBrush * brush = CBrush: FromHandle (HBRUSH) GetStockObject (NULL_BRUSH ));
Dc. SelectObject (brush );
For (int I = 0; I <m_ptrArray.GetSize (); I ++)
{
Switch (CGraph *) m_ptrArray.GetAt (I)-> m_nDrawType)
{
Case 1:
Dc. SetPixel (CGraph *) m_ptrArray.GetAt (I)-> m_ptEnd, RGB (0, 0 ));
Break;
Case 2:
Dc. MoveTo (CGraph *) m_ptrArray.GetAt (I)-> m_ptOrigin );
Dc. LineTo (CGraph *) m_ptrArray.GetAt (I)-> m_ptEnd );
Break;
Case 3:
Dc. Rectangle (CRect (CGraph *) m_ptrArray.GetAt (I)-> m_ptOrigin,
(CGraph *) m_ptrArray.GetAt (I)-> m_ptEnd ));
Break;
Case 4:
Dc. Ellipse (CRect (CGraph *) m_ptrArray.GetAt (I)-> m_ptOrigin,
(CGraph *) m_ptrArray.GetAt (I)-> m_ptEnd ));
Break;
}
}
}
This is a common practice, with a little more code than the other two. For the other two methods, write another two! Count the number of items!
The question of coordinate point conversion is actually very simple. You don't have to think too complicated. Actually, there are two kinds of coordinate point conversion problems. Logical points are converted into device points and device points into logical points, the coordinates used by the plotting functions are logical points. If you want to output data to a device (display, printer), you have to convert it to a device point. The origin of a device point is always the coordinate point (0, 0) in the customer area. We only need to map according to the formula to make it OK. In fact, MFC has helped us do a good job of ing. Every time we respond to the WM_PAINT event, we will call the OnPrepareDC (CDC * dc) method. In this method, the method for converting coordinates is done.
Take a look at the MFC code, and all the principles will become less mysterious!