MFC window messages PostMessage and SendMessage

Source: Internet
Author: User

These messages used to be relatively small, but today I have a matter, I think the message is not.

The thing is, I need to refresh the contents of the dialog box in the thread, but there is an assertion error when executing to UpdateData.

Looked up the relevant information, found that this may be multiple modules to invoke the same method of the problem occurred. I can't get through my logic anyway.

I will now update the dialog information function by sending a window message through the thread, letting the dialog message handler function handle my customized message and refresh the dialog data for me.

http://blog.csdn.net/a8082649/article/details/7733527

1. Customize the message ID.

#define Wm_my_message (wm_user+100) Wm_user is the ID reserved for the Windows system for non-system messages, at least 100, because messages from other controls take up part of the message.

2. Define the message handler function.

The message handler function is a member function of the message target class. It should first be declared in the. h file. Here, for example, the main window class, the main window class name Cmaindialog, the message handler function is declared first in CMainDialog.h.

Protected

afx_msg LRESULT onmymessage (WPARAM WPARAM, LPARAM LPARAM);

3. Implement message-handling functions in CMainDialog.cpp


lpesult cmaindialog::onmymessage (WPARAM WPARAM, LPARAM LPARAM) {//TODO: Handling user custom Messages ...  return 0; }

4. Mapping the message ID and message handler functions in CMainDialog.cpp

Begin_message_map (CMainFrame, CMDIFrameWnd)
{{Afx_msg_map (CMainFrame)
On_wm_create ()
On_wm_timer ()
On_message (Wm_my_message, OnMyMessage)
}}afx_msg_map
End_message_map ()

When the thread is created, the main window handle is passed as a parameter to the thread, and the message can be delivered with PostMessage.

Message preprocessing functions

Add the message handler function PreTranslateMessage, which can be added via MFC ClassWizard

  1. BOOL cpretranslatemessagedlg::P retranslatemessage(MSG* pMsg)
  2. {
  3. if (pMsg->message==wm_lbuttondown)
  4. {
  5. MessageBox("3344");
  6. }
  7. Return CDialog::P retranslatemessage(pMsg);
  8. }

This function is a manually added message function that can be used by pmsg->message to determine what the message is, such as a left-click message for Wm_lbuttondown, which executes when the left button is clicked. Also, the PreTranslateMessage function is executed when the keyboard is tapped .

// ============================================================================================================= ======

SendMessage function function : This function sends the specified message to one or more windows . This function invokes the window program for the specified window until the window program finishes processing the message and returns. Unlike function PostMessage, a message is sent to a thread's message queue and returned immediately.

Function prototypes :

LRESULT SendMessage (hwnd hwnd,UINT wmsg,WPARAM WPARAM,LPARAM iparam);

Parameters:

hWnd: The handle of the window whose window program will receive the message. If this parameter is Hwnd_broadcast, the message is sent to all top-level windows in the system, including invalid or invisible non-owned windows, overwritten windows, and pop-up windows, but messages are not sent to child windows.

wmsg are used to distinguish constant values from other messages, which can be predefined constants in Windows cells or custom constants .

WParam is typically a message-related constant value or a handle to a window or control

LParam is typically a pointer to data in memory. Since Wparm,lparam, and pointer are all 32-bit, they can be converted from one to the other

return value : The return value specifies the result of the message processing, depending on the message being sent.

SendMessage use case

1. System messages (wm_)

:: SendMessage (this->m_hwnd,wm_close,0,0);

SendMessage (hwnd,wm_keydown,vk_tab,0x000f0001);

SendMessage (hwnd,wm_char,vk_tab,0x000f0001);

SendMessage (hwnd,wm_keyup,vk_tab,0xc00f0001);

SendMessage (hwnd,wm_keydown,vk_tab,0x000f0001);

SendMessage (hwnd,wm_char,vk_tab,0x000f0001);

SendMessage (hwnd,wm_keyup,vk_tab,0xc00f0001);

SendMessage (hwnd,wm_keydown,vk_tab,0x000f0001);

SendMessage (hwnd,wm_char,vk_tab,0x000f0001);

SendMessage (hwnd,wm_keyup,vk_tab,0xc00f0001);

SendMessage (hwnd,wm_keydown,vk_return,0x001c0001);

SendMessage (hwnd,wm_char,vk_return,0x001c0001);

SendMessage HWND, Wm_showwindow, sw_hide, vbnull ' hidden form

SendMessage HWND, Wm_showwindow, Sw_normal, vbnull ' show form

SendMessage (HWND, Wm_syscommand, Sc_close, ByVal 0&) ' Closed

SendMessage (HWND, Wm_syscommand, Sc_minimize, ByVal 0&) ' Minimized

SendMessage (HWND, wm_paste, 0, 0) ' Paste

SendMessage (HWND, wm_copy, 0, 0) ' Copy

SendMessage (HWND, Wm_undo, 0, 0) ' Undo

2. Notification message

For example: send a click message to the Resource ID idc_button2 button:

SendMessage (wm_command,

Makelong (idc_button2,bn_clicked),

(LPARAM) (GetDlgItem (Idc_button2), GetSafeHwnd ()));

Or:

:: SendMessage (M_hwnd,

WM_COMMAND,

Makelong (idc_button2,bn_clicked),

(LPARAM) (GetDlgItem (Idc_button2), GetSafeHwnd ()));

Example: Send a message to a menu item with a menu ID of id_get

:: SendMessage (AfxGetMainWnd ()->m_hwnd,wm_command,id_get,null);

3. User-defined messages

Sendmessge (wm_mymsg, 0,0);

Or:

: : Sendmessge (:: AfxGetMainWnd ()->m_hwnd,wm_mymsg,0,0);

4. Sending messages to other applications

You can also send messages to other applications, provided that you find a handle to the form, such as: Use with the FindWindow () function;

Such as:

CString str= "360w.txt-Notepad ";//Send a wm_close message to the Notepad window for str

CWnd *pwnd=cwnd::findwindow (NULL,STR);//Gets the destination Window object

:: SendMessage (*pwnd,wm_close,0,0);

How to get the window caption:

1. Using VC + + self-tool Spy + +

2. Programming implementation, EnumWindows function can be implemented; see "Enumerate all forms and send close messages to open forms"http://download.csdn.net/detail/nuptboyzhb/4162747

Example:

HWND Gameh=null; Gameh=::findwindow (NULL, "window caption");//Refer to the title of the game. can be viewed with the Spy + + tool that comes with vc6.0

cwnd* pWnd = Cwnd::fromhandle (Gameh);

At the coordinates of the (x, y) point, send the mouse down message//Note that x, Y is the coordinates of the client area

:: SendMessage (*pwnd,wm_lbuttondown,0, (y<<16) +x);

Send mouse move message, mouse move to point (x, y)

:: SendMessage (*pwnd,wm_mousemove,0,makelong (x, y));

Send Paste Message

:: SendMessage (dlg_hwnd,wm_paste,0,0);

5. Send your own registered message 5.1 send side:

This example registers a message with the registerwindowmessage function and then sends it with the Win32 API function SendMessage (). Main Terminal: Establish MFC dialog box, add a button in the dialog CPP file pre-processing to add the following code: Static UINT near Wm_rgsmsg=registerwindowmessage ("MESSAGE");//  Registers a message that sends its own registration after the button click event function is added: 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 accept window found");  Return } else:: SendMessage (hwnd,wm_rgsmsg,1,0);//Send Message}

5.2 Receiving End:

For this example, the RegisterWindowMessage function is still the first to register an identical message, and then define the message macro, which binds the handler to receive the window: building an MFC Dialog Project add the following code to the dialog box CPP file preprocessing: Static UINT near Wm_rgsmsg=registerwindowmessage ("message");//Register a message to send its own registered messages at the message macro declaration to declare the message as: afx_msg LRESULT onrgsmsg (WPARAM    Wparam,lparam LPARAM); Declare the message map function add the following code where the message map is established: on_registered_message (WM_RGSMSG,ONRGSMSG)//Register the message macro should be placed in the CPP file, add the function definition, the code is as follows:  LRESULT cmydlg::onrgsmsg (WPARAM wparam,lparam LPARAM) {AfxMessageBox ("received message"); return TRUE; }

http://blog.csdn.net/nupt123456789/article/details/7370463

MFC window messages PostMessage and SendMessage

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.