开发者

Where to group-catch thread messages WINAPI/MFC

Does anyone know where is the best place to forward thread messages to child controls?

Basically, app has to catch registered thread messages in range msg_frst...msg_last and forward them to active frame/view.

There are 3 overload points in MFC, or so it seems: DefWndProc OnWndMessage PreTranslateMess开发者_运维问答age

Which is the correct one to override?


A message posted with PostThreadMessage() has a NULL window handle. So forget about any of the window methods, DispatchMessage() isn't going to deliver them. All you got is CWinThread::PreTranslateMessage().

But there's a big hazard here, this isn't going to be called anymore when any code in that thread start pumping its own message loop. Your messages now fall in the bit bucket because other code is now calling Peek/GetMessage(). That's a lot more likely then you think, MessageBox() is enough. Or the COM modal loop. Etcetera.

You can only safely use PostThreadMessage() to send messages to a thread that does not create any windows of its own. The workaround is simple enough, provide the thread with a window handle so it can call PostMessage() instead. That could be a hidden window, dedicated to handling these messages for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜