MFC delivers messages with PostMessage

Source: Internet
Author: User
Tags message queue knowledge base

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)
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.

SendMessage function Description: Sends the specified message to one or more windows. This function invokes the window procedure for the specified window until the window procedure finishes processing the message.

The PostMessage function puts the message in the message queue and returns immediately.

Problems

1) Use SendMessage to cut, copy, and paste

View Row Code
1 SendMessage (hwnd, wm_copy, 0, 0);
2 SendMessage (hwnd, Wm_cut, 0, 0);
3 SendMessage (hwnd, Wm_paste, 0, 0);

2) The difference between SendMessage and PostMessage

PostMessage returns immediately after the message is placed in the message queue, and SendMessage is not returned until the window procedure finishes processing the message.

3) SendMessage send wm_copydata messages transfer data between processes

The primary purpose of the WM_COPYDATA message is to allow a small amount of read-only data to be passed between processes. The SDK documentation recommends that the user use the SendMessage () function, and the receiver does not return until the data copy is complete, so that the sender cannot delete and modify the data.

For example:

View Row Code
1 std:string strdata = "VC Knowledge Base vckbase.com";
2 copydatastruct CDs;
3 Cds.dwdata = 0;
4 Cds.cbdata = strdata. Length ();
5 Cds.lpdata = strdata. C_str ();
6 ::SendMessage (hwnd, wm_copydata, NULL, (LPARAM)&cds);

3, PostThreadMessage sometimes fail, reported 1444 error (Invalid thread identifier.), in fact, this is not necessarily the reason why the thread does not exist, it is possible that the thread does not exist in the message queue. In fact, not every thread has a message queue, so how does the thread have it? The answer is to call at least one message-related function, such as getmessage,peekmessage.

4, if it is postthreadmessage dynamic allocation of memory to another thread, pay attention to the correct release of RAM. and PostThreadMessage not be able to post wm_copydate such synchronization messages, or will error!

5, it is best not to use PostThreadMessage Post message to a window, using postmessage substitution.

1 #include <windows.h>2#include <cstdio>3#include <process.h>4    #defineMy_msg wm_user+1005    Const intMax_info_size = -;6HANDLE hstartevent;//Thread Start Event7    //thread function8unsigned __stdcall Fun (void*param)9    {Tenprintf"Thread Fun Start One     A     -\ n"); -msg msg; thePeekMessage (&msg, NULL, Wm_user, Wm_user, pm_noremove); -        if(! SetEvent (hstartevent))//Set Thread Start event -        { -printf"Set Start Event failed,errno:%d\n",:: GetLastError ()); +            return 1; -        } +         A         while(true) at        { -            if(GetMessage (&msg,0,0,0))//get msg from message queue -            { -                Switch(msg.message) -                { -                 Casemy_msg: in                    Char* PInfo = (Char*) Msg.wparam; -printf"recv%s\n", pInfo); todelete[] PInfo; +                     Break; -                } the            } *        }; $        return 0;Panax Notoginseng    } -    intMain () the    { +HANDLE hthread; Aunsigned nthreadid; theHstartevent =:: CreateEvent (0, False,false,0);//Create thread Start event +        if(Hstartevent = =0) -        { $printf"Create start Event failed,errno:%d\n",:: GetLastError ()); $            return 1; -        } -        //Start Thread theHthread = (HANDLE) _beginthreadex (NULL,0, &fun, NULL,0, &nthreadid); -        if(Hthread = =0)Wuyi        { theprintf"Start thread failed,errno:%d\n",:: GetLastError ()); -CloseHandle (hstartevent); Wu            return 1; -        } About        //Wait thread start event to avoid postthreadmessage return errno:1444 $:: WaitForSingleObject (hstartevent,infinite); -CloseHandle (hstartevent); -        intCount =0; -         while(true) A        { +            Char* PInfo =New Char[Max_info_size];//Create dynamic MSG thesprintf (PInfo,"msg_%d",++count); -            if(! PostThreadMessage (Nthreadid,my_msg, (WPARAM) PInfo,0))//Post thread msg $            { theprintf"Post Message failed,errno:%d\n",:: GetLastError ()); thedelete[] PInfo; the            } the:: Sleep ( +); -        } inCloseHandle (hthread); the        return 0; the}

MFC delivers messages with PostMessage

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.