Windows programming foundation - form articles (this article may help you learn Windows programming)

zhaozj2021-02-17  116

Windows programming foundation - form articles

Now Windows has become the most widely used operating system in the world, which makes it important to learn Windows programming. Like learning other programming languages, we also start learning Windows programming from a simplest example. This example will create a Windows Form and display the "Hello" string in the middle of the form (see Figure 1). 1. The beginning and end of the program

Like the main () function in the C language, the Windows program starts and ends from the WinMain () function. The WinMain () function is defined in WinBase.H, which is as follows:

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

Where the first parameter (Hinstance Hinstance) is an instance handle of the program, this instance handle is the unique identifier of the program. The second parameter (Hinstance Hprevinstance) is run with multiple program instances, but this parameter is always NULL because Windows9x and NT (including higher versions) are 32-bit operating systems. The third parameter (LPSTR LPCMDLINE) is a pointer to string to save the command line parameters when running the program. The fourth parameter (int NShowCMD) is used to specify how the program starts to display. 2. Define and register window classes

To create a window in Windows First: Define the window class and register this window class. Defining window classes are done by filling the WNDCLASS structure, this structure is defined in Winuser.h, which is as follows:

typedef struct tagWNDCLASSA {UNIT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCSTR lpszMenuName; LPCSTR lpszClassName;} WNDCLASSA, * PWNDCLASSA, NEAR * NPWNDCLASSA, FAR * LPWNDCLASSA;

Let's first define a WndClass structure: Wndclass WndClass;

Then populate the domain of the WNDCLASS structure:

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW; File: / / Define window type

WNDCLASS.LPFNWNDPROC = WNDPROC; file: // Defines the message processing function of this window Wndclass.cbclsextra = 0; file: // Reserved space, 0 is not reserved wNDClass.cbWndextra = 0; file: // reserved space, 0 is not reserved

WNDCLASS.HINSTANCE = Hinstance; File: // Example handle (the first parameter of the winmain function)

Wndclass.hicon = loadicon (null, idi_application); file: // Set the icon of the window

WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow); file: // Set the mouse cursor shape

WNDCLASS.HBRBACKGROUND = getStockObiect (white_brush); file: // Set window background is white

WNDCLASS.LPSZMENUNAME = NULL; File: // Set the window menu, because this routine does not use the menu, set to NULL

WNDCLASS.LPSZCLASSNAME = myAppname; file: // Defines the name of the window class

Finally, use the registerclass () function registration window class:

IF (! registerclass (& wndclass)) Return 0;

3. Create and display window

We completed the definition window and register this window class, now you have to create this window and display it. Creating a window To complete, the specific method is as follows:

HWND = CREATEWINDOW (MyAppname, File: // Name "Hello", file: // Window Title WS_OVERLAPPEDWINDOW, FILE: // Window Style CW_USEDEFAULT, FILE: // Window C Cw_USEDEFAULT, FILE: // Window Coordinate Y CW_USEDEFAULT, FILE: / / Window Width CW_USEDEFAULT, FILE: / / Window High Null, File: // Parent Window Handle Null, File: // Window Menu Hill Hinstance, File: // Handle NULL FILE: // Parameter Pointer);

After creating a completion window, Windows does not display it on the display, just saving all information for this window in memory, to display this window, you need to use the showwindow (hwnd, icmdshow) function. This function has two parameters, the first parameter is the window handle returned when the CreateWindow () function creates a window, and the second parameter is the third parameter of the WinMain () function. In order to ensure that the window can be displayed normally to the display we need to call the UpdateWindow (HWND) function.

4. The message loop Windows will create a message queue for each currently running program to complete the user and program interaction. In order to allow the program to handle this message queue, we should use a message loop from the message queue from the message queue: While (GetMessage (& MSG, NULL, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);} message loop from The getMessage () function begins, first removes a message from the message queue, then pass the MSG structure to Windows through the TranslateMessage (& MSG) function, and finally use the DispatchMessage (& MSG) function to pass the MSG structure back to Windows, and then Windows will This message is sent to the corresponding window process.

5. Window Process Functions To process Windows sent messages, you need to define a window process function. When populating the WNDCLASS structure, we define the message processing function of the window (WndClass.lpfnWndProc = WndProc) WndProc, which is used to process window process functions for the window message. Its definition is as follows:

Lresult Callback WndProc (HWND HWND, Unit Message, WPARAM WPARAM, LPARAM LPARAM)

These four parameters and the top four domains of the MSG structure are the same. The first parameter (hwnd hwnd) is the window handle of the message, which is the same as the return value of the CREATEWINDOW () function. The second parameter is used to identify the number of the message. The third parameter and the fourth parameter are 32-bit message parameters to provide more information for messages.

6. Processing window message

After defining the window procedure function, we can process the window message sent by Windows in the window process function. Generally use Switch and CASE statements to determine what message received and processes this message accordingly.

Switch (Message) {Case WM_CREATE: ... ... RETURN 0; Case WM_Paint: ... Return 0; Return DEFWINDOWPROC (HWND, Message, WPARAM, LPARAM); one of the "WM" The syner is a message parameter, defined in the Winuser.h file. WM_CREATE is the first message received by the window process. When the CREATEWINDOW () function is called, the window process function receives this message when the window is created. The second message WM_Paint is a very important message. He is responsible for painting something on the form, we have to display the "Hello" string through it. When Windows will send a WM_PAINT message when Windows is established in the first time and needs to refresh the form.

7. Compile connection

To compile the above program we need a Win32 compiler, we choose VC6.0. Start VC6.0, run the menu "File-> New ...", select the "Projects" tab in the "New" dialog, and select "Win32 Application", "Projects Name:" The name of the program " Hello "(see Figure 2), click the" OK "button. Click the "Finish" button in the wizard. Then run the menu "File-> New ...", select the "Files" tab in the "New" dialog, and select "C Source File", select "Add to Project:" on the right, in "file" Enter the file name "Hellofile" (see Figure 3), click the "OK" button. Enter the program code, press CTRL F5 to compile the running program.

8. All source procedures and comments

#include

File: // Message Processing Function LRESULT CALLBACK WINDOWFUNC (HWND, UINT, WPARAM, LPARAM);

Char szwinname [] = "Hello"; // Program class name

File: // Program main function int WinAPI WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NSHOWCMD) {hwnd hwnd; msg; WNDCLASS WNDCLASS;

File: // Filled WNDCLASS.style = cs_hredraw | cs_vredraw; file: // Define window type WNDCLASS.LPFNWNDPROC = WindowFunc; file: // Defines the message processing function of this window WNDClass.cbclsextra = 0; file: // Reserved space, 0 is not reserved wndclass.cbwndextra = 0; file: // Reserved space, 0 is not reserved WNDCLASS.HINSTANCE = Hinstance; File: // The first parameter of the winmain function ) wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); file: // set the window icon wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); file: // set the mouse cursor shape wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) File: // Setting the window background is white wndclass.lpszMenuname = null; file: // Set window menu, Since this routine does not use the menu, set to null wndclass.lpszclassName = szwinname; file: // Define the name of the window class File: // Register window IF (! registerclass (& wndclass)) Return 1; file: // Setting up window hWnd = CREATEWINDOW (SzwinName, // window name "Hello", // window Title Ws_overlappedWindow, // Window CW_USEDEFAULT, / / ​​Window Coordinate X CW_USEDEFAULT, / / ​​Window Coordinate Y CW_USEDEFAULT, // Window Width CW_USEDEFAULT, // Window High Null, // Parent Window Handle Null, // Directory H INSTANCE, // The handle null file: // parameter pointer);

File: // Display Window ShowWindow (HWND, NSHOWCMD); UpdateWindow (HWND); File: // Settings Window Message Cyclic While (GetMessage (& MSG, NULL, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG); Return msg.wparam;}

File: // Window Message Cycle Function LRESULT CALLBACK WINDOWFUNC (HWND HWND, UINT Message, WPARAM WPARAM, LPARAM LPARAM) {HDC HDC; PAINTSTRUCT PS; Rect Rect;

Switch (Message) {case wm_paint: file: // String "Hello" HDC = BeginPaint (HWND, & PS); getClientRect (hwnd, & review); DrawText (HDC, "Hello", - 1, & RECT (HLLO ", - 1, & Re , DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint (hwnd, & ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0;} return DefWindowProc (hwnd, message, wParam, lParam);} Note: this procedure Windows98 VC6.0 is lowered.

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

New Post(0)