最近学习了windows编程,以防忘记,特意来做一下小小的总结,走起!!! 这篇文章主要说两件事:windows消息机制概括,窗口建立过程。
Windows Messaging Mechanism overview
First of all:
This figure is excerpted from an CSDN blog, thanks to the author, who will tell you the address.
In this figure:
1,数字1箭头: 表示windows(os)将消息传递到进程的消息队列中 2,数字2箭头: 表示进程通过消息循环在消息队列中读取消息 3,数字3箭头: 表示进程的消息循环将读取到的消息告诉给windows(os) 4,数字4箭头: 表示windows(os)通过进程传递的消息来调用进程绑定的回调函数(WndProc),根据回调函数,来确定是否做出响应.
Do you understand a lot now?
Window creation Process
Let's take a look at a procedure that was excerpted from the first example of Mr. P (Charles Petzold) (HELLOWIN.C).
#include <windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);//callback function declarationintWINAPI WinMain (HInstance hinstance,//Process handleHInstance hPrevInstance,//Previous process handlePSTR szCmdLine,//// command-line parameters intIcmdshow)//Window display status{StaticTCHAR szappname[] = TEXT ("Hellowin");//string ending with ' \ s ' character, here is the name of the program (actually used as the name of the following window class)HWND hwnd;//Window handleMSG msg;//Message structure bodyWndclass Wndclass;//Window classWndclass. Style= Cs_hredraw | Cs_vredraw;//style of window classWndclass. Lpfnwndproc= WndProc;//Window class-bound callback function (also known as window procedure)Wndclass. Cbclsextra=0;//Window class extra parameters (default = 0)Wndclass. Cbwndextra=0;//Window class extra parameters (default = 0)Wndclass. HInstance= HInstance;//Process handleWndclass. Hicon= LoadIcon (NULL, idi_application);//Load program iconWndclass. Hcursor= LoadCursor (NULL, Idc_arrow);//Load cursorWndclass. Hbrbackground= (Hbrush) getstockobject (White_brush);//Background BrushWndclass. Lpszmenuname=NULL;//Menu name PointerWndclass. lpszClassName= Szappname;//Name of window class if(! RegisterClass (&wndclass))//Registration window class{MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror);return 0; }//Create a memory here that stores the information for creating the window and returns a handle to the window (the window's label)hwnd = CreateWindow (Szappname,//Name of window classTEXT ("The Hello program"),//Window name (what the title bar displays)Ws_overlappedwindow,//Window styleCw_usedefault,//window x-coordinateCw_usedefault,//Window y-coordinateCw_usedefault,//Window widthCw_usedefault,//Window length NULL,//Parent window handle NULL,//Menu handleHINSTANCE,//Process handle NULL);//Window creation data /* When the program finishes running CreateWindow and ShowWindow is not running, Windows sends the first message to the WndProc of the window: Wm_create */
ShowWindow (hwnd, icmdshow);//Display windowUpdateWindow (HWND);//Update window /* After running UpdateWindow, Windows sends a second message to the WndProc of the window: WM_PAINT. If the ShowWindow is not invoked or the call fails, WM_PAINT message is not sent * / //message loop while(GetMessage (&msg,NULL,0,0))//null cannot be written as HWND, otherwise the program will not run properly{TranslateMessage (&msg);//Convert keyboard messagesDispatchMessage (&MSG);//Send messages to Windows (OS), tuned back and forth by Windows}returnMsg. WParam;//End return}//Window procedure (callback function)LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc;//device handlePaintstruct PS;//contains the information drawn by the client area of the window is a structural bodyRect rect;//Rectangle (two points) //Here is the message processing Switch(message) {//Window creation message CaseWm_create:return 0;//Window client area redraw message CaseWM_PAINT:HDC = BeginPaint (hwnd, &PS);//Get device handle, start redraw (WM_PAINT, other time with GetDC ())GetClientRect (hwnd, &rect);//Get the size of the client areaDrawText (HDC, TEXT ("Hello, Windows 10!"), -1, &rect, Dt_singleline | Dt_center | Dt_vcenter);//Draw a stringEndPaint (hwnd, &PS);//End Redraw (dedicated in Wm_paint, other times with ReleaseDC ()) return 0;//Window Exit message CaseWm_destroy:postquitmessage (0);//Send Exit message return 0; }returnDefWindowProc (HWND, message, WParam, LParam);//windows Default handling of messages //(Note: The third and fourth parameters cannot pass an error!!!) }
Presumably the next time you look at the time should be able to quickly recall it, hey.
Reference: http://blog.csdn.net/crocodile__/article/details/9417439
Windows Programming Small Note