简体   繁体   中英

SendMessage() and PostMessage() proper usage

I've got a problem with using SendMessage() and PostMessage() properly. The thing what i'm trying to do is 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.

The case label case WM_USER + 1: is nested in the inner case statement (that switches on wmId ). 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: .

The above also applies to 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. This is one of those cases where you should be using PostMessage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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