简体   繁体   English

mfc-具有自定义参数的sendmessage / postmessage

[英]mfc - sendmessage/postmessage with custom parameters

I want to have a message handler in MFC which accepts whatever parameters I define in the message-map. 我想在MFC中有一个消息处理程序,它接受我在消息映射中定义的任何参数。

For example, 例如,

static UINT UWM_STATUS_MSG = RegisterWindowMessage("Status message");
static UINT UWM_GOT_RESULT= RegisterWindowMessage("Result has been obtained");

//{{AFX_MSG(MyClass)
    afx_msg void OnCustomStringMessage(LPCTSTR);
    afx_msg void OnStatusMessage();
//}}AFX_MSG


BEGIN_MESSAGE_MAP(MyClass, CDialog)
    //{{AFX_MSG_MAP(MyClass)
        ON_REGISTERED_MESSAGE(UWM_STATUS_MSG, OnStatusMessage)
        ON_REGISTERED_MESSAGE(UWM_GOT_RESULT, OnCustomStringMessage)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void MyClass::OnCustomStringMessage(LPCTSTR result)
{
    m_result.SetWindowText(result);
}

void MyClass::OnStatusMessage()
{
    // Do something
}

DWORD WINAPI MyClass::thread(LPVOID lParam)
{
    char result[256] = { 0 };
    SendMessage(UWM_STATUS_MSG);

    // Do some stuff and store the result

    PostMessage(UWM_GOT_RESULT, result);
}

Is such a thing possible? 这样的事情可能吗?

The signature of member functions that are invoked via ON_MESSAGE or ON_REGISTERED_MESSAGE must be: 通过ON_MESSAGE或ON_register_MESSAGE调用的成员函数的签名必须为:

afx_msg LRESULT OnMyFunction(WPARAM p1, LPARAM p2); afx_msg LRESULT OnMyFunction(WPARAM p1,LPARAM p2);

You have to deal with that using cast operators. 您必须使用强制转换运算符处理该问题。

Therefore you should write this: 因此,您应该这样写:

...
afx_msg LRESULT OnCustomStringMessage(WPARAM p1, LPARAM p2);
...

LRESULT MyClass::OnCustomStringMessage(WPARAM p1, LPARAM p2)
{
  LPCTSTR result = (LPCTSTR)p1 ;
   m_result.SetWindowText(result);
}

DWORD WINAPI MyClass::thread(LPVOID lParam)
{
    static char result[256] = { 0 };   // we need a static here
                                       // (see explanations from previous answers)
    SendMessage(UWM_STATUS_MSG);

    // Do some stuff and store the result

    PostMessage(UWM_GOT_RESULT, (WPARAM)result);
}

If MyClass::thread is meant to be invoked from several different threads you need to deal with the result array in a more compilcated manner that just declaring it static, for example allocating the array in MyClass::thread ands deleting it in OnCustomStringMessage as suggested by user2173190's answer. 如果打算从多个不同的线程调用MyClass :: thread,则需要以一种更加编译的方式处理结果数组,即仅声明它为静态,例如,在MyClass :: thread中分配该数组,然后按照建议在OnCustomStringMessage中将其删除由user2173190的答案。

Try using WM_USER messages as your custom message and handle it in OnMessage method by overriding it. 尝试将WM_USER消息用作您的自定义消息,并通过覆盖它在OnMessage方法中对其进行处理。 To know about WM_USER messages refer here 要了解有关WM_USER消息的信息,请参见此处

If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters cannot include pointers. 如果将WM_USER以下范围内的消息发送给异步消息函数(PostMessage,SendNotifyMessage和SendMessageCallback),则其消息参数不能包含指针。 Otherwise, the operation will fail. 否则,操作将失败。 The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used. 该函数将在接收线程有机会处理该消息之前返回,并且发件人将在使用该消息之前释放内存。

PostMessage's message parameters can include pointers You can include pointers as parameters. PostMessage的消息参数可以包含指针您可以包含指针作为参数。 Cast them as an integer and do not dispose or free them on the thread or function that fires the PostMessage, but dispose or free them on the receiving thread or function. 将它们强制转换为整数,不要在触发PostMessage的线程或函数上处置或释放它们,而在接收线程或函数上处置或释放它们。 Just make sure that when using a pointer, that you only use one pointer type for one message, don't mix pointer to different objects with the same message. 只要确保使用指针时,您只对一条消息使用一种指针类型,就不要将指向同一消息的不同对象的指针混合使用。

http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms644944(v=vs.85).aspx http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms644944(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM