在PostMessage中传递CString参数 [英] Passing CString argument in PostMessage
本文介绍了在PostMessage中传递CString参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将CString作为参数传递给PostMessage?再次如何将LPARAM/WPARAM转换回CString?救救我吧!
How to pass CString as argument to the PostMessage? and again how to convert LPARAM/WPARAM back to CString? Help me!
推荐答案
您不知道.
当您发布消息时,您不知道何时会收到该消息.您要传递的字符串已经超出范围后,可以接收它.
如果您要在同一过程中将消息发布到另一个窗口,请使用源/接收器惯用语(又名生产者/消费者):
You don''t.
When you post a message, you don''t know when it will be received. It can be received after the string you want to pass is already out of scope.
If you''re posting a message to another window in the same process, use the source/sink idiom (AKA producer/consumer):
void producer()
{
wchar_t * wstring = new wchar_t[80];
wcscpy(wstring, L"HELLO");
::PostMessage(HWND, MY_CUSTOM_MESSAGE, 0, (LPARAM) wstring);
// DO NOT DELETE wstring;
}
class Consumer
{
LRESULT On_MyCustomMessage(WPARAM /*notused*/, LPARAM lpWstring)
{
wchar_t *pString = (wchar_t *) lpWstring;
// Do something with pString... You can even pass it to a CStringW constructor.
delete[] pString; // and don't forget this!
return 0L;
}
};
如果您使用的是CString
,请参见AllocSysString()
成员函数. BSTR旨在传递.
希望这会有所帮助,
巴勃罗.
If you''re using CString
, see the AllocSysString()
member function. BSTRs are meant to be passed around.
Hope this helps,
Pablo.
这篇关于在PostMessage中传递CString参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文