Win32 application of SDK design principles

  • 2020-04-02 02:34:36
  • OfStack

Generally speaking, the so-called Win32 Application program development, is in the C language level, the direct use of Win32 API (Application Programming Interface: the system is open to the programmer to use the Interface. To develop Windows applications or system programs. Although there are not many people developing applications directly with Win32 API, but in-depth understanding of the principles of Windows system programming is still the only way to become a Windows developer.

The so-called Win32 is actually an API specification that corresponds to the UNIX system programming interface standard POSIX. The following is the basic idea of direct WIN32 SDK programming or a framework analysis:

A WINDOWS program is divided into two parts: program code and UI(User Interface) resources Finally, the two parts are compiled into a complete EXE file by the RC compiler. UI resources refer to function menus, dialog appearance, program ICONS, cursor shapes, and so on. The actual content (binary code) of these UI resources is generated by a variety of tools and exists with a variety of extensions, such as.ico.bmp.cur, etc. Programmers must describe them in a so-called resource description file (.rc). The RC compiler (rc.exe) reads the description of the RC file and centralizes all the UI resource files into a.res file, which is then combined with the program code to create a complete Windows executable.

Windows programs will call a number of functions, which can be divided into C Runtimes and Windows API , and the API is provided by the operating system itself, including gdi32.lib, user32.lib, kernel32.lib, comdlg32.lib, th32.lib and so on. The first three are the import function libraries corresponding to the three major modules of Windows.

Windows' messaging mechanism is "message-based, event-driven." , i.e., Windows programs are driven by external events In other words, the program keeps waiting for messages, external events enter the system in the form of messages and put into the corresponding queue, and then the program calls Getmessage API to get the corresponding messages and make corresponding processing. Windows are used to receive and process messages. Each window has a function to process the message. The programmer must design the window function (Windows procedure).

Win32 application (SDK) implementation is mainly divided into the following steps:

One, WinMain function
The main() function is the entry point for C programs, and the WinMain function is the entry point for Windows programs,

MSG structure
Defines a MSG structure, which is a data format in Windows:

Registration window
RegisterClass() does the job of registering a window class, setting the properties of the window, including borders, colors, titles, positions, and so on.

Create a window
The CreateWindow function can be used to create parent, pop-up, and child Windows, and when creating a window it determines the window class, window title, window style, size, and initialization location.

Five, display window
After creating the window, you need to use ShowWindow to display the window.

Refresh the window
Call the UpdateWindow function to refresh the window.

Message cycles
After the initialization is done, WinMain enters what is called a message loop, USES a while loop, keeps getting message, transforms the message using TranslateMessage, and passes the message to the window function using DispatchMessage.

8. Get information
The GetMessage function gets the message from the message queue, and if there is no message in the message queue, this function waits for the message.

Transform messages
TranslateMessage converts a particular message to a different message, such as a message queue with both WM_KEYDOWN and WM_KEYUP messages, which means that the user has pressed some keys on the keyboard at the same time, to a WM_CHAR message.

Send the message to the callback function
DispatchMessage sends the message to the window function of the window with the help of the USER module. However, DispatchMessage does not specify the function name, but it can send the message to the window. This is because when the message occurs, the OS has marked the window according to the current state, and the window class to which the window belongs is also clear.

The WinMain function returns the value


return msg.wParam; //Returns the value of wParam in the struct variable MSG.

Windows proc window function
Life pivot in Windows: window function WindowProc. WindowProc is a callback function that often USES the switch/case approach to determine the type of message to determine the processing


Related articles: