Amit
Amit

Reputation: 703

Destroying the current window and create a new one

I have a program that is written in WinAPI. I have the Login Window, and after that, the actual program window. I want to destroy the login window, and create the new window..

I've been using this :

Destroying the program :

    DestroyWindow(MainHwnd);

and the WndProc of the window (of MainHwnd's window):

LRESULT Client::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
    case WM_CREATE:
    break;

    case WM_COMMAND:
    break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
    break;

    case WM_DESTROY:
        PostQuitMessage(1);
    break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
}

return 0;
}

And the people here in StackOverflow told me in my previous question, that I a'int destroying the window currently, and I'm causing a Stack Overflow which ends where the window is destroyed (the program stills run - so it looks like everything is working as planed), but I do not want to use bad programing (specialy causing stack overflow to destroy a window haha)

So, how can I destroy a window correctly?

Also, some times, when I use DestroyWindow(MainHwnd) it gets inside both WM_DESTROY and WM_CLOSE (in the current WndProc I have posted above).. Is this related to the Stack Overflow problem ?

Btw - I also know how to use Windows Forms in #C, I'm trying to write something like : this.Close(); which closes the current window (maybe that makes my question more clear)...

Thanks!

Upvotes: 0

Views: 360

Answers (1)

From microsoft about the WM_CLOSE:

An application can prompt the user for confirmation, prior to destroying a window, by processing the WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice. By default, the DefWindowProc function calls the DestroyWindow function to destroy the window.

So calling DestroyWindow(hwnd); or not is the same.

LRESULT Client::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            break;

        case WM_COMMAND:
            break;

        case WM_CLOSE:
            //DestroyWindow(hwnd);
            break;

        or

        case WM_CLOSE:
            DestroyWindow(hwnd);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        default:
           return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

see an example closing window

Basically is what i said.

valter

Upvotes: 1

Related Questions