Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

DestroyWindow behavior

$
0
0

Hi,

I am getting strange result while executing below code.


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

    switch (message)
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_ABOUT:
                   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
                   break;
                case IDM_EXIT:
                   DestroyWindow(hWnd);
                   break;
                default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            RECT rt;
            GetClientRect(hWnd, &rt);
            DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
            EndPaint(hWnd, &ps);
            break;
        case WM_LBUTTONDOWN:
            ::PostMessage(hWnd, WM_USER + 1, 0, 0);
            ::PostMessage(hWnd, WM_USER + 1, 0, 0);
            ::PostMessage(hWnd, WM_USER + 1, 0, 0);
            ::DestroyWindow(hWnd);
            break;
        case (WM_USER + 1):
            OutputDebugString("\n(WM_USER + 1)");
            break;
        case WM_DESTROY:
            OutputDebugString("\n(WM_DESTROY)");
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

When window gets WM_LBUTTONDOWN, I am sending 3 User messages and then I am calling ::DestroyWindow(hWnd). I am getting WM_DESTROY message immediately.

Now my question is that after destroying window, what happens for the pending messages?

In my application sometimes I am getting WM_USER message even if i have destroyed the window. I search in MSDN for DestroyWindow(), but I did not understand how WINDPROC for destryed window can be called after DestroyWindow(). Please Help to understand the exact behavior.

Regards,

Ankit


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>