windows - Win32 call SendMessage rekursivt

Indlæg af Hanne Mølgaard Plasc

Problem



Som den nedenfor viste kode for den tilpassede besked WM\_TESTMSG -handler kalder den SendMessage for at sende den samme besked. Og jeg tror, ​​at der vil være en Stack Overflow-undtagelse , og MessageBeep bliver ikke kaldt, fordi SendMessage vil kalde WindowProc direkte. Men når jeg tester koden, fungerer det godt!
Hvorfor ingen undtagelse? Tak!


#include <Windows.h>

#define WM\_TESTMSG (WM\_USER + 0x100)
LRESULT CALLBACK MainWndProc(IN HWND hWnd, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam)
{
    LRESULT ret = 0;
    switch (uMsg)
    {
        case WM\_CREATE:
        {
            PostMessage(hWnd, WM\_TESTMSG, 0, 0);
            break;
        }

        case WM\_TESTMSG:
        {
            SendMessage(hWnd, WM\_TESTMSG, 0, 0);
            MessageBeep(MB\_ICONERROR);  // I think the program can't run to here
            break;
        }

        case WM\_CLOSE:
        {
            DestroyWindow(hWnd);
            break;
        }
        case WM\_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }

        default:
        {
            ret = ::DefWindowProc(hWnd, uMsg, wParam, lParam);
            break;
        }
    }

    return ret;
}



BOOL RegisterWndClass(IN HINSTANCE hInstance)
{
    WNDCLASSEX wndClassEx;

    ZeroMemory(&wndClassEx, sizeof(wndClassEx));
    wndClassEx.cbSize = sizeof (wndClassEx);
    wndClassEx.style = CS\_HREDRAW | CS\_VREDRAW;
    wndClassEx.lpfnWndProc = MainWndProc;
    wndClassEx.cbClsExtra = 0;
    wndClassEx.cbWndExtra = 0;
    wndClassEx.hInstance = hInstance;
    wndClassEx.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI\_HAND));
    wndClassEx.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC\_ARROW));
    wndClassEx.hbrBackground = (HBRUSH)GetStockObject(DKGRAY\_BRUSH);
    wndClassEx.lpszMenuName = NULL;
    wndClassEx.lpszClassName = TEXT("MyMainWnd");
    wndClassEx.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(IDI\_ERROR));

    return ::RegisterClassEx(&wndClassEx);
}


int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    RegisterWndClass(hInstance);

    HWND hMainWnd = CreateWindowEx(0, L"MyMainWnd", L"MainWnd", WS\_OVERLAPPEDWINDOW | WS\_VISIBLE, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, NULL, 0, hInstance, 0);

    ShowWindow(hMainWnd, SW\_SHOWNORMAL);
    UpdateWindow(hMainWnd);

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

    return msg.wParam;
}

Bedste reference