Chapter One internal running mechanism of Windows Program (4) WinMain function

Source: Internet
Author: User
Tags bitwise operators

The WinMain function is equivalent to the main function, which acts as an entry function for Windows programs. When WinMain ends or returns, the Windows program ends.

A Win32 application that creates a window and responds to the keyboard and mouse messages in the window, the program implementation steps are:

Definition of 1.WinMain function; 2. Create a window; 3. Loop the message; 4. Writing window procedures

Definition of the WinMain function:

int WINAPI WinMain (hinstance hinstance,    hinstance hprevinstance,    LPSTR lpcmdline,    int ncmdshow);

The first parameter represents the handle to the instance that the program is currently running, which is a numeric value. When a program runs in Windows, it uniquely identifies a running instance (only in a running instance of a program that has an instance handle). An application can have multiple instances, each running an instance, the system assigns a handle to the instance and passes the HINSTANCE parameter to the WinMain function.

The second parameter represents the handle to the previous instance of the current instance. In the Win32 environment, this value is null.

The third parameter is a null-terminated string that specifies the command-line arguments that are passed to the application. If you want to pass parameters to your application, you can set them in Project project properties, debugging, command parameters. Such as:

The fourth parameter is how the specified window is displayed, such as Maximize, minimize, hide, and so on. The value of this parameter is determined by the caller of the program, and the application usually does not need to handle this value.

Creation of Windows

Steps:

1. Design a window class; 2. Register window class; 3. create window; 4. display and Update window.

1. Before creating a window, you need to design a window of that type to specify the characteristics of the window. The characteristics of a window are defined by the WNDCLASS structure. The WNDCLASS structure is defined as follows:

struct {    UINT style;    WNDPROC Lpfnwndproc;     int Cbclsextra;     int Cbwndextra;    HINSTANCE hinstance;    Hicon Hicon;    Hcursor Hcursor;    Hbrush Hbrbackground;    LPCTSTR lpszmenuname;     *pwndclass;

The first variable specifies the style of the window. Common styles are: Cs_hredrow (redraws the entire window when the width changes horizontally) cs_vredrow (redraws the window when height changes vertically) cs_noclose (disables the close command in the System menu, the window does not have a close button) Cs_ Dblclks (when the user double-clicks the mouse, sends a mouse double-click Message to the window). We can use bitwise operators to combine these styles.

The second variable is a function pointer to the window procedure function, and the window procedure function is a callback function. The callback function is not called directly by the implementing party of the function, but is called by the other party when a particular event or condition occurs, and is used to respond to the event.

  The third variable is Cbclsextra. (This parameter does not understand what is meant, follow the book to copy down, look again, think about it again). Windows manages a WNDCLASS structure for each window class in the system. When the application registers a window class, it allows the Windows system to allocate and append a certain amount of additional memory space to the WNDCLASS structure, which is called the class-attached memory , which is shared by all windows that belong to such a window class. Class additional memory space is used to store additional information for the class. The Windows system initializes this portion of memory to 0. Generally we set this parameter to 0.

A fourth variable Cbwndextra. Windows manages an internal data structure for each window, and when registering a window class, the application is able to specify an additional memory space of a certain number of bytes, called the window attached memory . When you create such a window, the structure of the Windows system's nine-tailed window allocates and appends the specified number of Windows to additional space, which the application can use to store window-specific data. The Windows system initializes this portion of memory to 0. If the application is registered with the WNDCLASS Structure dialog box (created with the class pseudo-directive in the resource file), the member must be set to Dlgwindowextra. Generally we set this parameter to 0.

The fifth variable, HINSTANCE, specifies an instance handle that contains the window procedure.

The sixth member variable hicon the icon handle for the specified window class. This member variable must be a handle to an icon resource, and if the member is NULL, then the system will provide a default icon. When assigning a value to a Hicon variable, you can use the LoadIcon function to load an icon resource and return the handle assigned to the icon by the system. The prototype declaration for this function is: Hicon LoadIcon (hinstance hinstance,lpctstr lpiconname)

The seventh variable hcursor specifies the cursor handle of the window class. This member variable must be a cursor handle, and if the member variable is NULL, the application must explicitly set the cursor's packing whenever the mouse enters the application window. When assigning a value to Hcursor, you can use the call loadcursor function to load a cursor resource and return the handle assigned to the cursor by the system. The function prototype is declared as follows: Hcurosr loadcursor (hinstance hinstance, LPCTSTR lpcursorname);

The eighth variable hbrbackground the background brush handle for the specified window class. When the window is redrawn, the system uses the brush specified here to erase the background of the window. You can call the Getstockobject function to get the system's standard brush. The Getstockobject function can be used not only to get a handle to a brush, but also to get a handle to a brush, font, and palette. Because the Getstockobject function can return a handle to a variety of resource objects, it is not possible to determine which resource object's handle is returned until the function is actually called, so its return value type is defined as Hgdiobj, which requires a type conversion when actually used. For example, to specify a handle to a black brush for hbrbackground members, you can wndclass.hbrbackground = (hbrush) getstockobject (Black_brush);

The nineth member variable, lpszmenuname, is a null-terminated string that specifies the name of the menu resource . If you use the ID number of the menu resource, you need to use the Makeintresource macro to convert it. If the Lpszmenuname member is set to NULL, Windows created based on this window will not have a default menu. Note that the menu is not a window!

The tenth variable, lpszclassname, is a null-terminated string that specifies the name of the window class.

2. Registration window class

After you have finished designing the window class (WNDCLASS), you need to call the RegisterClass function to register it, and then you can create a window of that type after the registration succeeds. The prototype of the registration function is declared as follows:

ATOM registerclass (CONST wndclass *lpwndclass);

The function has only one parameter, which is a pointer to the window class object that was designed in the previous step.

3. Create a window-Step 3

After you have designed the window class and successfully registered it, you can use the CreateWindow function to generate this type of window. The prototype of the CreateWindow function is declared as follows:

HWND CreateWindow (  
LPCTSTR Lpclassname, LPCTSTR lpwindowname, DWORD dwstyle, int x, int y, int nwidth, int nheight, HWND hwndparent, HMENU HMENU, hinstance hinstance, lpvoid lpparam);

The Lpclassname parameter specifies the name of the window class, which is the name of the lpszClassName member pointer in a window class that is designed for wndclass in step 1.

The Lpwindowname parameter specifies the name of the window. If the window style has a title bar, the window name specified here will be displayed on the title bar.

The Dwstayle parameter specifies the style of the created window. Note that the Dystyle parameter of the style member and the CreateWindow function in wndclass refers to the style of the window class, and the window that is created based on that window class has such a style, which refers to the style of a specific window.

The parameter x,y,nwidth,nheight of the CreateWindow function specifies the x, y coordinates of the upper-left corner of the window, the width of the window, and height. If the parameter x is set to Cw_usedefault, the system is the upper-left coordinate of the window and ignores the y parameter, and if the parameter nwidth is set to Cw_usedefault, the system selects the default width and height for the window, and the parameter nheight is ignored.

The parameter hwndparent specifies the parent window handle of the window being created.

The Hmenu parameter specifies a handle to the Window menu.

The HINSTANCE parameter specifies the instance handle of the application to which the window belongs.

Parameter Lpparam: LPARAM The incoming data pointer as an additional parameter to the WM_CREATE message. When creating a customer window for a multi-document interface, the Lpparam must point to the CLIENTCREATESTRUCT structure. Multiple Windows sets this parameter to NULL.

If the creation succeeds, the CreateWindow function returns the handle assigned to the window by the system, otherwise returns NULL. Note that you should define a window handle variable to receive the handle value returned after the window is created before creating the window.

The mechanism of the callback function implementation:

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 time or condition occurs, the caller uses a function pointer to invoke the function to process the event.

  

Chapter One internal running mechanism of Windows Program (4) WinMain function

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.