Since you want to write DX using C ++, you know some common knowledge of C ++ for Windows SDK programming ~

xiaoxiao2021-03-06  38

*********************************************************** *** Windows SDK programming window sample program *************************************************** *************** / / * WIN32 Application Framework is mainly composed of "Window Message Function", etc. / # include "stdafx.h" 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, its function is Complete a range 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 Window sample program "; // window title name // --------------- Window class definition ------------------- ---------------- / * Window definitions In the Windows application, the window is defined in 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 LPCTSTR LPCURSORNAME / / Cursor Resource Name or System Predefined Skill ID Name) * / WNDClass.hbrbackground = (Hbrush) GetStockObject (White_brush); // Window Background is White / * GetStockObject (): Get the already defined brush, painting brush, GetStockObject font object handle () function prototype: HGDIOBJ GetStockObject (int fnObject); // fnObject object identification name * / wndclass.lpszMenuName = NULL; // no menu window wndclass.lpszClassName = lpszClassName; // window Classified name 'window instance' // ----------------- The following is the registration of the window class ------------------------------------------------ --------- / * Registration window Class 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 is no 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 with the 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)) // message loop {TranslateMessage (& MSG); DispatchMessage (& MSG);} * / msg msg; // message structure / * getMessage () Role: Read a message from the message queue and put the message in a MSG structure: BOOL getMessage (lpmsg lpmsg, // pointing to the MSG structure HWND HWND HWND HWND HWND HWND, UINT WMSGFILTERMIN, // Used for the minimum information number for message filtering Value uint WMSGFILTERMAX // The maximum information number value for message filtering, such as 0, but not filtered message); When getMessage returns 0, 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 parameter lpmsg to the specified window * / while (GetMessage (& MSG, NULL, 0, 0) // Message loop {TranslateMessage (& msg); DispatchMessage (& msg);} Return msg.wparam; // When the program is terminated, the information returns the information} // ------------ ----------------- Window function ------------------------------- ------ / * The window message processing function defines the response of the application to the received different messages, which contains the process of the application to the messages that can be received, usually, the window function is Or multiple Switch ... case statements, each CASE statement corresponds to a message, and when the application receives a message, the corresponding CASE statement is activated and performed the corresponding response program module.

转载请注明原文地址:https://www.9cbs.com/read-59311.html

New Post(0)