Chapter 1 of "VC ++ in-depth explanation": Internal Operating Mechanism of Windows programs

Source: Internet
Author: User

1.1api and SDK

API: Application Programming Interface
SDK: software development kit

1.2 window and handle

A Windows application must have at least one window called the main window.
An application window usually contains the title bar, menu bar, system menu, maximum minimization box, adjustable border, and scroll bar.
Windows are divided into customer and non-customer areas.
In Windows applications, windows are identified by Windows handles (hwnd. Handle (handle ).

1.3 messages and message queues

Windows programming is an event-driven programming model, mainly message-based.
In Windows, messages are represented by the MSG struct.
Typedf struct tagmsg {
Hwnd; // The member variable hwnd indicates the window to which the message belongs.
Uint message; // The message member variable specifies the message identifier.
Wparam; // used to specify the additional message of the message
Lparam; // used to specify the additional message of the message
DWORD time; // the time when the message is delivered to the message queue.
Point pt; // The current mouse position
} MSG;
After each windows program starts to run, the system creates a message queue for the program, which is used to store the window messages created by the program.
Messages in Windows programs can be divided into "incoming messages" and "no incoming messages ".
The system puts the incoming messages into the application's message queue, and then the application extracts and sends them.
Messages that do not enter the queue are directly sent to the window during the system call window.
Whether you enter the queue or not, the system calls the window function to process the message.

1.4winmain Function

The winmain function is an entry function of a Windows program. When the winmain function ends or returns a result, the Windows application ends.
Int winapi winmain (
Hinstance, // handle of the Instance currently running by the program, which is a numerical value
Hinstance hprevinstance, // handle of the previous instance of the current instance
Lpstr lpcmdline, // specifies the command line parameters passed to the application
Int ncmdshow // specify the display mode of the window
);
Window Creation: 1. Design window; 2. Registration window; 3. Creation window; 4. display window
Design window. Windows has defined the basic properties that a window should have for us. We just need to fill in some of the window to design it. Window is characterized by struct

Wndclass.
Typedef structure _ wndclass {
Uint style; // specifies the style of the window of this type.
Wndproc lpfnwndproc; // function pointer pointing to window Procedural Functions
Int cbclsextra; // windows manages a wndclass structure for each window class in the system
Int cbwndextra; // memory attached to the window
Handle hinstance; // specifies the instance handle of the program that contains the Window Process
Hicon; // specify the icon handle of the window class
Hcursor; // specify the cursor handle of the window class
Hbrush hbrbackground; // specifies the background paint brush handle of the window class
Lpctstr lpszmenuname; // a NULL terminated string that specifies the name of the menu resource
Lpctstr lpszclassname; // a NULL terminated string that specifies the name of the window class
} Wndclass;
The registration window. After wndclass is designed, you need to call the registerclass function for registration.
Atom registerclass (const wndclass * lpwndclass); // * lpwndclass is the pointer of the window class object.
After creating a window, design and register the window class, you can use the createwindow function to generate this type of window.
Hwnd createwindow (
Maid, // specify the name of the window class
Lptstr lpwindowname, // specify the window name
DWORD dwstyle, // specify the style of the created window
Int X, // X coordinate in the upper left corner of the window
Int y, // y coordinate in the upper left corner of the window
Int nwidth, // The window width
Int nheight, // The window height
Hwnd hwndparent, // specifies the parent window handle of the created window
Hmenu, // specify the handle of the window menu
Handle hinstance, // specify the handle of the application instance to which the window belongs
Lpvoid lpparam // The Data Pointer passed in as an additional parameter of the wm_create message.
);
Display window. Call the showwindow function to display the window.
Bool showwindow (
Hwnd, // The Window handle returned after the window is successfully created
Int ncmdshow // display status of the window
);
Update window. after calling the showwindow function, we call updatewindow to refresh the window.
Bool updatewindow (
Hwnd // The Window handle after successful creation
)
Message loop: after creating a window, display window, and update window, we need to write a message loop to continuously retrieve and respond to messages from the message queue.
To retrieve a message from a message queue, call the getmessage () function.
Bool getmessage (
Lpmsg, // point to a message (MSG) struct. The message information retrieved from the thread's message queue is saved in this struct object.
Hwnd, // specify the window in which messages are received
Uint wmsgfiltermin, // specifies the minimum value of the message to be obtained
Uintwmsgfiletermax // specify the maximum value of the message to be obtained. If both Min and Max are set to 0, all messages are received.
);
The getmessage function returns a non-zero value for all messages except wm_quit.
For wm_quit messages, this function returns zero.
If an error occurs,-1 is returned.

Windows application Message Processing Mechanism: (1) the operating system receives window messages from the application and delivers the messages to the Message Queue of the application. (2) Application calls in a message loop

The getmessage function extracts a message from the message queue. After a message is retrieved, the application can pre-process the message, for example, discard the response to a message or call

Translatemessage generates new messages. (3) The application calls dispatchmessage to send the message back to the operating system. (4) The system uses the lpfnwndproc Member of the wndclass struct.

The pointer of the stored window process function calls the Window Process to process the message.

Window Process function, used to process messages sent to the window. The main code of a Windows application is concentrated in window process functions.
Lresult callback windowproc (
Hwnd, // message window handle
Uint umsg, // message code
Wparam, // additional parameters of the message code
Lparam // additional parameters of the message code
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.