first, differentiate the window class, window class object, and window
the key to differentiation is to know that the lifecycle of the C ++ window object is not the same as that of the window! (It is easy to misunderstand that the C ++ window class object is the window !) When a window is destroyed, it has no relationship with the c ++ window class object. The bond between them lies in the member variables (such as m_hwnd) in the C ++ window class ), the variable stores the handle of the window related to the c ++ window object. When a window is destroyed, the desdtroywindow () function of cwnd is called. After the window is destroyed, the corresponding window class Object (such as m_hwnd) is set to null. on the other hand , When We parse a C ++ window class object, that is, when the object is destroyed, the corresponding window object should be destroyed, because the link between the window object and the window has been broken. A window is also a kind of resource, which also occupies the memory. In this way, when the C ++ window class object is destructed, you also need to reclaim the relevant window resources, that is, destroy the window, therefore, the relationship between the two items is a single item.
draw line: You can obtain DC in four ways, draw a line with the mouse
1) Call the API Function Method to draw lines using HDC
1 HDC;
2 HDC = :: getdc (m_hwnd);
3 movetoex (HDC, m_ptorigin.x, m_ptorigin.y, null);
4 lineto (HDC, point. x, point. y);
5 :: releasedc (m_hwnd, HDC); /// must be used in pairs
The getdc () function must be used in pairs with the releasedc () function.
2) draw lines using the CDC member functions. Do not forget releasedc
1 CDC * PDC = getdc ();
2 PDC -> moveTo (m_ptorigin);
3 PDC -> lineto (point);
4 releasedc (PDC); /// must be used in pairs
Similarly, manually release the DC.
3) Use cclientdc
1 cclientdc ( This );
2 // cclientdc DC (getparent ();
3 DC. moveTo (m_ptorigin);
4 DC. lineto (point); /// releasedc is not required here, because cclientdc will automatically release the DC
The cclientdc constructor transmits the object of a window class. It automatically releases the DC after use, and does not need to explicitly call releasedc ()
4) use cwindowdc to draw lines in the entire screen area.
1 Cwindowdc DC ( This );
2 // Cwindowdc DC (getparent ());
3 /* Cwindowdc DC (getdesktopwindow (); // you can draw a line on the screen.
4 DC. moveTo (m_ptorigin );
5 DC. lineto (point ); */
6 /* Cpen pen (ps_dot, 1, RGB (0,255, 0 ));
7 Cclientdc DC (this );
8 Cpen * poldpen = Dc. SelectObject (& pen );
9 DC. moveTo (m_ptorigin );
10 DC. lineto (point );
11 DC. SelectObject (poldpen ); */
Similarly, the cwindowdc constructor also needs to pass the object of a window class. The difference from cclientdc is that this object can access the entire window area, including the frame window (out of the menu bar and toolbar ). Cclientdc can only access the customer zone.
At the same time, you can get a preliminary understanding
Cpen * poldpen = Dc. SelectObject (& pen );
Function. Pay attention to its return value!