Three methods to keep the image output by the CDC-1

Source: Internet
Author: User

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:

  1. 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.
  2. 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.
  3. 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.