Procedures for Win32 Windows programs

Source: Internet
Author: User
Tags textout win32 window

Review the Win32 Window program

Design a window class;
Registration window class;
Create a window;
Displays and updates the window.
Message loop, to get the message;
The message response for the window procedure function.

Design window class:

typedefstruct_wndclass {UINT style;//Types of window classesWNDPROC Lpfnwndproc;//window procedure function (callback function) intCbclsextra;//set additional memory space for this class intCbwndextra;//set additional memory space for this windowHANDLE hinstance;//instance number of the current applicationICON Hicon;//icons, typically assigned with the LoadIcon functionHcursor Hcursor;//cursors, typically assigned with the LoadCursor functionHbrush Hbrbackground;//window Background getstockobject ()LPCTSTR Lpszmenuname;//set Menu name (constant string)LPCTSTR lpszClassName;//Design the name of a window class} wndclass;

Type of window class:

A class of variables is often used in our programs, and each bit of this variable corresponds to a particular feature. When a bit of the variable is 1 o'clock, it indicates that the attribute corresponds to that bit, and when that bit is 0 o'clock, there is no attribute corresponding to that bit. When a certain number of bits in a variable is 1 o'clock at the same time, it represents a combination of several attributes. Which one of the variables represents which meaning, not easy to remember, so we often according to the characteristics of the English spelling of the capitalization to define some macros, the macro corresponding to the value of only the number corresponding to the character (bit) is 1, the rest of the bit is 0. We can find cs_vredraw=0x0001,cs_hredraw=0x0002,cs_dblclks =0x0008,cs_noclose=0x0200 using goto definition. What they have in common is that only one is 1 and the remaining bits are 0. If we want the values of a variable to have both Cs_vredraw and Cs_hredraw properties, we simply use the binary or (|) operator to combine them or the operations, such as Style=cs_vredraw | Cs_hredraw | Cs_noclose. If we want to remove one of the characteristics of some of the original characteristics of a variable, with the inverse (~) and then the (&) operation, it can be achieved, such as on the basis of the style just removed cs_noclose features, you can use style & ~cs_ Noclose implementation.

Window procedure functions:

The second member variable, LPFNWNDPROC, specifies the procedure function for this type of window, also called the callback function. The principle of the callback function is that when the application receives a message to a window (remember that the message that was previously told is usually window-related?). ), you should call a function to process the message. This invocation procedure is performed by the operating system without the application's own implementation, but the code of the callback function itself must be done by the application itself. For a message, what function (callback function) does the operating system call in the application to handle? The operating system invokes the function specified by the LPFNWNDPROC member in the type that the window that receives the message belongs to. Each of the different types of Windows has its own dedicated callback function, which is specified by the Lpfnwndproc member.

Register window class:

RegisterClass (&wndclass);

To create a window:

HWND CreateWindow (LPCTSTR lpclassname,//Pointer to registered class nameLPCTSTR Lpwindowname,//Pointer to window nameDWORD Dwstyle,//window Style    intX//horizontal position of window    intY//vertical position of window    intNwidth,//window Width    intNheight,//Window HeightHWND hWndParent,//handle to Parent or owner windowHMENU HMENU,//handle to menu or Child-window identifierHANDLE HInstance,//handle to application instanceLPVOID Lpparam//pointer to window-creation data);

Display and update window:

The ShowWindow function sets the specified window'BOOL ShowWindow (     //  Handle to Window     int//BOOL updatewindow (             //  handle of window);

Message loop (Get message):

BOOL GetMessage (        //  address of structure with message       //  handle of Window        // First message        // last message);
msg msg;      while (GetMessage (&msg,null,0,0))    {        TranslateMessage (&msg);        DispatchMessage (&msg);    }

Window procedure function (callback function):

LRESULT CALLBACK Winsunproc (HWND hwnd,//Handle to WindowUINT umsg,//message identifierWPARAM WPARAM,//First message parameterLPARAM LPARAM//Second message parameter){    Switch(umsg) { CaseWm_lbuttondown: Break;  CaseWm_close: Break;default:        returnDefWindowProc (Hwnd,umsg,wparam,lparam); }    return 0;}
//xinxin Sun Video case Code reference//run Environment Visual C + +: New Win32 application (note type) project, and then create a new WinMain.cpp C + + source file)//Win32 Window Program#include<windows.h>#include<stdio.h>//window procedure function declarationLRESULT CALLBACK Winkevinproc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM); intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine,intncmdshow) { //Designing a window classwndclass wndcls; Wndcls.cbclsextra=0; Wndcls.cbwndextra=0; Wndcls.hbrbackground=(Hbrush) getstockobject (Black_brush); Wndcls.hicon=LoadIcon (null,idi_error); Wndcls.hcursor=loadcursor (Null,idc_cross); Wndcls.hinstance=hinstance; Wndcls.lpfnwndproc= Winkevinproc;//callback functionWndcls.lpszclassname ="zcx"; Wndcls.style= Cs_hredraw |Cs_vredraw; Wndcls.lpszmenuname=NULL;//Register window classRegisterClass (&wndcls); //Create window, save window handleHWND hwnd ; hwnd= CreateWindow ("zcx","* * University", Ws_overlappedwindow,0,0, -, -, NULL, NULL, HINSTANCE, NULL); //Display and Refresh WindowsShowWindow (hwnd, SW_SHOWNORMAL); UpdateWindow (HWND); //define the message structure body, start the message loopmsg msg; while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); }//EndOf while return 0;} //endof WinMain ()//Writing window procedure functionsLRESULT CALLBACK Winkevinproc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {Switch(umsg) { CaseWM_CHAR:Charszchar[ -]; sprintf (Szchar,"Char is%d", WParam); MessageBox (Hwnd,szchar,"zcx",0);  Break;  CaseWm_lbuttondown:messagebox (hwnd,"Mouse Clicked","zcx",0);  HDC hdc; HDC= GetDC (hwnd);//cannot be called when responding to a WM_PAINT messageTextOut (HDC,0, -,"* * University", strlen ("* * University"));  ReleaseDC (HWND,HDC);  Break;  CaseWM_PAINT:HDC HDC;  Paintstruct PS; HDC= BeginPaint (HWND,&AMP;PS);//can only be called when responding to WM_PAINT messagesTextOut (HDC,0,0,"zcx", strlen ("zcx")); EndPaint (hwnd,&PS);  Break;  CaseWm_close:if(Idyes = = MessageBox (hwnd,"Do you really quit the program?","pop-up window tips", Mb_yesno))  {DestroyWindow (HWND); }//endof IF   Break;  CaseWm_destroy:postquitmessage (0);  Break; default:  returnDefWindowProc (hwnd,umsg, WParam, LParam);}//endof swith () return 0;}//End of Winsunproc ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.