Windows program Internal operating mechanism

Source: Internet
Author: User
Tags bitwise operators

Windows operating mechanism within the program

First, API with the SDK

The Windows operating system provides a variety of functions to facilitate our development of Windows applications, which are interfaces provided by the Windows operating system to application programming (application Programming Interface), referred to as API functions . The API functions we call when we write a Windows program refer to System-provided functions, all of which are described in the Windows.h header file.

The full name of the SDK is software Development Kit, which is translated into a software development kit. The SDK is actually a collection of resources needed for development, including API libraries, help documents, manuals, assistive tools, and more.

Second, window and handle

Windows are a very important element in Windows applications, and a Windows application must have at least one window, called the main window. A window is a rectangular area on the screen that is the interface that Windows applications interact with users.

An application window typically includes a title bar, a menu bar, a system menu, a minimize box, a maximize box, an adjustable border, and some scroll bars.

Windows can be divided into customer and non-client areas . The customer area is part of the window, and the application typically displays text or draws graphics in the customer area. The title bar, menu bar, System menu, Minimize box, maximize box, adjustable border are collectively referred to as non-client areas of the window, which are managed by the Windows system, while the application primarily manages the appearance and operation of the client area.

In a Windows application, a window is identified by a window handle (HWND) . we need to manipulate a window to get the handle to the window first. in Windows programs, there are a variety of resources (windows, icons, cursors, and so on) that the system allocates memory for when creating these resources, and returns the identification number, the handle, that identifies those resources.

Third, message and Message Queuing

In Windows, not only the user program can call the system's API functions, in turn, the system will also invoke the user program, the call is done through Message Queuing

Windows program design is an event-driven mode of programming, mainly based on message. When the user interacts with the application, the operating system senses the event, wraps the event into a message, posts it to the application's message queue, and the application extracts the message from the message queue and responds. During this process, the operating system also "sends messages" to the application. The so-called "send Message" is actually a function in the operating system calling program that is responsible for processing the message, which is called the window procedure .

    1. News

In a Windows program, messages are defined by the MSG struct.

Windows defines the value of the message as a WM_XXX macro, and the WM is the abbreviation for Windows message, and XXX corresponds to the uppercase form of the English spelling of some kind of message. In a program, we usually use messages in the form of WM_XXX macros.

2. Message Queuing

After each Windows application starts executing, the system creates a message queue for the program that holds messages for the Windows that the program creates. Windows places the resulting message in the message queue, and the application continuously extracts the message from the message queue and responds with a message loop. This message mechanism is the mechanism by which Windows programs run.

3. Incoming news and no incoming team news

Messages in Windows programs can be categorized as " incoming message " and " do not enter the queue message . " Incoming messages are placed by the system into the application's message queue, which is then taken out and sent by the application. Messages that do not enter the queue are sent directly to the window when the system invokes the window procedure. The message is eventually processed by the System Call window procedure function, either in the queue or in the queue.

4. WinMain function

When the Windows operating system launches a program, it calls the program's WinMain function (which is actually called by the startup code inserted into the executable file). WinMain is a Windows the entry point function of the program is the same as the entry point function of the DOC program, and when the WinMain function ends or returns,theWindows application ends .

Four, Write a complete Win32 program

Steps:

Definition of the WinMain function

Create a window

Making a message loop

Writing window procedure functions

1. creation of Windows

To create a complete window, you need to follow these steps:

Design a window class Wndclass

Register window class registerclass

Create window CreateWindow

Display and update Windows ShowWindow UpdateWindow

A Windows program can contain multiple window procedure functions, and a window procedure is always associated with a particular window class, using the same window procedure based on the window created by the window class.

If the window is created successfully, the CreateWindow function returns the handle assigned to the window by the system, otherwise null is returned. Note that a window handle variable should be defined before the window is created to accept the value of the handle returned after the window was created .

The UpdateWindow function refreshes the window by sending a WM_PAINT message, andUpdateWindow sends the WM_PAINT message directly to the window procedure function for processing. Instead of putting it in a message queue, be aware of this .

2. Message Loops

After creating a window, displaying a window, updating a window, you need to write a message loop that continuously pulls the message out of the message queue and responds. To remove a message from the message queue, you need to call the GetMessage () function.

The parameter HWND specifies which window to receive the message. It is usually set to NULL, which is used to receive window messages belonging to all windows of the calling thread.

The parameter wmsgfiltermin specifies the minimum value of the message to get, typically set to 0.

The parameter Wmsgfiltermax specifies the maximum value of the message to get. If both Wmsgfiltermin and Wmsgfiltermax are set to 0, all messages are accepted.

The GetMessage function returns 0 only if the WM_QIUT message is received. At this point, the while statement determines the condition is false, the loop exits, the program can end the run. When a WM_QIUT message is not received, the Windows application passes through this while loop to ensure that the program is always in a running state

Five, Knowledge points

1. Steps to create a WIN32 application:

Write the WinMain function, which you can find and copy on MSDN

Design window class

Register window class

Show and update Windows

Writing a message loop

Writing window procedure functions

2. In Windows.h, class style identifiers beginning with Cs_ are defined as 16-bit class constants that have only one 1. Identifiers defined in this way are called "bit flags", and we can use bitwise operators to combine these styles.

3. The implementation mechanism of the callback function is:

(1) Define a callback function;

(2) The Party providing the function implementation registers the function pointer of the callback function with the caller at initialization time;

(3) When a particular event or condition occurs, the caller uses a function pointer to invoke the callback function to process the event.

4. For the Windows message handling mechanism, the Procedure for window procedure functions is called as follows:

(1) When the window class is designed, the address of the window procedure function is paid to the LPFNWNDPROC member variable;

(2) Call RegisterClass (&wndclass) Register window class, then the system has the address of the window procedure function we wrote;

(3) When the application receives a message from a window, the call DispatchMessage (&MSG) passes the message back to the system, and the system uses the function pointer that was obtained when the window class was previously registered, invoking the window procedure function to process the message.

5. During a function call, the stack is used. _stdcall and _cdecl are two different function calling conventions, which define the order in which the function parameters are stacked, whether the calling function or the called function pops up the stack, and the method that produces the function decorated name.

6. In VC + +, for customized menus, icons, cursors, dialog boxes and other resources are saved in the resource script (usually the extension. rc) file. The resource file itself is a text file format, and you can edit the resource directly using a text editor if you know the format of the resource file.

7. In VC + +, the resource is identified by an identifier (ID), and the same ID can identify multiple different resources. The ID of the resource is essentially an integer defined as a macro in "resource.h".

8. The Getstockobject function can be used not only to get a brush handle, but also to get a handle to a brush, font, and palette.

9. Note that the menu is not a window.

Message handling mechanisms for Windows applications:

(1) The operating system receives a window message from the application and posts the message to the application's message queue.

(2) The application calls the GetMessage function in the message loop to remove a single message from the message queue. After the message is taken out, the application can preprocess the message.

(3) The application calls DispatchMessage and passes the message back to the operating system. The message is represented by the MSG struct object, which contains a handle to the window that receives the message. Therefore, the DispatchMessage function is always properly passed.

(4) The system utilizes a pointer to a window procedure function stored by the LPFNWNDPROC member of the WNDCLASS struct to invoke the window procedure, processing the message (i.e. "The system sends a message to the application").

11. Note the dwstyle parameter that distinguishes the style member and the CreateWindow function in Wndclass, which specifies the style of the window class, which is created based on the window class, which is the style that specifies a specific window. The following are descriptions of several commonly used window types:

Ws_overlapped: Produces a cascade of Windows, a cascade of Windows with a title bar and a border;

Ws_caption: Create a window with a title bar

Ws_sysmenu: Create a window with a system menu on the title bar

Ws_thickframe: Creating a window with adjustable borders

The TranslateMessage function is used to convert a virtual key message to a character message. The character message is posted to the calling thread's message queue and is removed the next time the GetMessage function is called. Note that the TranslateMessage function does not modify the original message, it simply generates a new message and posts it to the message queue.

The DispatchMessage function dispatches a message to the window process, which is processed by the window procedure. DispatchMessage actually passes the message back to the operating system, which is called by the operating system to process the message.

14. Comparison of message acquisition and sending functions:

PeekMessage will return immediately to save the message in the message queue.

GetMessage when there is a message, the message is removed from the message queue.

PostMessage: Returns the message immediately after it is placed in the thread message queue where the specified window is located.

PostThreadMessage: Returns the message immediately after it is placed in the message queue of the specified thread. For thread messages, the HWND member of the MSG struct should be set to null.

SendMessage: The message is sent directly to the window process processing, the process is finished before returning.

The device context device description table.

Contains the structure of the device information, all graphics-related operations under the Windows platform are done using DC. GetDC (), ReleaseDC () is used in pairs to prevent memory leaks.

Wm_paint.

WM_PAINT messages are generated by Windows systems that inform the application to redraw their windows. Typically, the first time a Windows program produces a WM_PAINT message is emitted by the UpdateWindow function that is about to enter the message loop. Windows takes advantage of this opportunity to draw windows into the program's window.

WM_PAINT messages are generated in several cases:

Change in window size (depending on cs_hredraw,cs_vredraw settings)

Window from scratch

Restore after window minimized

is overwritten by another window and then displayed.

Using BeginPaint () and EndPaint () to get the DC, the same beginpaint () and EndPaint () two parint can only be called in a WM_PAINT message.

Windows program Internal operating mechanism

Related Article

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.