1. Create two dialog box items and add custom messages to StdAfx. h respectively.
# Define WM_CONTROLPRINT WM_USER ++ 1001
2. the sender obtains the FindWindow () method for handling other windows ();
Void CAaDlg: OnButtonsend ()
{
// TODO: Add your control notification handler code here
// Obtain the handle of other processes by using the form name
CWnd * pWnd = CWnd: FindWindow (NULL, _ T ("Bb "));
If (pWnd = NULL)
{
AfxMessageBox ("the recipient is not running! ");
Return;
}
PWnd-> SendMessage (WM_CONTROLPRINT, NULL, 0 );
}
3. the receiving end receives messages from other processes.
// Message Response Function
Void OnControlPrint (WPARAM wParam, LPARAM lParam );
Void CBbDlg: OnControlPrint (WPARAM wParam, LPARAM lParam)
{
If (wParam = 0 & lParam = 0)
AfxMessageBox ("HelloWorld! ");
Return;
}
// Add message ing
BEGIN_MESSAGE_MAP (CBbDlg, CDialog)
// {AFX_MSG_MAP (CBbDlg)
ON_MESSAGE (WM_CONTROLPRINT, OnControlPrint)
//} AFX_MSG_MAP
END_MESSAGE_MAP ()
Running result:
Source code download: progress message. rar
Url: http://greatverve.cnblogs.com/archive/2012/10/01/mfc-SendMessage.html
Refer:VCOfSENDMESSAGEAndPOSTMESSAGEOfZoneOther
The SendMessage function is blocked. The PostMessage function is not blocked.
SendMessage:
Function: This function sends a specified message to one or more windows. This function calls the window program for the specified window until the window program finishes processing the message and returns it. The PostMessage function returns a message immediately after it is sent to the Message Queue of a thread.
Function prototype: LRESULT SendMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM IParam );
Parameters:
HWnd: the handle of the window whose window program will receive messages. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows, however, messages are not sent to subwindows.
Msg: Specifies the message to be sent.
WParam: Specify the additional message.
IParam: Specify the attached message.
Return Value: the return value specifies the message processing result, depending on the sent message.
Note: applications that need to communicate with HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for communications between applications.
If the specified window is created by the calling thread, the window program is called as a subroutine immediately. If the specified window is created by different threads, the system switches to the thread and calls the appropriate window program. Messages between threads are processed only when the thread executes the message retrieval code. The sending thread is blocked until the receiving thread finishes processing the message.
Windows CE: Windows CE does not support all messages supported by Windows desktop. Before using SendMesssge, check whether the sent message is supported.
Quick query: Windows NT: 3.1 and later versions: Windows: 95 and later versions; Windows CE: 1.0 and later versions; header file: winuser. h. Input Library: user32.lib; Unicode: Unicode and ANSI in Windows NT.
PostMessage:
Function: This function puts a message (sent) to the Message Queue associated with the thread created in the specified window, and returns the message without waiting for the thread to process the message. Messages in a message queue are obtained by calling GetMessage and PeekMessage.
Function prototype: B00L PostMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );
Parameters
HWnd: the handle of the window in which the window program receives messages. Two values with specific meanings are recommended:
HWND. BROADCAST: all top-level windows sent to the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows. Messages are not sent to subwindows.
NULL: the operation of this function is the same as that of the PostThreadMessage function that calls the dwThread parameter to set it to the current thread identifier.
Msg: Specifies the message to be sent.
WParam: specify the specific information of the attached message.
IParam: specify the specific information of the attached message.
Return Value: If the function call is successful, a non-zero value is returned. If the function call fails, the return value is zero. To obtain more error information, call the GetLastError function.
Note: applications that need to communicate in HWND_BROADCAST mode should use the RegisterwindwosMessage function to obtain unique messages for inter-Application Communication.
If you send a message below the WM_USER range to the asynchronous message function (PostMessage. sendpolicymessage, SendMesssgeCallback), the Message Parameter cannot contain a pointer. Otherwise, the Operation will fail. The function will be returned before the thread processes the message, and the sender will be released before the memory is used.