I have tried many functions, such as ShowWindow
& IsWindowVisible
to at least try to give the result if the window is minimized, let alone restore it. These functions constantly return false whether the window is minimized or not.
I have also tried using GetWindowPlacement
with SetWindowPlacement
with no success.
My HWND
finds Chrome with FindWindow(TEXT("Chrome_WidgetWin_1"), NULL);
which is successful, but I want to test/restore the window if it's minimized and these past 10 hours has nothing to show for it.
1 Answer
Chrome has an invisible window with the same name. The invisible window simply needs to be skipped.
void show(HWND hwnd)
{
//We can just call ShowWindow & SetForegroundWindow to bring hwnd to front.
//But that would also take maximized window out of maximized state.
//Using GetWindowPlacement preserves maximized state
WINDOWPLACEMENT place;
memset(&place, 0, sizeof(WINDOWPLACEMENT));
place.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hwnd, &place);
switch (place.showCmd)
{
case SW_SHOWMAXIMIZED:
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
break;
case SW_SHOWMINIMIZED:
ShowWindow(hwnd, SW_RESTORE);
break;
default:
ShowWindow(hwnd, SW_NORMAL);
break;
}
SetForegroundWindow(hwnd);
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR cmdline, int nshow)
{
const wchar_t *classname = L"Chrome_WidgetWin_1";
HWND hwnd = NULL;
for (;;)
{
hwnd = FindWindowEx(0, hwnd, classname, 0);
if (!hwnd) break;
//skip Chrome's invisible winodw
if (IsWindowVisible(hwnd))
{
wchar_t buf[260];
GetWindowText(hwnd, buf, 260);
OutputDebugString(buf);
OutputDebugString(L"\n");
show(hwnd);
break;
}
}
return 0;
}
-
In theory that should work. However I have tested this method without success. I even had no other applications running except chrome(minimized) and of course, the program I'm making. Would also like to note, for some reason
IsIconic
always returns false for me. Commented Apr 24, 2015 at 2:22 -
I see what you mean. Each chrome window has at least one child, if the top window won't cooperate then go after the child. Commented Apr 24, 2015 at 3:15
-
Thank you for the explanation of why chrome won't cooperate, and this brute force kind of method is exactly what I'm looking for. Again, thank you so much. Commented Apr 24, 2015 at 3:31
-
Thanks Kilo King. I made another edit which looks smoother. Actually, I am still not sure why Chrome is acting weird or how its windows are arranged, but at least it seems to work consistently. Commented Apr 24, 2015 at 6:00
-
1@IInspectable Windows documentation is not helpful here. The problem was Chrome has invisible windows. That's solved. I used @Kilo King's suggestion of
WINDOWPLACEMENT
, so now Window is brought to front correctly. I had put in some nonsense earlier forSetActiveWindow
etc. it was out of frustration, I removed that. Commented Apr 24, 2015 at 16:35
ShowWindowAsync(chrome, SW_SHOWMAXIMIZED);
it actually worked. It brought the window out of minimize. But running it a few more times, it stopped working?OpenIcon()
? Of course, you saidIsIconic()
is returning false...