Sun Xin VC ++ lecture notes-(1) Internal Operating Mechanism of Windows programs

Source: Internet
Author: User

1. Windows programming is an event-driven Program Design Based on messages. When a user needs to complete a certain function, he needs to call some OS support, and then the OS packs the user's needs into messages and inputs them into the message queue, finally, the application removes the message from the message queue and responds to it.
2. Message structure:
Typedef struct tagmsg {// msg
Hwnd; // handle of the window for receiving messages. Window associated.
Uint message; // Message ID. What is the message itself.
Wparam; // additional message information. Depends on the message itself.
Lparam;
DWORD time; // message delivery time.
Point pt; // The Position of the cursor on the screen during message delivery.
} MSG;

3. Message Queue:
Each application OS creates a message queue for it, and the message queue is a buffer zone of first-in-first-out, where each element is a message, the OS puts each generated message into the message queue in sequence. The application always removes the first message in the current message queue. After the application removes the message, it will know the user's operations and program status, then it is processed as a message response, and the message response is encoded.

4. In addition to good C-based programming, you must master the following two aspects:
1. Message itself. Different messages represent user operations and application statuses.
2. for a specific message, the OS must execute a specific function to respond to the message.

5. Window program entry:
Int winapi winmain (
Hinstance, // handle of the current case.
Hinstance hprevinstance, // handle of the previous case.
Lpstr lpcmdline, // command line pointer
Int ncmdshow // The status displayed in the window
);
Note: The winmain function is a Windows program entry point function called by the OS. When the OS starts the application, the parameters of the winmain function are transmitted by the OS.

6. To create a complete window, follow these four steps:
1. Design a window class, such as wndclass wndcls;
2. register the window class; for example, registerclass (& wndcls );
3. Create a window, for example, createwindow () and createmediawex ();
4. Display and update Windows. For example, showwindow () and updatewindow ();

Note: You must create a window based on the registered window class.

7. Windows window classes:
Typedef struct _ wndclass {
Uint style; // Window Type
Wndproc lpfnwndproc; // window process function pointer (callback function)
Int cbclsextra; // additional bytes of the window class, which are shared by the window class. Usually 0.
Int cbwndextra; // additional bytes of the window. It is usually set to 0.
Handle hinstance; // handle of the current application case.
Hicon; // loadicon ();
Hcursor; // The cursor handle loadcursor ();
Hbrush hbrbackground; // hbrush getstockobject ();
Lpctstr lpszmenuname; // menu name
Lpctstr lpszclassname; // Class Name
} Wndclass;

8. window class registration:
Atom registerclass (
Const wndclass * lpwndclass // address of Structure with Class
// Data
);

9. Create window:
Hwnd createwindow (
Maid, // pointer to registered Class Name
Lptstr lpwindowname, // pointer to window name
DWORD dwstyle, // window style
Int X, // horizontal position of window
Int y, // vertical position of window
Int nwidth, // window width
Int nheight, // window height
Hwnd hwndparent, // handle to parent or owner window
Hmenu, // handle to menu or child-window identifier
Handle hinstance, // handle to application instance
Lpvoid lpparam // pointer to window-creation data
);

10. Display and update window:
Bool showwindow (
Hwnd, // handle to window
Int ncmdshow // show state of window
);
Bool updatewindow (
Hwnd // handle of window
);

11. Message loop:
MSG;
While (getmessage (& MSG,...) // retrieves a message from the Message Queue
{
Translatemessage (& MSG); // converts messages (such as keyboard messages)
Dispatchmessage (& MSG); // callback function for dispatching messages to the window (OS calls the window callback function for processing ).
}

Where:
// ** The getmessage function retrieves a message from the calling thread's message queue and places it in the specified structure.
// ** If the function retrieves a message other than wm_quit, the return value is nonzero. if the function retrieves the wm_quit message, the return value is zero. if there is an error, the return value is-1.

Bool getmessage (
Lpmsg, // address of Structure with message
Hwnd, // handle of window
Uint wmsgfiltermin, // first message
Uint wmsgfiltermax // last message
);

// The translatemessage function translates virtual-key messages into character messages. the character messages are posted to the calling thread's message queue, to be read the next time the thread callthe getmessage or peekmessage function.
Bool translatemessage (
Const MSG * lpmsg // address of Structure with message
);

// The dispatchmessage function dispatches a message to a window procedure.
Long dispatchmessage (
Const MSG * lpmsg // pointer to structure with message
);

12. Window Process function (callback function) prototype:
The windowproc function is an application-defined function that processes messages sent to a window. the wndproc type defines a pointer to this callback function. windowproc is a placeholder (placeholder) for the application-defined function name.

Lresult callback windowproc (// here windowproc is a code name.
Hwnd, // handle to window
Uint umsg, // message identifier
Wparam, // first Message Parameter
Lparam // second Message Parameter
);

Note: Two function call conventions (_ stdcall and _ cdecl ):
# Define callback _ stdcall
// _ Stdcall standard call reservation, which is a Pascal call Convention. For example, Delphi uses a standard call convention.
# Define winapiv _ cdecl
// _ Cdecl is a C-language call convention.

Major differences: the order in which function parameters are transmitted and the clearing of stacks.
Problem: Except for function calls with variable parameters, the rest are generally the _ stdcall convention. However, C/C ++ compiled the _ cdecl convention. Therefore, if you call the _ stdcall function in a VC or other environment, you must add the _ stdcall modifier to the function declaration, to call this function, use the _ stdcall Convention (for example, when using a DLL written in Delphi ).
(This method can be used in VC to modify: Project | settings... | C/C ++ | ...)

A set of switch statements are used in window process functions to process messages:
For example:
Lresult callback windowproc (
Hwnd,
Uint umsg,
Wparam,
Lparam
)
{
Switch (umsg)
{
Case wm_paint:
...
Break;
Case...
Break;
Case wm_close:
// Destroywindow (hwnd );
// Destroy the window and send the wm_destroy message.
Break;
Case wm_destroy:
// Postquitmessage (0 );
// Send the wm_quit message to the message queue and the request is terminated.
// Getmessage () returns 0 after obtaining the wm_quit message and exits the message cycle // loop to terminate the application.
Break;
Default:
Return defwindowproc (hwnd, umsg, wparam, lparam );
// Use the default Window Process to process messages that we are not interested in (other messages ).
// This is required.
} // Switch
Return 0;
} // Windowproc

13. destroywindow () function and postquitmessage () function prototype:
// ** The destroywindow function destroys the specified window. The function sends wm_destroy and wm_ncdestroy messages.

Bool destroywindow (
Hwnd // handle to window to destroy
);

// ** The postquitmessage function indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a wm_destroy message.
// ** The postquitmessage function posts a wm_quit message to the thread's message queue and returns immediately; the function simply indicates (prediction, notification) to the system that the thread is requesting to quit at some time in the future.

When the thread retrieves the wm_quit message from its message queue, it shocould exit its message loop and return control to the system.

Void postquitmessage (
Int nexitcode // exit code
);

14. About DC handle acquisition:
A) Use beginpaint () and endpaint () pairs. Note: it can only be used in response to the wm_paint message.
B) Use the getdc () and releasedc () pairs. Note that they cannot be used in the response wm_paint.

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.