Sun Xin VC lecture notes-operating principle of Windows program

Source: Internet
Author: User

Statement:

I am also reading a video from instructor Sun recently. I want to take some Reading Notes to improve my understanding. However, after searching on csdn, I found that some friends have made notes. Based on the Object-Oriented "inheritance" perspective, I plan to add and modify it to solve the labor force. Shouldn't copyright be involved ?!

I searched the blog. csdn. Net/lewislau and two friends wrote the relevant notes (and both of them are the same ). I don't know who is the original author, so I listed two blog addresses:

Http://blog.csdn.net/hhitjsj021 http://blog.csdn.net/d007879

In the future, I will modify and post documents based on my predecessors! Haha! Inherit!

 

 

 

 

Windows program design 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.

 

MSG structure

--------------------------------------------------------------------------------

The MSG structure contains message information from a thread's message queue.

Syntax

Typedef struct {
Hwnd; // indicates the handle of a window. The message is associated with that window.
Uint message; // The specific message, expressed in an unsigned integer.
Wparam; // additional message Parameters
Lparam; // same as above
DWORD time; // a 32-bit integer that indicates the time when the message was delivered.
Point pt; // indicates the cursor position
} MSG, * PMSG;

The Resource Identifier. The operating system uses the handle to indicate the resource. Common handles include hicon, hcursor, hwnd, and hinstance)
 
For example, when you press the key, the wm_char message is sent and the corresponding ASCII code is saved through the additional parameters of the message.

 

 

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.

In addition to a good C foundation, you need to master two aspects of VC programming:
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.

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.

 

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.

 

 

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, * pwndclass;

Window style is a variable. each bit of the variable corresponds to a feature. If it is 1, this feature is available; if it is 0, this feature is not available. In order to facilitate memory, use some macros to correspond to some features, through inverse (~) And phase (&) can cancel some features. It is usually set to "cs_hredraw | cs_vredraw" to indicate vertical and horizontal repainting.

Hicon can be assigned a value by loadicon (it has two parameters: hinstance and lpctstr. Generally, the first parameter is null and only the second parameter is assigned, that is, the icon ID)
Hcursor is the same as hicon
Hbrush uses the getstockobject function, which can be used to obtain pen, paint brush, character, and picture board. Use hbrush for continuous forced conversion. Because the returned value of getstockobject is different from that of hbrush.

Window class registration:
Atom registerclass (
Const wndclass * lpwndclass // address of Structure with Class
// Data
);
// Note that the address character is used

 

Creation window:
Hwnd createwindow (
Lpctstr lpclassname, // register the window class name, with quotation marks
Lptstr lpwindowname, // window title, enclosed in quotation marks
DWORD dwstyle, // The Window Type (style) is generally (ws_overlappedwindow)
Int X, // Window X coordinate
Int y, // Window X coordinate
Int nwidth, // width
Int nheight, // height
Hwnd hwndparent, // handle pointing to the parent window
Hmenu, // menu handle
Handle hinstance, // handle of the current instance, passed by winmain
Lpvoid lpparam // wm_create additional parameter incoming pointer
);
The wm_create message is sent when the window is created.

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

Message Loop
MSG;
While (getmessage (& MSG,...) // retrieves a message from the Message Queue
{
Translatemessage (& MSG); // converts messages (such as keyboard messages. The conversion process does not affect the original message. Only new messages are created.
Dispatchmessage (& MSG); // callback function for dispatching messages to the window (OS calls the window callback function for processing ).
}

Bool getmessage (
Lpmsg, // message struct variable
Hwnd, // handle, which window? If it is null, all window handles
Uint wmsgfiltermin, // minimum message value. If it is 0, all messages are returned.
Uint wmsgfiltermax // maximum Message Value
);

 

Callback principle: When an application receives a message to a window, it should call a function to process the message. The message is automatically completed by the operating system.

Note: The function name can be used to indicate the first address (function pointer) of the function code. The additional data is usually 0.

Window Process function (callback function) prototype:
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

In response to wm_destroy, call postquitmessage (INT) to end the process. It will deliver a wm_quit message to the message queue. When the getmessage of the message loop receives the wm_quit message, 0 is returned and the program ends.
For messages that are not interested, use the window parameter in defwindowproc.

 

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 use

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.