简体   繁体   English

WinAPI - 带有自己回调的消息循环

[英]WinAPI - message loop with own callback

The usual WinAPI message loop looks something like this: 通常的WinAPI消息循环看起来像这样:

MSG msg;
while (GetMessage(&msg, hwnd, 0, 0))
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

Is it allowed not to call DispatchMessage() but to handle the message on your own? 是否允许不调用DispatchMessage(),而是自己处理消息? If not, how could I nicely approach this behavior while avoiding global variables and thread problems? 如果没有,我怎么能在避免全局变量和线程问题的同时很好地处理这种行为?

Edit: I basically want to use my own callback function, which hasn't the WndProc signature. 编辑:我基本上想要使用自己的回调函数,它没有WndProc签名。 But I can't think of a way to call that function out of a WndProc without using static or global variables. 但我想不出一种在不使用静态或全局变量的情况下从WndProc中调用该函数的方法。 [Which would require locking, which I think isn't the best thing you can do with a callback function which probably gets called very frequently.] [这需要锁定,我认为这不是你可以用回调函数做的最好的事情,它可能会被频繁调用。]

Thanks for your help. 谢谢你的帮助。

Is it allowed not to call DispatchMessage() but to handle the message on your own? 是否允许不调用DispatchMessage(),而是自己处理消息? If not, how could I nicely approach this behavior while avoiding global variables and thread problems? 如果没有,我怎么能在避免全局变量和线程问题的同时很好地处理这种行为?

If you are planning to use multiple threads in your GUI then each thread that creates a window will need to manage it's own message queue. 如果您计划在GUI中使用多个线程,那么创建窗口的每个线程都需要管理它自己的消息队列。

From this page: http://msdn.microsoft.com/en-us/library/ms810439.aspx 从此页面: http//msdn.microsoft.com/en-us/library/ms810439.aspx

Changes to the Message Loop 消息循环的更改

Applications with multiple threads must include a message loop in each thread that creates a window. 具有多个线程的应用程序必须在每个创建窗口的线程中包含消息循环。 The message loop and window procedure for a window must be processed by the thread that created the window. 窗口的消息循环和窗口过程必须由创建窗口的线程处理。 If the message loop does not reside in the same thread that created the window, the DispatchMessage function will not get messages for the window. 如果消息循环不驻留在创建窗口的同一线程中,则DispatchMessage函数将不会获取该窗口的消息。 As a result, the window will appear but won't show activation and won't repaint, be moved, receive mouse messages, or generally work as you expect it to. 因此,窗口将出现,但不会显示激活,也不会重新绘制,移动,接收鼠标消息,或者通常按预期工作。

You can react to a message there, but you still need/want to call DispatchMessage and actually handle the message in your normal wndproc. 您可以对那里的消息做出反应,但您仍然需要/想要调用DispatchMessage并实际处理正常wndproc中的消息。 I'd be happy to say more about avoiding globals and/or threading problems, but it's hard to comment without more details about what you want to avoid. 我很乐意多说一些关于避免全局和/或线程问题的内容,但如果没有关于你想要避免的更多细节,就很难评论。

Yes, you can handle the message yourself, if you wish. 是的,如果您愿意,您可以自己处理此消息。 I usually set the result field to 0, but Windows only make use of this field for a few messages. 我通常将结果字段设置为0,但Windows仅将此字段用于一些消息。

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

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