Hello,
I'm making an application in Visual C++ that hides/shows an application window from taskbar by clicking a button.

First problem (googled it) is that I don't know how to retrieve the information about a window state (SW_HIDE,SW_SHOWNORMAL, etc.). Is it hidden, minimized, normal or maximized? I only know how to set them. I want this information to toggle between hide/show states.

Second problem more difficult is that showing back a normal window maximized or normal works fine BUT if that window is used by a game I can't restore directly in the fullscreen video mode - I see only my screen as it is at the moment and no mouse pointer (as it would focus that window but does not retrieve the fullsceen game mode).

Here's my function for hiding from taskbar:

void hide(char* className, char* windowTitle)
{


			HWND hc = FindWindow(className,windowTitle);
			
		
			if(hc == NULL) MessageBoxA(NULL,"Cannot find the window","Error",MB_OK);
			else
			{
				ShowWindow(hc,SW_HIDE);
			    SetForegroundWindow(hc);

				MessageBoxA(NULL,"We retrieve the window to normal state","OK",MB_OK);
				ShowWindow(hc,SW_SHOWNORMAL);
				SetForegroundWindow(hc);

			}
}

Dani AI

Generated

Two key details explain why your toggle logic misfires. First, GetWindowPlacement does not tell you whether a window is hidden; its showCmd reports only the last shown state (normal/minimized/maximized). A hidden window will typically still report SW_SHOWNORMAL or SW_SHOWMAXIMIZED, so testing showCmd == SW_HIDE will never succeed. Use IsWindowVisible(hWnd) (or check WS_VISIBLE via GetWindowLongPtr(GWL_STYLE)) to decide hidden vs. shown, and initialize WINDOWPLACEMENT.length before calling GetWindowPlacement. For minimized and maximized checks, use IsIconic and IsZoomed. See GetWindowPlacement, IsWindowVisible, IsIconic, and IsZoomed.

Example toggle that preserves the previous show state when revealing a hidden window:

bool ToggleWindow(HWND h)
{
    if (!IsWindow(h)) return false;

    if (!IsWindowVisible(h)) {
        WINDOWPLACEMENT wp{};
        wp.length = sizeof(wp);
        if (GetWindowPlacement(h, &wp) && wp.showCmd == SW_SHOWMAXIMIZED)
            ShowWindow(h, SW_SHOWMAXIMIZED);
        else
            ShowWindow(h, SW_RESTORE);  // shows and unminimizes if needed
        SetForegroundWindow(h);         // best-effort; may be denied by OS
        return true;
    }

    ShowWindow(h, SW_HIDE);
    return true;
}

Note: bringing another process’s window to the foreground is restricted by the OS; SetForegroundWindow can fail unless focus rules are met or you coordinate via AllowSetForegroundWindow/COM equivalents. This is why games (especially exclusive fullscreen) sometimes do not immediately resume focus. See SetForegroundWindow and AllowSetForegroundWindow.

Recommended Answers

All 4 Replies

Try the function...

BOOL GetWindowPlacement( HWND hWnd,
WINDOWPLACEMENT *lpwndpl
);

Thanks but didn't help because I don't know how to use that WINDOWPLACEMENT variable.

I solved the fullscreen problem in games.

I only want somehow to get the current state of that window - SW_HIDE, SW_NORMAL, SW_MAXIMIZE, SW_MINIMIZE. These are integer values.

Your reply was really helful :) . For that I was searching. Just one problem if you want to help me - hope I don't buzz you too much, I'm little newbie in winapi. This is my function for toggle:

void hide(char* className, char* windowTitle)
{


			HWND hc = FindWindow(className,windowTitle);
			
		
			if(hc == NULL) MessageBoxA(NULL,"Cannot find the window","Error",MB_OK);
			else
			{

				
				WINDOWPLACEMENT wd;
				UINT nCurShow;

				if (GetWindowPlacement( hc, &wd ))
				{
					nCurShow = wd.showCmd;
					if(nCurShow==SW_HIDE)
					{
					    ShowWindow( hc, SW_SHOWMAXIMIZED);
						SetForegroundWindow(hc);
						
						MessageBoxA(NULL,"maximized now","info",MB_OK);
					}
					else if(nCurShow==SW_SHOWMAXIMIZED || nCurShow==SW_SHOWNORMAL || nCurShow==SW_SHOWMINIMIZED)
					{	
						ShowWindow( hc, SW_HIDE );
					    SetForegroundWindow(hc);
				
						MessageBoxA(NULL,"hidden now","info",MB_OK);
					}

				}
				else
				{
						ShowWindow( hc, SW_SHOWMAXIMIZED );
						SetForegroundWindow(hc);
				}
			
			}
}

It's just it always enter this statement:
else if(nCurShow==SW_SHOWMAXIMIZED || nCurShow==SW_SHOWNORMAL || nCurShow==SW_SHOWMINIMIZED) as if it can't detect that the window is currently hidden. (and it really is hidden). Again, thanks.

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.