1, the basic structure of SendMessage:
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,
LPARAM LPARAM); Parameter 2
Some of these parameters are as follows:
typedef unsigned int UINT;
typedef UINT WPARAM;
typedef LONG LPARAM;
typedef LONG LRESULT;
2. Examples of usage:
1 voidctextshow::onbnclickedgraybtn ()2 {3 //TODO: Add control notification handler code here4 Mydib Dib (FILEPATHSTR);5 Dib. Bitmaptoiplimage ();6 intWidth=Dib. GetWidth ();7 intheight=Dib. GetHeight ();8iplimage* src=cvcreateimage (Cvsize (width,height), ipl_depth_8u,1);9 Cvcvtcolor (dib.piplimage,src,cv_rgb2gray);TenCvsmooth (Src,src,cv_median, One); OneCvthreshold (SRC,SRC, +,255, cv_thresh_binary); A:: SendMessage ( This->m_hwnd,wm_showimage,0, (LPARAM) src); -}
Implementation in the New dialog box, click the Grayscale button, you can convert the picture binary graph, where SendMessage is a binary graph to display; After clicking, send the message with parameters (wm_user+1) to the main window, wm_ ShowImage is the identifier for the custom message, first the new dialog box is set to the main window, and the constructor of the Ctextshow joins:
1 afxgetapp ()->m_pmainwnd=this;
So This->m_hwnd in SendMessage is the main window, instead of GetDlgItem (idc_picshow)->m_hwnd, where idc_picshow is the ID of the display picture control, I do not know why the picture is not displayed. Specific process:
1) defined in the form that receives the message and in the header file where the thread is located: #define Wm_showimage wm_user+1
2) Next define a function that the message needs to map, as follows: afx_msg LRESULT onshowimage (WPARAM WPARAM, LPARAM LPARAM);
Note The format must be: two parameters are necessary, the return type must be Lresult
3) Add Message function Map: on_message (wm_showimage,onshowimage)
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:
1 LRESULT ctextshow::onshowimage (WPARAM WPARAM, LPARAM LPARAM) 2 {3 iplimage* plmgsrc= (iplimage*) LParam; 4 ciplimagetobitmap Iplimagetobitmap; 5 Iplimagetobitmap.showimageauto (Plmgsrc,getdlgitem (idc_picshow)); 6 return 0 ; 7 }
3. Summary:
Whenever you use the MFC Wizard or manually add message processing, you need to increase the three code in the source file:
1. Add the function declaration of the message processing member function in the definition of the class.
2. Add the corresponding message map entry in the message map table of the class.
3, in the implementation of the class to add the message processing member functions of the implementation of the function.
If you customize the message at the time, you also need to define a macro for the custom message in the header file.
For example, to add a custom message UM_AAA processing in the ABC class, Respond to the ONAAA () function when the message is generated. This can be accomplished in four steps:
1. Define a macro for the custom message in the header file ABC.H.
#define UM_AAA wm_user+1
A large number of messages are provided in the Windows system while providing a space for user-defined messages, and when we customize the macro of the message, only the values above the Wm_user will not conflict with the system-provided messages.
2. Declare the ONAAA () of the message response function in the header file ABC.H.
Protected
{{afx_msg (ABC)
afx_msg void Onaaa ();
}}afx_msg
Declare_message_map ()
The message response function should be placed before Declare_message_map (), afx_msg the comment macro. the function declaration does not have a afx_msg qualifier, which is also a macro, the macro surface of this function is a message response function declaration.
3. Do message mapping
Beigin_message_map (ABC)
{{Afx_msg_map (ABC)
On_message (UM_AAA, ONAAA)
}}afx_msg_map
End_message_map ()
The um_aaa and ONAAA response functions are associated with on_message () between AFX_MSG_MAP annotation macros before End_message_map ().
4, the implementation of the message response function in ABC.CPP
void Onaaa ()
{......}
Only by following the 4 steps above, you can add the appropriate message and response functions to your system program according to your needs.
How to use SendMessage to send messages correctly