The first Win32 program, simple to create a window:
#include<windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szcmdline,inticmdshow) { StaticTCHAR szappname[] = TEXT ("Hellowin") ; HWND hwnd; MSG msg; Wndclass Wndclass; Wndclass.style= Cs_hredraw |Cs_vredraw; Wndclass.lpfnwndproc=WndProc; Wndclass.cbclsextra=0 ; Wndclass.cbwndextra=0 ; Wndclass.hinstance=hinstance; Wndclass.hicon=LoadIcon (NULL, idi_application); Wndclass.hcursor=loadcursor (NULL, Idc_arrow); Wndclass.hbrbackground=(Hbrush) getstockobject (White_brush); Wndclass.lpszmenuname=NULL; Wndclass.lpszclassname=Szappname; if(! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror); return 0 ; } hwnd= CreateWindow (Szappname,//window class nameTEXT ("The Hello program"),//Window CaptionWs_overlappedwindow,//window StyleCw_usedefault,//Initial x positionCw_usedefault,//Initial y positionCw_usedefault,//Initial x SizeCw_usedefault,//Initial y sizeNull//parent Window HandleNull//Window menu HandleHINSTANCE,//Program Instance handleNULL);//Creation ParametersShowWindow (hwnd, icmdshow); UpdateWindow (HWND); while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); } returnMsg.wparam;} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc; Paintstruct PS; Rect rect; Switch(message) { Casewm_create:return 0 ; CaseWM_PAINT:HDC= BeginPaint (hwnd, &PS); GetClientRect (hwnd,&rect); DrawText (hdc, TEXT ("Hello, Windows program!"), -1, &rect, Dt_singleline| Dt_center |dt_vcenter); EndPaint (hwnd,&PS); return 0 ; CaseWm_destroy:postquitmessage (0) ; return 0 ; } returnDefWindowProc (hwnd, message, WParam, LParam);}
A window is created by a window class that is WNDCLASS, and the window class determines the window procedure for processing window messages.
This is defined in MSDN:
struct { UINT style; WNDPROC Lpfnwndproc; int Cbclsextra; int Cbwndextra; HINSTANCE hinstance; Hicon Hicon; Hcursor hcursor; Hbrush Hbrbackground; LPCTSTR Lpszmenuname; LPCTSTR *pwndclass;
In the program, we generally define and initialize this
Wndclass Wndclass; Wndclass.style = Cs_hredraw | Cs_vredraw; Wndclass.lpfnwndproc = WndProc; Wndclass.cbclsextra = 0 ; Wndclass.cbwndextra = 0 ; Wndclass.hinstance = hinstance; Wndclass.hicon = LoadIcon (NULL, idi_application); Wndclass.hcursor = LoadCursor (NULL, Idc_arrow); Wndclass.hbrbackground = (Hbrush) getstockobject (White_brush); Wndclass.lpszmenuname = NULL; Wndclass.lpszclassname = szappname;
The Wndclass.style field represents the style of the class, specifically participating in MSDN:
Style |
Action |
Cs_bytealignclient |
Aligns the window's client area to a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display. |
Cs_bytealignwindow |
Aligns the window on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display. |
Cs_classdc |
Allocates one device context to is shared by all windows in the class. |
Cs_dblclks |
Sends a double-click message to the window procedure when the user double-clicks the mouse While the cursor was within a window belonging to the class. |
Cs_dropshadow |
Windows XP: Enables the drop shadow effect on a window. The effect is turned on and off through Spi_setdropshadow. |
Cs_globalclass |
Specifies the window class is an application global class. For more information, see Application Global Classes. |
Cs_hredraw |
Redraws the entire window if a movement or size adjustment changes the width of the client area. |
Cs_noclose |
Disables Close on the Window menu. |
Cs_owndc |
Allocates a unique device context for each window in the class. |
Cs_parentdc |
Sets the clipping rectangle of the child window to that of the parent window so, the child can draw on the parent. A window with the CS_PARENTDC style bit receives a regular device context from the system ' s cache of device contexts. |
Cs_savebits |
Saves, as a bitmap, the portion of the screen image obscured by a window of this class. |
Cs_vredraw |
Redraws the entire window if a movement or size adjustment changes the height of the client area. |
What we have chosen here is Cs_hredraw | Cs_vredraw, which indicates that the window will be redrawn, regardless of the vertical or horizontal size changes.
Wndclass.lpfnwndproc represents a window procedure setting, pointing to a function WndProc.
Wndclass.cbclsextra and Cbwndextra are used to maintain some of the reserved space in the internal window structure and can use these reserved spaces as needed.
Wndclass.hinstance indicates that the application's instance handle is WinMain the first argument.
Wndclass.hicon sets the icon for the window class. To use the local icon, the first argument must be a hinstance instance handle, the second parameter is an icon, and the predefined icon begins with IDI.
Wndclass.hcursor, like above, loads the mouse pointer.
Wndclass.hbrbackground Specify the background color, here we use white paint brush.
Wndclass.lpszmenuname is the Window menu.
WNDCLASS.LPSZCLASSNAME specifies a name for the window class.
So the initialization of the window class is complete, using the RegisterClass registration class, the following is the creation of the window,
Using the CreateWindow function, MSDN is defined as follows:
HWND CreateWindow ( __in lpctstr lpclassname,//window name __in lpctstr lpwindowname,//Window caption __in< C6/>dword dwstyle,//Window style __in int x,//x coordinate __in int c14> y, //y coordinate __in int nwidth, //length __in int nheight,//height __in HWND hwndparent,//parent window handle __in HMENU HMENU, //Window menu handle
__in hinstance hinstance,// program instance handle __in lpvoid lpparam //create parameter);
CreateWindow only creates Windows internally, that is, Windows allocates a piece of memory, and you want to display the window using the ShowWindow function.
The first parameter is the window handle created by CreateWindow, and the second is the Icmdshow value that WinMain accepts, which determines the window's initial display format.
UpdateWindow is the redraw window, which is done by sending a WM_PAINT message.
The last is the loop of the message, which is to accept the message and call the WndProc function for processing when there is a message.
Windows programming--creating windows