win32消息循环 - 我应该破解还是返回?

时间:2012-10-01 17:45:15

标签: c++ visual-c++

我想知道Windows消息循环的正确返回值是什么。到目前为止,我已经使用了以下内容:

case WM_LBUTTONDOWN: // left mouse button pressed.
    if (condition == true )
    {
        do_something();
    }
break;

但我也看到了类似的事情:

if (IDC_BUTTON == LOWORD(wParam) && BN_CLICKED == HIWORD(wParam))
{
    do_something();
    return true;
}
break;

哪一个是正确的?我应该return true吗?或者我应该break?这有关系吗?

编辑是否取决于我是否正在按下按钮或鼠标移动?

3 个答案:

答案 0 :(得分:4)

返回值记录在MSDN上,作为每条消息的文档的一部分。例如,WM_LBUTTONDOWN的文档说明了

  

如果应用程序处理此消息,则应返回零。

对于其他消息,返回值可能更有意义。你应该经常阅读文档,不要猜。

答案 1 :(得分:2)

breakreturn是否取决于具体情况 - 即您希望在处理此消息后做什么。如果你还有其他事情要做 - 你可以马上回来。

但是当你从消息循环中返回时 - 请确保return 0;
根据{{​​3}}:If an application processes this message, it should return zero

答案 2 :(得分:1)

如果使用break,则执行仍保留在当前函数中。它允许您在返回之前设置结果变量并执行一些不一致的操作。但是如果你返回,你的执行将进入调用函数,并且不会继续执行当前函数中的任何进一步操作。因此没有正确的变体 - 由您来决定使用什么:breakreturn。这取决于你自己的设计。

例如:

bool result;

switch(msg)
{
case WM_LBUTTONDOWN:
    //do smth...
    result=true;
    break;//execution goes to (3)
case WM_MOUSEMOVE:
    //actions...
    result=true;
    break;//execution goes to (3)
case WM_RBUTTONDOWN:
    //some code...
    //no break here! execution continues to the next event
case WM_MWHEELUP:
    //if WM_RBUTTONDOWN occured, execution continues to here and goes on
    result=false;//maybe you need false here, I dont know
    break;//processing WM_RBUTTONDOWN will stop only here, as well as processing WM_MWHEELUP
case WM_USER1:
    //lines of code....
    return true;//do not perform final actions starting from (3), but end up with this function immideately
default:
    result=false;    
}

// (3)
//perform some final actions...

return result;

它不取决于您与之交互的事件。