Windows program running principle subtotal using VC ++

Source: Internet
Author: User

How Windows programs run

Overview
Entry function ---- Window ----- message loop ---- callback function

1. program entry function winmain
Int winapi winmain (
Hinstance;
Hinstance hprevinstance;
Lpstr lpcmdline; // Run Command
Int ncmdshow; // show state
);

The winmain function is called by the operating system, and all parameters in it are assigned values by the operating system.
When the operating system starts an application, it is assigned an instance number and put it in hinstance. If there is a parameter, it is placed in the lpcmdline, and the application status are all implemented by the operating system calling winmain.

2. Window
1) Design a window class;
2) register a window;
3) create a window;
4) display and update windows;

1) wndclass;
2) registerclass (const wndclass * lpwndclass );
3) hwnd createwindow (
Maid,
Lptstr lpwindowname,
DWORD dwstyle,
Int X,
Int y,
Int nwidth,
Int nheight,
Hwnd hwndparent,
Hmenu,
Hinstance,
Lpvoid lpparam );
4) bool showwindow (hwnd, int ncmdshow );
Updatewindow (hwnd );

Typedef struct {
Uint style;
Wndproc lpfnwndproc;
Int cbclsextra; // additional data of the window class
Int cbwndextra; // extra window data
Hinstance; // the instance of current application
Hicon;
Hcursor;
Hbrush hbrbackground;
Lpctstr lpszmenuname;
Lpctstr lpszclassname;
} Wndclass, * pwndclass;

-- Lpfnwndproc process function (callback function)
Code for event processing;
Called by the operating system;
Place the Code Compiled by the user;

3. process functions (callback functions)
Lresult callback windowproc (
Hwnd,
Uint umsg,
Wparam,
Lparam
);

What is the basis for the operating system to call corresponding process functions for processing specific events?
The function specified by the lpfnwndproc member in the type of the window that receives the message is called. Each window has its own exclusive callback function.

-- Hinstance current application instance number
Assigned by the hinstance parameter in the winmain Function

4. Messages
The operating system packs each event into a message struct MSG and passes it to the application.
MSG definition:
Typedef struct tagmsg {
Hwnd; // handle to window -- 1
Uint message; // -- 2
Wparam;
Lparam;
DWORD time;
Point pt;
}

-- 1 handle
Resource ID
Icon, cursor, window, instance

-- 2 macro
Used to replace numeric values, starting with WM

5. Message Loop
The application uses the getmessage function to call messages placed in the message queue by the operating system.
Bool getmessage (
Lpmsg,
Hwnd,
Uint wmsgfiltermin,
Uint wmsgfiltermax
);

Translatemessage () converts the obtained wm_keydown and wm_keyup messages into a wm_char message.
Put it in the message queue.
Dispatchmessage () transmits the received message to the operating system, and then the operating system transmits the first four parameters in the MSG struct
Hand it to the callback function and call this callback function for processing.

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.