开发者

Where to group-catch thread messages WINAPI/MFC

开发者 https://www.devze.com 2023-01-25 07:21 出处:网络
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 f

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

精彩评论

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