为什么是“TranslateMessage"和“调度消息"单独调用? [英] Why are "TranslateMessage" and "DispatchMessage" separate calls?

查看:23
本文介绍了为什么是“TranslateMessage"和“调度消息"单独调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的大多数 Win32 主循环的结构都是这样的:

Most of the Win32 main loops I've seen are all structured like:

while (GetMessage(&message, NULL, 0, 0) > 0) {
  TranslateMessage(&message);
  DispatchMessage(&message);
}

有人向我指出 MsgWaitForMultipleObjects 可用于向主循环添加一些变化.但是是否存在在 GetMessage 之间做某事的情况, TranslateMessageDispatchMessage 真的有用吗?

It was pointed out to me that MsgWaitForMultipleObjects may be used to add some variety to a main loop. But is there a scenario where doing something between GetMessage, TranslateMessage and DispatchMessage is actually useful?

推荐答案

更传统的消息循环如下所示:

The more traditional message loop looks like this:

while (GetMessage(&msg, 0, 0, 0)) 
{
    if (!TranslateAccelerator(hwndMain, haccel, &msg))
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    } 
}

这是一个非常重要的提示,提示您在发送消息之前要做的事情:捕获应该在窗口看到之前拦截和特殊处理的消息.键盘快捷键就是一个经典的例子,无论哪个窗口有焦点,都需要检测它们.

It is a pretty big hint to what you'd want to do before dispatching the message: catch messages that ought to be intercepted and treated specially before the window sees them. Keyboard shortcuts are a classic example, they need to be detected no matter what window has the focus.

任何 GUI 类库都使用名为 App.PreProcessMessage 之类的虚拟方法公开它,该虚拟函数可以被覆盖,以便您的程序可以实现自己的快捷方式等.

Any GUI class library exposes it with a virtual method named something like App.PreProcessMessage, a virtual function that can be overridden so your program can implement its own shortcuts and whatnot.

这篇关于为什么是“TranslateMessage"和“调度消息"单独调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆