"Turn" SendMessage and Wprame, Lparame

Source: Internet
Author: User

Original URL: http://www.cnblogs.com/renyuan/archive/2012/11/26/2789103.html

SendMessage and Wprame, Lparame

typedef unsigned INT UINT

typedef UINT WPARAM

typedef LONG LPARAM

typedef LONG LRESULT

In Win32API, the original 16-bit variable is also extended to 32 bits, so the size of wparam and lparam is exactly the same at this point. WParam often represent the ID of some controls or a combination of high-level bottoms to indicate the position of the mouse, and if the sender of a message needs a pointer to a struct or a handle of a certain type, it is customary to pass it by lparam.

In theory, when using custom messages, the meaning of WPARAM and lparam can be arbitrarily specified by programmers, but it is best to follow the habits of MFC. When calling the SendMessage () function, the second argument is WPARAM, and the third argument is the lparam of the message, but when you write the On_message () macro in a class in the program to process the message, the handler Somehandler (WPARAM, Lpram (the default is 0) must be interpreted in terms of the meaning of the SendMessage call when interpreting both parameters.

The basic structure of SendMessage is as follows:
SendMessage (
HWND hwnd,//Handle to the target window or thread of the message delivery.
UINT MSG,//message category (here can be some system messages, also can be their own definition, described below)
WPARAM WPARAM,//Parameter 1 (WPARAM is actually the same type as uint,
Right-click on the "Go to wparam definition" option in the VC compiler to view.
LPARAM LPARAM); Parameter 2

For example:: SendMessage (This->m_hwnd, Wm_my_dosome, (WPARAM) 0, (LPARAM) 0);

Here I send the message is received in this form, (so receive the message is also this window) so handle with: This->m_hwnd, here the message category Wm_my_dosome is I custom,
In the header file of the form or thread that receives the message:
#define Wm_my_dosome wm_user+1//do something
Of course you can also define more as:
#define Wm_doother wm_user+2//Do other

means to do something.

The SendMessage () in MFC is, who calls it, is the carrier that receives the message, and must implement the message. For example: Pview->sendmessage (), there is a PView window to receive the message (which is also a window).

Message response mechanism

1, the composition of the message: A message consists of a message name (UINT), and two parameters (Wparam,lparam). The system sends a message to a window when the user enters or changes the state of the window. For example, when a menu is selected, a WM_COMMAND message is sent, and WParam's low word (LOWORD (WParam)) is the ID number of the command, which is the menu ID of the menu. Of course, users can also define their own message names, or they can use custom messages to send notifications and transfer data.

2. Who will receive the message: A message must be received by a window. In the process of the window (WNDPROC), the message can be parsed, and the messages that are of interest to you are processed. For example, if you want to process the menu selection, you can define the code to process the WM_COMMAND, and if you want to do the graphics output in the window, you must process the WM_PAINT.

3. The unhandled message went there: MS has written the default window procedure for Windows, and this window process will be responsible for handling messages that you do not process. It is because of this default window procedure that we can exploit windows to develop without paying too much attention to the processing of various messages in the window.    For example, when a window is being dragged, a lot of messages are sent, and we can ignore it and let the system handle it on its own. 4. Window handle: When it comes to the message, the window handle, the system through the window handle to uniquely identify a window in the entire system, when sending a message, a window handle must be specified to indicate that the message was received by that window. And each window will have its own window procedure., so the user's input will be handled correctly. For example, there are two windows that share a window procedure code, and when you press the mouse on one of the windows, the message is sent through a handle to window one instead of window two. message mechanism: The system will maintain one or more message queues, and all resulting messages are put back into or inserted into the queue. The system takes each message out of the queue, depending on the receive handle for messageInstead, the message is sent to the message loop of the program that owns the window. Each of the running The program has its own message loop, gets its own message in the loop and invokes the corresponding window procedure based on the handle of the receive window. In the absence of a message, the message loop gives control to the system so windows can do multiple tasks at the same time. The use of SendMessage in MFC and the addition of corresponding functions:

1,:: SendMessage (AfxGetMainWnd ()->M_HWND,WM_CHILDFRAMEDBCLK, 0,0);

Where WM_CHILDFRAMEDBCLK is a custom message ID (whose ID is wm_user+1), AfxGetMainWnd ()->m_hwnd is to get the main window (this cannot be used here GetParent ()->m_ HWnd or GetParentFrame ()->m_hwnd, because this is the parent window, but the parent window is not necessarily the main window, it must be noted, otherwise the message will be wrong resulting in receiving.

2. Next, define a function that the message needs to map, as follows:

afx_msg LRESULT onchlidframedbclick (WPARAM WPARAM, LPARAM LPARAM);

Note that the format must be: Two parameters are necessary, the return type must be LRESULT, many articles on the internet have neglected these two points, which is also a common error in the online article place.

3. Adding Message function mappings

On_message (Wm_childframedbclk,onchlidframedbclick)

Note that this must be on_message, cannot use On_command, the former mainly for user-defined messages, the latter for WM_COMMAND commands, such as menus, toolbars and so on.

4. Implement the Message function:

We define one such thing in the receiving form (the process),

LRESULT Cmainframe::onchlidframedbclick (WPARAM WPARAM, LPARAM LPARAM)
{
Cancelfullscreenwin (); Here called a full screen of the child window of the self-write function, I do not post it, and later on the topic will be mentioned

return 0;
}

"Turn" SendMessage and Wprame, Lparame

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.