2 2 1 8 Exercise Windows Program Model

Mondo Technology Updated on 2024-01-29

/*--

008 Programming Guru Win32 API daily practice.

The 8th example is hellowinC: Windows program model.

c) www.bcdaren.com coding whiz.

#include

lresult callback wndproc (hwnd, uint, wparam, lparam) ;Window Procedure-Message function.

int winapi winmain (hinstance hinstance, hinstance hprevinstance,pstr szcmdline, int icmdshow) pstr non-wide string pointer char*

static tchar szappname = text ("hellowin") ;The name of the window class.

hwnd hwnd ;Window handle.

msg msg ;message struct variable -f1

wndclass wndclass ;Window class structure variables.

Step 1: Initialize the window class.

wndclass.style = cs_hredraw | cs_vredraw ;Window class style - horizontal and vertical identifiers - been.

All windows of the class are redrawn when changed.

wndclass.lpfnwndproc = wndproc ;Window handlers --- function pointers.

wndclass.cbclsextra = 0 ;The window is extended, and cb represents the number of bytes.

wndclass.cbwndextra = 0 ;Window instance extensions.

wndclass.hinstance = hinstance ;Process instance handle.

wndclass.hicon = loadicon (null, idi_application) ;The minimized icon for the window.

wndclass.hcursor = loadcursor (null, idc_arrow) ;Window mouse cursor.

wndclass.hbrbackground = (hbrush) getstockobject (white_brush);Background color.

wndclass.lpszmenuname = null ;Window menu.

wndclass.lpszclassname = szappname ;The name of the window class.

Step 2: Register a window class.

if (!registerclass (&wndclass))

messagebox(null, text("Registration failed!"),szappname, mb_iconerror);

return 0;

Step 3: Create a window.

hwnd = createwindow( szappname, the name of the window class.

text ("Create a window"), window title.

ws overlappedwindow, window style.

cw usedefault, the initial horizontal position of the window.

cw usedefault, the initial vertical position of the window.

cw usedefault, the width of the window in device units

cw usedefault, the height of the window in device units

null, the handle of the parent window.

null, window menu handle.

hinstance, program instance handle.

null) ;Create parameters.

Step 4Display and update the window.

showwindow (hwnd, icmdshow) ;Display the window.

updatewindow (hwnd) ;Update window.

Step 5: Message looping.

while (getmessage (&msg, null, 0, 0)) message queue, message is.

wm quit returns 0 to exit the message loop.

Return the msg structure to Windows and convert the virtual key message to a character message.

translatemessage(&msg);

Return the msg structure to Windows again and distribute a message to the window program.

dispatchmessage(&msg);

msg.wparam comes from a message indicating an exit and returns this value to the system, thus exiting.

return msg.wparam ;

Window process. lresult callback wndproc (hwnd hwnd, uint message, wparam wparam, lparam param)

hdc hdc ;External environment handle (device context handle).

paintstruct ps ;The client area used to draw the window owned by the application.

rect rect ;The rectangular structure is defined by its coordinates in the upper left and lower right corners.

switch (message)

case wm create: Creates a window message.

return 0 ;

case wm paint: Redraw window message.

hdc = beginpaint (hwnd, &ps);Start Drawing (Get Device Environment) - In this case, the display.

getclientrect (hwnd, &rect) ;This function gets the size of the window client area.

drawtext (hdc, text ("hello, windows!"), 1, &rect, -1 for a zero string.

dt_singleline | dt_center | dt_vcenter) ;Draw formatting: in specified.

The rectangular text is displayed on a single line centered horizontally and vertically.

endpaint (hwnd, &ps) ;Marks the end of the drawing in the specified window.

return 0 ;

case wm destroy: Exits the message loop.

postquitmessage (0) ;Indicates to the system that the thread has requested termination (exit).

return 0 ;

The Invoke Operating System Default Window procedure provides default processing for any window messages that are not handled by the application.

return defwindowproc (hwnd, message, wparam, lparam) ;

Results: The Note icon, title, and system menu are all located in the title bar, with areas below the title bar being the Clients area.

Figure 2-1 Creating a window.

In hellowinThe function of the C program is to draw a custom window and output the text string "hello, Windows!" in the window client area”。

The program is divided into two parts: the main program WinMain and the window procedure WndProc.

Main program

winmain function.

The winmain function is the entry function of the Windows window program, while the entry function of the console program is the main function. When creating a project, the desktop application is selected, so the Project Properties -> Linker -> System -> Subsystem option defaults to Window, as shown in the image. Of course, if this option is set to "Not set", the linker will judge that it is a window program based on the function name "winmain".

2-2 vs Window Subsystem Options.

Winmain function prototype:

int winapi winmain (hinstance hinstance, hinstance hprevinstance,pstr szcmdline,int icmdshow);

It can also be: int winapi winmain( hinstance hinstance, hinstance hprevinstance,lpstr lpcmdline, int ncmdshow);

The return value of the function is of type int. A return value of 0 indicates a normal end, and a non-0 value indicates an abnormal end.

The function call convention is: winapi, that is, the stdcall call convention.

Parameter 1 hinstance hinstance: is the handle of the current process, which is given by the operating system when the process is created and initialized.

Parameter 2 hinstance hprevinstance: the previous process handle, which is a parameter left over from the win16 system. Win16 is a 16-bit single-task real-mode operating system, this parameter is used to determine whether the previous process has ended, and only when the previous process ends, the current process can be created.

Argument 3 pstr szcmdline: is a long pointer to a null-terminated string that contains command-line arguments.

Parameter 4 int icmdshow: An integer value that determines how the main window is displayed. It can be something like this:

SW Hide: Hides the window, and activates another window.

SW maximize: Maximize the window, and activate it.

SW minimize: minimizes the window and activates the next top-level window.

SW RESTORE: Activates and displays the window. If the window is minimized or maximized, Windows reverts to its original size and position.

SW show: Displays and activates the window at its current size and position.

SW showdefault: Sets the state of the window for the first display based on the WSHOWWewWindowField of the startupinfo struct passed to the createProcess function when the application is started.

SW showmaximized: Maximizes the window and activates it.

SW showminimized: Minimize the window, and activate it.

SW showminnoactive: Minimize the window, but don't activate it.

SW showna: Displays the window in its current state, but does not activate it.

SW shownoactivate: Displays the window at its nearest size and position, but does not activate it.

SW shownormal: Similar to SW restore, it is displayed and activated based on the default position and size of the window.

The default is the normal size display window.

There are 4 variables that need to be defined in the winmain function:

static tchar szappname = text ("hellowin") ;Define a window class name, which is usually also the name of the process.

hwnd hwnd ;Define a window handle through which you can manipulate the window.

msg msg ;Define a message structure variable.

wndclass wndclass ;Define a window class structure variable. Window classes have already been explained in the previous section, so I won't repeat them.

If we want to view the relevant help information, we can select it and press the F1 key to check the MSDN related information. You can also select it and click the right mouse button to quickly grasp the definition or go to the definition, vs operation is very convenient.

The five steps of the main program to create a window:

Step 1: Initialize the window class. If you want to create a custom window, you must specify the information about the window and pass it to the operating system. To put it simply, it is to initialize the fields of the window class struct. If you don't need or don't have a field, you can set it to null. However, the fields of window style, window procedure, and window class name are mandatory.

Step 2: Register a window class. After we initialize a new window class, we need to register the window class with the WinodWS system and call the API function RegisterClass.

Step 3: Call the createwindow function to create a window.

Step 4: Display and update the window.

showwindow (hwnd, icmdshow) ;Display the window.

updatewindow (hwnd) ;Update window.

Step 5: Loop the message.

If the message is wm quit, it returns 0 and exits the message loop.

while (getmessage (&msg, null, 0, 0))

Return the msg structure to Windows and convert the virtual key message to a character message.

translatemessage(&msg);

Return the msg structure to Windows again and distribute a message to the window program.

dispatchmessage(&msg);

The message loop is a while loop structure, which calls the getmessage function to get the message from the window message queue, and returns 0 if the message is wm quit to exit the message loop. Otherwise, the translatemessage function and dispatchmessage function handle it.

The five steps of the main program to create a window are mandatory, fixed, and can be used as templates for Windows programs. In most cases, just copy it and use it is fine. If you want to add sub-windows or handle control messages or menu shortcut messages, you can change it a little. The basics of Windows programming are much simpler than they seem.

Window process.

The window process is responsible for handling the various messages sent to the window by the Windows system.

lresult callback wndproc (hwnd hwnd, uint message,wparam wparam, lparam lparam);

We will find that the four parameters of the window process are the first four fields of the msg message structure. This is a message parameter passed by the Windows operating system.

Parameter hwnd: the handle of the current window. A unique identifier used to identify a window or application subwindow.

Message: describes the type of message that the window needs to process, such as wm paint (the window needs to be redrawn), wm destroy (the window is about to be destroyed), etc.

Parameter wparam: is a message-specific additional information. The content will vary depending on the specific message type.

Parameter lparam: is also a message-specific additional information. The content will vary depending on the specific message type.

The return value lresult is the information returned after the message has been processed, and the return information varies according to the message type.

Whenever an event occurs (such as mouse clicks, keyboard keystrokes, timer events, etc.), Windows will send a message to the corresponding window and process the message through the WNDPROC function.

For most messages, once wndproc has processed them, it should return 0. For some messages, such as WM Create and WM Paint, the returned value may contain certain information. All of this information is explained in detail in the specific instructions for each message.

If a message is not processed by wndproc, then you should call the defwindowproc function of the default window procedure of the Windows system and let the system handle it in the default way. The vast majority of messages are handled by the defwindowproc function.

The call convention of WNDPROC is callback, which is the same as WinAPI, and the same stdcall call convention. The reason why callback is used, as the name suggests, wndproc is a ** function, that is, the Windows system calls wndproc to process various messages.

The wndproc window process mainly contains a switch structure, which is processed separately according to the different message IDs, and after processing, it directly returns 0;Return. Other messages that are not processed are left to defwindowproc to handle as they are by default.

Next, we will break down this template program and implement the window creation process step by step, so that we can analyze the window implementation process.

Related Pages