MFC learning drawing notes (1)

This week's software development homework, I was also initially learning MFC (commonly known as having no food). It really feels a bit obscure in operation. Today I will share some basic drawing methods of MFC.

1. Onpaint () function:

1  void CMyDrawer_CPlusDlg :: OnPaint ()
 2  {
 3      if (IsIconic ())
 4      {
 5          CPaintDC dc ( this ); // Device context for drawing 
6  
7          SendMessage (WM_ICONERASEBKGND, reinterpret_cast <WPARAM> (dc.GetSafeHdc () ), 0 );
 8  
9          // center the icon in the workspace rectangle 
10          int cxIcon = GetSystemMetrics (SM_CXICON);
 11          int cyIcon = GetSystemMetrics (SM_CYICON);
 12          CRect rect;
 13          GetClientRect (& rect);
14          int x = (rect.Width ()-cxIcon + 1 ) / 2 ;
 15          int y = (rect.Height ()-cyIcon + 1 ) / 2 ;
 16  
17          // Draw icon 
18          dc.DrawIcon (x, y , m_hIcon);
 19  
20      }
 21      else 
22      {
 23          // Image refresh 
24          if (draw)
 25          {
 26              BitBlt (ownerHDC, 0 , 0 , ownerRect.right, ownerRect.bottom, memHDC, 0 , 0, SRCCOPY); // Display the drawn image 
27              DrawLine (ownerHDC, xFirst, yFirst, xSecond, ySecond);        
 28          }
 29          else 
30          {
 31              BitBlt (ownerHDC, 0 , 0 , ownerRect.right, ownerRect.bottom, memHDC, 0 , 0 , SRCCOPY); // Display the drawn image 
32          }
 33          CDialogEx :: OnPaint ();
 34          // ------------------------- ------------------------------------------ Things I Draw 
35          CClientDC dc ( this ); //Selected drawing current environment 
36          the HDC HDC = the this -> the GetDC () -> the m_hDC;
 37 [      / *     DrawBall (HDC, 400, 100, 450, 150); * / 
38 is          a CPen mypen (the PS_SOLID, 2 , the RGB ( 255 , 0 , 0 ));
 39          CBrush mybrush (RGB ( 0 , 0 , 255 ));
 40          CFont myfont;
 41          myfont.CreatePointFont ( 200 , L " Times New Roman " );
 42          CPen * oldpen = (CPen *)SelectObject(hdc, mypen);
43         CBrush *oldbrush = (CBrush *)SelectObject(hdc, mybrush);
44         CFont *oldfont = (CFont *)SelectObject(hdc, myfont);
45 
46         MoveToEx(hdc, 50, 250, NULL);
47         LineTo(hdc, 50, 450);
48         MoveToEx(hdc, 125, 250, NULL);
49         LineTo(hdc, 125, 450);
50         ::TextOut(hdc, 150, 405, L"Stack " , 1 );
 51          :: TextOut (hdc, 150 , 25 , L " non-recursive implementation of preorder traversal " , 10 );
 52          /// / ============== ====================================== 
53  
54          SelectObject (hdc, oldpen);
 55          DeleteObject ( mypen);
 56          SelectObject (hdc, oldbrush);
 57          DeleteObject (mybrush);
 58          SelectObject (hdc, oldfont);
 59          DeleteObject (myfont);
 60  
61  
62      }
 63 }

This is a demo of the Onpaint () function in MFC, because I have just learned it, so I will give the simplest guidance to people who are just new to MFC drawing like me.

After adding the Onpaint () function in the resource view class wizard, MFC will automatically generate a function containing the framework. All we have to do is in this framework

CDialogEx::OnPaint();

 

After this sentence, just fill in what you want. It should be noted that before drawing the interface, you need to handle the HDC of the window, and then use the various functions and drawing methods that have been encapsulated in the MFC to draw the picture to be drawn on the HDC of the form. It can be understood simply as an object of a painting operation. Before each painting, an object must be obtained.

        DC CClientDC ( the this ); // Select the current drawing environment 
        HDC HDC = the this -> GetDC () -> m_hDC;

The meaning of these two lines of code is to get the hdc handle of the form currently to be spent.

Then you can start drawing!

2. Several commonly used functions for drawing:

2.1: Draw a straight line

        MoveToEx(hdc, x1, y1, NULL);
        LineTo(hdc, x2, y2);

The first function is used to locate the starting point coordinates of the line (x1, y1), the second function is used to locate the end point coordinates of the line (x2, y2);

2.2: Draw a circle

Ellipse(hdc, x0, y0, x1, y1);

This function can draw not only a circle but also an ellipse. The two coordinate values ​​defined are the coordinates of the apex of the upper left corner and the coordinates of the apex of the lower right corner of the rectangle circumscribed to the (ellipse) circle.

Change after the rest.

 

Guess you like

Origin www.cnblogs.com/ZJU-LOSER/p/12758087.html