VC ++ instance programming series - the first day

zhaozj2021-02-08  214

Part 1: Win32 SDK learning

On the first day, make a blank window

Let's first figure out the process of WIN32 SDK applications:

Let's take a look at how the Win32 program starts:

In C or C , a DOS program starts from the main () function, then the Windows program starts from the main () function? Of course, the answer is NO. So what start from the Windows program? In Windows, the program starts from a function called WinMain (), which is like the main () function in the DOS program.

Let's take a look at the format of the Winmain () function:

Int WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NSHOWCMD)

Parameter Description:

Hinstance: Current instance handle, automatically assigned by the system. HPREVINSTANCE: The last instance handle is always NULL in Win32. LPCMDLINE: The command line parameters of this program, did not expect it, the program under Windows also supports command line parameters. This is a string. Nshowcmd: Specifies how to display the window. This parameter, I have never understood that the program is started by the system, I don't know how to enter this parameter.

return value:

If you exit after receiving a WM_QUIT message, you should return the WPARAM parameter of the message; if you exit before the message loop start, you should return 0.

The beginning of the Win32 program is clear, what should this function be a bit?

In the WinMain () function, these items should generally be completed: (Structure and function detailed explanation of Win32 SDK)

Register a new main window type; you need to understand a structure and a function: WNDCLASS structure; RegisterClass function newly created and display the main window; you need to understand the CREATEWINDOW function, showwindow function, and UpdateWindow function. Start the message loop of the main window. Here you need to understand the getMessage function, TranslateMessage function, DispatchMessage.

Ok, try to write an empty window program now:

/ / -------------------------------------------------------------------------------------------- -------------------- // ********************* Editor main header ********************* // file name: Heditor.h // -------------- -------------------------------------------------- ---- bool H_RegWindowsClass (hINSTANCE hInstance); bool H_InitWindow (hINSTANCE hInstance, int nShowCmd); LRESULT CALLBACK H_WndProc (HWND hwnd, UINT uMsg, wPARAM wParam, lPARAM lParam); // ---------- -------------------------------------------------- ------- // ******************* Huaxi Ying multi-function editor main file *********** *********** // file name: Heditor.cpp // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- // Contains file #include / / Must contain a #include "heditor.h" // global variable hinstance h_instance; // save the variable of the current instance handle, for later use // Function int WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT nShowCMD) {msg Cu_MSG; // Message Structure IF (HinRegWindowsClass (Hinstance)) // Register New Window Type {// If the registration success h_instance = Hinstance; // Save the current instance handle H_initWindow (Hinstance, nshowcmd); // Create a window and display while (GetMessage (& Cu_MSG, NULL, 0, 0)) // Start Message Cycle {TranslateMessage (& Cu_MSG); // Translation Character Messaging DITCHMESSAGE (& Cu_MSG); // Assign Information} Return (Cu_Msg.wParam);} Return (0);} BOOL H_REGWINDOWSCLASS (Hinstance Hinstance) // Register Window Type { WNDCLASS WC; // Defines class registration structure wc.style = cs_owndc | cs_hredraw | cs_vredraw; // Define class style wc.lpfnWndProc = h_wndproc; // window process callback function wc.hinstance = hinstance; // instance handle wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hicon = loading; // window icon wc.hcursor = loadingcursor (null, idc_arrow); // Window Cursor wc.hbrbackground = (Hbrush) (color_window 1) ; // Window Background Brush wc.lpszMenuname = NULL; // Window menu name wc.lpszclassname = "WebMain_class"; // Define class name if (RegisterClass (& WC)) // Register window Return True; // Success, return Real Return False;

// Failed, return false} BOOL H_INITWINDOW (// Window Initial Function Hinstance Hinstance, // Current Window Instance Handle INT NSHOWCMD // Display Window Way) {HWND CU_HWND; // Window Handle Cu_hWnd = CreateWindow ("WebMain_Class", / / Class name "A Blank Window", // Window Name WS_OVERLAPPEDWINDOW, // Window Style 0,0, // The upper left corner coordinate 500, 400, // window of the high null, // window of the parent handle or owner Handle null, // window menu handle hinstance, // instance handle null // is generally null, discussed in the establishment of the later multi-document window); // New window If (! Cu_hwnd) {// If create a window Failure return (false);} showwindow (cu_hwnd, nshowcmd); // Display window updateWindow (cu_hwnd); // Update window return (true);} LRESULT CALLBACK H_WNDPROC (// Window Processing Function HWND HWND, // Current Window Handle UINT UMSG, // Message Identifier WPARAM WPARAM, // Message of the first additional parameter LPARAM LPARAM // Message of the second additional parameter) {Switch (UMSG) {Case WM_DESTROY: postquitMessage (0); // End Program Break; Default: Return DefWindowProc (HWND, UMSG, WPARAM, LPARAM); // Call the default window process} return (0);} is good, the program code has been written, press Ctrl F5 to try. The results of the operation are as follows:

Ok, a blank window has been completed, you need to pay attention to the H_WndProc () function, this function cannot be called himself, must be called by the system. Its name can be arbitrary, but the parameters cannot be changed. This function is written in the lpfnwndproc parameter that is written to the window type structure when registering the window type.

I have to write a teaching article for the first time, it is inevitable that there is a mistake, please point out: hqywork@263.nethttp://websl.126.com

Not finished, to be renewed ...

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

New Post(0)