Hello,
Recently I've been doing some Windows API programming and I came across a problem. I want to make a console program that accepts commands, when the command 'win' shows up it will create a window with some automated features.

Lets say I had a command sendfile [file-path] [where]. I'd like the GUI to allow me to browse the file, and select where from a list of some kind. But the problem I'm having is I'm unsure how to make it so when you close the GUI window it will simply hide and I can call it back through my console window. Currently I've managed to make it go away once but when you try win again it doesn't show up. I also made it so every time you type win it will create the window. That works but it wont save previous data on the forum because it is destroying it and recreating.

Does anyone know how to cancel the close event and make the window hide instead.?

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_QUIT:
            // cancel ?
            ShowWwindow(hwnd,SW_HIDE);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Thank you in advance for reading my long and useless explanation.

Dani AI

Generated

A few concrete points that explain why the window disappears permanently and how to keep it reusable.

The root problem is handling the wrong messages. WM_QUIT is not delivered to a window procedure; it’s posted to the thread’s message queue by PostQuitMessage and causes GetMessage to return 0 (ending the message loop). DefWindowProc handles WM_CLOSE by calling DestroyWindow, which leads to WM_DESTROY. To hide instead of destroy, intercept WM_CLOSE and avoid calling the default handler or DestroyWindow. Also avoid calling PostQuitMessage from WM_DESTROY unless the entire process should exit — that will terminate the message loop and effectively destroy any windows the thread created.

A minimal approach (inside the WindowProc) is to catch WM_CLOSE, hide the window and return 0 so the default close behavior doesn’t run:

case WM_CLOSE:
    ShowWindow(hwnd, SW_HIDE);
    return 0;

Create the GUI window once and keep its HWND (global/static). Before creating on the “win” command check that HWND is valid with IsWindow(hwnd); if valid call ShowWindow(hwnd, SW_SHOW) and bring it to front. If the window still won’t reappear, the usual causes are: the window was destroyed, the creating thread exited (Windows destroys windows when their creating thread ends), or PostQuitMessage ended the loop. was right that ShowWindow hides the window, and ’s suggestion to check Task Manager is useful for seeing whether the process/thread is still alive. Preserve UI state in memory or in the window’s user data so hiding/re-showing retains prior values. If the console thread needs to request show/hide on a window created on another thread, post a custom message to that window’s thread rather than manipulating window internals directly.

Recommended Answers

All 3 Replies

Member Avatar for Member #718140

The ShowWindow(hwnd,SW_HIDE) will hide the window without destroying it as you wanted, but to get it back you'll need to call ShowWindow(hwnd,SW_SHOW). You'll need a way to get hold of the keypress or whatever you're using to reactivate the window, which may be tricky.

Remember that once the window is hidden, keypresses will no longer come to the wndproc, but instead will go to whatever window was underneath.

It wasn't working, when I showed again it wouldn't show up. I believe the close event is still being called and the window's code destructs the window making it no longer usable.

Any other ideas?

Are you sure that the Window isn't destroyed? Check in task manager.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.