Send Message MFC message map(2011-07-11 17:03:49)
reproduced
Tags: sending messages MFC message map |
|
1. When developing an application with the Win + API function, it is often necessary to use the Sendmassege function to send messages to certain objects to enable the object to implement certain functions (there are also corresponding APIs at this time)
Function to implement this function). For example, to close a window, you can send a WM_CLOSE message.
SendMessage (hwnd,wm_syscommand,sc_close,0);//Close main window
SendMessage (hwnd,wm_close,0,0);//close Normal window
2, Example to send a WM_COPYDATA message (that is, the WM_COPYDATA message is passed between two processes)
Before we do this example, let's take a look at the structure of the WM_COPYDATA message copydatastruct
typedef struct tagcopydatastruct{
dword dwdata;//handle to receive data
dword cbdata;//data size
pvoid lpdata;//data pointer
}copydatastruct;
Main Terminal:
Build MFC dialog box project, add edit box and a button
After you add the button in the Click event function:
void Cmydlg::onbutton1 ()
{
Todo:add your control notification handler code here
HWND Hwnd=::findwindow (NULL, "accept window");//Find our window handle to send a message
if (hwnd==null)
{
AfxMessageBox ("No acceptance window found");
Return
}
CString Msgedit; Declare the message we want to send
GetDlgItem (IDC_EDIT1)->getwindowtext (msgedit);//Get edit box contents, assign to Msgedit
Copydatastruct data={0}; Initializing a copydatastruct struct object
Data.dwdata= (DWORD) This->getsafehwnd (); Handle to receive data
Data.cbdata=msgedit. GetLength (); The size of the data
Data.lpdata=msgedit. GetBuffer (Msgedit. GetLength ()); Data pointers
:: SendMessage (Hwnd,wm_copydata, (WPARAM) This->getsafehwnd (), (LPARAM) &data);//Send Message
}
Receive window:
Create MFC dialog box project, add edit box
Add a WM_COPYDATA message response for the dialog box
Edit the code after the following:
BOOL Cmydlg::oncopydata (cwnd* pWnd, copydatastruct* pcopydatastruct)
function interfaces include: Window handle and copydatastruct struct pointer, respectively corresponding to Wparam,lparam
{
Todo:add your message handler code here and/or call default
Char msgdata[256];//declaration of accepted messages
DWORD size= pcopydatastruct->cbdata; Data size
CopyMemory (msgdata,pcopydatastruct->lpdata,size);//copy content via data pointer
msgdata[size]= ' + '; Add an end flag to an array
GetDlgItem (IDC_EDIT1)->setwindowtext (msgdata);//Set Content to edit box
Return Cdialog::oncopydata (PWnd, pcopydatastruct);//Return success
}
3. Example 2--sending its own registered message
This example registers a message with the RegisterWindowMessage function and then sends it with the Win32 API function SendMessage ().
Main Terminal:
Create an MFC dialog box and add a button
Add the following code to the dialog CPP file preprocessing:
Static UINT near Wm_rgsmsg=registerwindowmessage ("message");//Register a message that sends its own registration
After you add the button in the Click event function:
void Cmydlg::onbutton1 ()
{
Todo:add your control notification handler code here
HWND Hwnd=::findwindow (NULL, "accept window");//Find our window handle to send a message
if (hwnd==null)
{
AfxMessageBox ("No acceptance window found");
Return
}
Else
:: SendMessage (hwnd,wm_rgsmsg,1,0);//Send Message
}
For this example, the RegisterWindowMessage function is still the first to register an identical message, then define the message macro, bind the handler function
Receive window:
Building MFC Dialog Box Project
Add the following code to the dialog CPP file preprocessing:
Static UINT near Wm_rgsmsg=registerwindowmessage ("message");//Register a message that sends its own registration
At the message Macro declaration
Declare the message as:
afx_msg LRESULT onrgsmsg (WPARAM wparam,lparam LPARAM); Declaring a message map function
Add the following code where you set up the message map:
On_registered_message (WM_RGSMSG,ONRGSMSG)//Registered message macro should be placed in this location
In the CPP file, add the function definition with the following code:
LRESULT cmydlg::onrgsmsg (WPARAM wparam,lparam LPARAM)
{
AfxMessageBox ("received message");
return TRUE;
}
Send Message MFC message map