Knowledge Point 1: Association of hwnd cwnd
Associate hwnd with cwnd *
1 Cwnd * Cwnd;
2 Hwnd;
3 Cwnd = Cwnd: fromhandle (hwnd ); // Associate a window handle with an MFC window class
4 // Cwnd-> attach (hwnd );
The problem exists in the two join methods of the third and fourth rows.CodeThe method in the third line can be correctly run, but the method in the fourth line will warn that WND is not initialized and crash occurs when running the code.
Method reference:
Http://www.cppblog.com/weiwang1133/archive/2008/09/17/62097.aspx
Http://topic.csdn.net/t/20050118/16/3735215.html
Knowledge Point 2: Use of the cbrush class
Code
1 CDC * PDC = New Cclientdc (WND ); // Define and initialize the CDC pointer object
2
3 Cbrush brush; // Define painter
4 Brush. createsolidbrush (RGB ( 0 , 255 , 0 )); // Call createsolidbrush to create a solid green paint brush
5 Cbrush * Poldbrush = PDC -> SelectObject ( & Brush ); // Load the painter into the CDC object
6
7 PDC -> Rectangle ( 50 , 50 , 200 , 200 ); // Call rectangle to draw a solid green rectangle
8 PDC -> SelectObject (poldbrush ); // Restore the original paint brush. This step is not very clear and requires further study.
9 Delete PDC; // Manually release objects
Place the rectangular part in the wm_paint message, and the definition part at the beginning of the window process. It is wrong to expose my foundation again and put the definition part in the switch of the message selection before.
---------------------------------------------
Bitmap painter
Unlike the previous paint brush, you need to call createpatternbrush to initialize the paint brush.
Bool createpatternbrush (cbitmap * pbitmap)
To learn a new bitmap class cbitmap, call the createbigmap method to initialize the bitmap object and pass it to the bitmap painter. There is basically no difference in the part after drawing.
Define bitmap brush
1 // Create bitmap
2 Cbitmap bm;
3 DWORD colorbits [ 8 ] = {0x44,0x33,0x22,0x11,0x11,0x22,0x33,0x44} ;
4 BM. createbitmap ( 8 , 8 , 1 , 1 , Colorbits );
5
6 // Define brush
7 Cbrush mbrush;
8 Mbrush. createpatternbrush ( & BM );
9
10 Cbrush * Moldbrush = MDC -> SelectObject ( & Mbrush );
The part of the drawing is still placed in the wm_paiint event body. This painting draws a rounded rectangle. The roundrect method is used to define the width of the rounded corner using the crect struct and the screen spacing. The cpoint struct is used to define the diameter of the rounded corner.
Paint round rectangle
1 // Paint bitmap brush
2 MDC -> Roundrect (crect ( 20 , 20 , 200 , 200 ), Cpoint ( 10 , 10 ));
3 MDC -> SelectObject (moldbrush );
4
5 // Delete object
6 BM. deleteobject ();
7 Mbrush. deleteobject ();
8 Moldbrush = NULL;
9 Delete MDC;
10 MDC = NULL;
On the eighth line, I added the pointer to the blank. The code in the book did not care about the pointer to the original paint brush. I don't know.