简体   繁体   English

SendMessage()和PostMessage()的正确用法

[英]SendMessage() and PostMessage() proper usage

I've got a problem with using SendMessage() and PostMessage() properly. 我在正确使用SendMessage()和PostMessage()时遇到了问题。 The thing what i'm trying to do is SendMessage(hWnd, WM_USER + 1, 0, 0); 我想做的是SendMessage(hWnd, WM_USER + 1, 0, 0); in my window procedure 在我的窗口程序中

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case WM_CREATE:
        break;
    case IDM_ABOUT:
        DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
        break;
    case IDM_EXIT:
        DestroyWindow(hWnd);
        break;
    case WM_USER:
        break;
    case WM_USER + 1:
        break;
    default:
        if (grid.ProcessEvent(wmId, wmEvent))
        {
            SendMessage(hWnd, WM_USER + 1, 0, 0);
            break;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Im not really sure why it doesn't work, maybe someone could help. 我不太确定为什么它不起作用,也许有人可以帮忙。 I'm sure SendMessage(...) is called but it doesn't affect to run WndProc with my arguments. 我确定已调用SendMessage(...),但是使用我的参数运行WndProc并不会影响。

The case label case WM_USER + 1: is nested in the inner case statement (that switches on wmId ). 案例标签case WM_USER + 1:嵌套在内部case语句中(在wmIdwmId )。 It needs to be moved out to the switch (message) case statement, so it is at the same level of nesting as case WM_COMMAND: and case WM_DESTROY: . 它需要移到switch (message) case语句中,因此它与case WM_COMMAND:case WM_DESTROY:处于同一嵌套级别。

The above also applies to case WM_USER: . 以上内容也适用于case WM_USER:

SendMessage is specified to not return until the receiver window has processed the message - which it cannot do as you're inside your own WndProc and messaging yourself. 指定SendMessage直到接收者窗口处理完消息后才返回-因为您在自己的WndProc中并自行发送消息,所以它无法执行。 This is one of those cases where you should be using PostMessage. 这是应该使用PostMessage的情况之一。

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

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