/ ************************************************** **** Windows SDK programming window one sample program ************************************** **************** /
/ * WIN32 application framework is mainly composed of "Window Message Function", etc. in the "Initialization Window Class", "Window Registration Class", "Window Message Function", etc.
LResult Callback WndProc (HWND, UINT, WPARAM, LPARAM); // Window Function Description
/ * WinMain function is the entry of all Windows applications, similar to the main function in the C language, which is functional to complete a series of definitions and initialization work and generate a message loop. The message loop is the core of the entire program. The WinMain function implements the following features. 1. Register the window class, create a window and perform other necessary initialization work; 2. Enter the message loop, call the corresponding processing process according to the message received from the application message queue 3. If the message loop retrieves the WM_QUIT message, termination of the program operation .
Winmain functions have three basic components: function description, initialization, and message loop.
The description of the Winmain function is as follows: int WinApi WinMain (// WinMain Function Description Hinstance Hinstance, // Program Current instance handle Hinstance HPREVINSTANCE, // Applications Other instance handle LPSTR LPCMDLINE, // Pointer Pointer INT NCMDSHOW // The integer value identifier of the window display mode when the application starts execution) Since the Window operating system is a multitasking operating system, multitasking can be managed, so the Windows application may be executed multiple times in parallel, so the same program may occur. The plurality of windows simultaneously exist, the Windows system performs an instance of the application every time the application is called the application and uniquely identifies it with an instance handle.
* / int apientry winmain (Hinstance Hinstance, // Winmain Function Description Hinstance Hprevinstance, LPSTR LPSTR LPCMDLINE, INT NCMDSHOW) {// Todo: Place Code Here. / * It is recommended to use PASCAL variable definition style, that is, at the beginning of the program (function) Define all variables Although C variable definition is more flexible, this program is easy to understand, no use of this method * / char lpszclassname [] = "window"; // window class name char lpsztitle [] = "Windows SDK programming One window sample program "; // window title name
/ / --------------- Window definition -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----- / * Definition of window classes In Windows applications, windows define the form and functionality of the window. Window class definition is completed by assigning the window data structure WNDClass, including the various properties of the window class, often used in the window class definition process: * / WNDCLASS WNDCLASS; WNDCLASS.Style = 0; // Window Type is the default type WNDCLASS.LPFNWNDPROC = WndProc; // window handler is wndproc wndclass.cbclsextra = 0; // window classes Non-extended wNDClass.cbwndextra = 0; // Window instance no extended wndclass.hinstance = Hinstance; // Current instance handle WNDCION (NULL, IDI_APPLICATION); // Using the default icon / * loadicon (): Load a window icon loadicon () function in the application: HiCon Loadicon (Hinstance Hinstance, // icon The module handle where the resource is located, uses the system predefined icon LPCTSTSTSTSTSTSTSTSTSTSTSTSTSR LPICONNAME // icon resource name or system predefined icon identifier name) * / WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow); // window Adopt arrow cursor / * LoadCursor (): Loads a window cursor loadCursor () function in the application: HCURSOR LOADCURSOR (Hinstance Hinstance, the module handle where the cursor resource is located, using the system predefined cursor LPCTSTSTR LPCURSORNAME / / Cursor Resource Name or System Predemended Number Name) * /
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); // window background is white / * GetStockObject (): Get the brush has been defined, brush, font object handles GetStockObject () function prototype: HGDIOBJ GetStockObject (int fnObject ); // fnObject is the identification name of the object
* /
WNDCLASS.LPSZMENUNAME = NULL; // None menu Wndclass.lpszclassName = lpszclassname; // window class name '
// ----------------- The following is the registration of window classes --------------------- ---- / * The WINDOWS system itself provides a partial predefined window class, programmers can also customize window classes, and window classes must be used first. The registration of the window class is implemented by the registration function regiSterclass (). The form is: registerclass (& wndclass) & Wndclass is the return value of the window class structure RegisterClass function, and the registration is successful. Returns true * / if (! Registerclass (& wndclass)) // Register window, if it fails, then sound {MessageBeep (0); RETURN FALSE;} / * Creating a Window Instance Create a window class Instance implemented by the function createWindow (), the prototype of this function is: hwnd createWindow (LPCTSTR LPSZCLASSNAME, / / Create window, window class name LPCTSTR LPSZTILE, / / Window instance Title DWORD DWSTYLE, / / Window Style INT X, // Window Left Card Card INT Y, // Window Left Card Coordinate INT NWIDTH, // Window Width INT NHEIGHT, // Window High HWND HWNDParent , // This window parent window hwenu hmenu, // This window main menu hinstance hinstance, // application current handle LPVOID LPPARAM); / / Pointer to a pointer to the window * / / creation window Operation HWND HWND ; // window structure hwnd = CreateWindow (lpszClassName, // create a window, window class name style CW_USEDEFAULT lpszTitle, title name WS_OVERLAPPEDWINDOW // window instance, // window, CW_USEDEFAULT, // upper left corner of the window coordinates to the default value CW_USEDEFAULT , Cw_usedefault, // The height and width of the window are default NULL, // This window does not have the parent window null, // This window does not have the main menu hinstance, // application current handle NULL); // Do not use this value showwindow (hwnd, ncmdshow); // Display Window
UpdateWindow (hwnd); // Draw a user area
/ * Message loop Windows application runs by message as the core. Windows puts the generated message in the application's message queue and the message WinMain function message loops to extract the message in the message queue, and passes it to the window function to process the corresponding processing process. MSG msg; // message structure
While (GetMessage (& MSG, NULL, 0, 0)) // Message Cycle {TranslateMessage (& MSG); DispatchMessage (& MSG);} * /
Msg msg; // message structure / * getMessage () effect: read a message from the message queue and put the message in a MSG structure: BOOL getMessage (lpmsg lpmsg, // Pointer to the MSG structure hWnd hwnd, uint WMSGFILTERMIN, / / The minimum information number value for message filtering UINT WMSGFILTERMAX / / The maximum information value for message filtering, such as the minimum and maximum value, but not the message is recorded); when the getMessage returns 0, the search Go to the WM_QUIT message, the program will end the loop and exit Bool TranslateMessage (Const Msg * LPMSG); responsible for converting the virtual key value of the message to character information LRESULT DispatchMessage (const msg * lpmsg); pass the message pointing to the parameter lpmsg to the specified window to the specified window
* / While (& MSG, NULL, 0, 0)) // Message loop {TranslateMessage (& MSG); DispatchMessage (& MSG);
Return information to the operating system when return msg.wparam; //
/ / ----------------------------------------------- -------------------- / * Window message processing function defines the response of the application to the received number of messages, which contains the application to various available The process of the received message, usually, the window function consists of one or more Switch ... CASE statements, each CASE statement corresponds to a message, and when the application receives a message, the corresponding CASE statement is activated and Perform the corresponding response program module. The general form of window functions is as follows: LResult Callback WindowProc (HWND HWND, UINT UMSG, WPARAM WPARAM, LPARAM LPARAM); parameters
hwnd: [in] Handle to the window uMsg:. [in] Specifies the message wParam:. [in] Specifies additional message information The contents of this parameter depend on the value of the uMsg parameter lParam:.. [in] Specifies additional ........................ ..
Lresult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {Switch (Message) {Case .... Break; ........ Case WM_DESTROY: // Void PostquitMessage (Int NEXITCODE) The function of the function is to send a WM_QUIT message to the program, the NEXITCODE application exits the code postquitMessage (0); // Call the function to issue a wm_quit message DEFAULT: / / default message processing function to ensure that the message sent to the window Can be processed Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);} return (0);}
* / LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_DESTROY: PostQuitMessage (0); // call the function WM_QUIT message sent default: // return the default message handler DefWindowProc (hwnd, message, wparam, lparam);
RETURN (0);} / * Note: Features of event drivers: Windows programming runs a run message processing function around the generation of events or messages. The execution order of the Windows program depends on the order in which the event occurs. The execution of the program is driven by the message generated, and the programmer can write a message handler for the message type to handle the received message, or send other messages to drive other handler. However, it is not necessary to predetermine the order of production. This is a significant feature of the event driver in object-oriented programming. The event-driven programming method is useful to write a interactive program, and the program written in this method allows the program to avoid the operation mode of the dead plate, so that users can use their own wishes to adopt flexible and more modified operating modes. Message delivery mechanism in the Windows application: There are several system definitions in the VC classification, commonly used messages, initialization messages, input messages, system messages, clipboard messages, text messages, DDE (dynamic data) Exchange) message, application custom message, etc. The message sent by the application is sent to the message queue, and the message is processed according to the order reached, and the response message processing module code is called. * /