The basic code created by the Windows window:
#include <Windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);intWINAPI WinMain (__in hinstance hinstance, __in_opt hinstance hprevinstance, __in LPSTR lpCmdLine, __i NintnShowCmd) {TCHAR* AppName = TEXT ("Demo"); 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=AppName; if(! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("Register Class Error"), AppName,0); return 0; } hwnd=CreateWindow (AppName, TEXT ("Demo"), Ws_overlappedwindow, Cw_usedefault, Cw_usedefault, Cw_usedefault, Cw_usedefault , NULL, NULL, HINSTANCE, NULL); ShowWindow (hwnd, NSHOWCMD); UpdateWindow (HWND); while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); } returnMsg.wparam;} LRESULT CALLBACK WndProc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc; Paintstruct PS; Rect rect; Switch(msg) { Casewm_create: {return 0; } Casewm_paint: {hdc= BeginPaint (hwnd, &PS); GetClientRect (hwnd,&rect); DrawText (hdc, TEXT ("Ray_xujianguo"), -1, &rect, dt_singleline| dt_center|dt_vcenter); EndPaint (hwnd,&PS); return 0; } CaseWm_destroy: {postquitmessage (0); return 0; } } returnDefWindowProc (hwnd, MSG, WParam, LParam);}
The following section explains:
1. There are some functions we can take a look at their role
LoadIcon load icon loadcursor load the mouse cursor getstockobject Get a drawing object, this example is to get a brush registerclass Register a window class MessageBox message box CreateWindow Create a window showwindow display window UpdateWindow Redraw Window GetMessage getting information from message queues translatemessage translating keyboard messages DispatchMessage forwarding messages to Windows BeginPaint window starts drawing getclientrect Gets the size of the window client area DrawText displays a text string endpaint Drawing of the End window postquitmessage the "exit" message into the message queue DefWindowProc perform the default message handling
2. Some numeric constants
In the above program we see the Cw_usedefault constants, in fact, we can see from the front of the upper case the type of the constant is what.
Prefix constant CS class style option CW Create window option DT text draw option IDI icon ID number IDC cursor ID number MB message box options WM window message WS window style
3. Understanding Handles
HINSTANCE Instance Handle-the program itself HWND window handle HDC device Environment handle
4. Registration of window classes
Look at the contents of Wndclass:
5. Creation of Windows
The window class simply defines the general characteristics of the window, so you can create many different windows based on the same window class, and you can specify more details when you call the CreateWindow function to create the window.
6. Display of Windows
7. Message Loops
First obtains the message from the message queue, then translates the keyboard the message, then forwards the message to the window procedure function processing, after the window program function processing completes, immediately carries on the next round of message loop.
8. Window procedure Functions
Creation of Windows window