I'm making a c# overlay for a game. It appears on the game but it won't hide if the game is minimized so I'm wondering if there's any way to check if a window is minimized/out of focus. I searched about it on google but I couldn't find anything useful.
Asked
Active
Viewed 690 times
1
-
[Chcek this for lost focus](https://stackoverflow.com/questions/570021/forms-lost-focus-in-c-sharp/570045) I think it will be help full – Avinash Reddy Apr 15 '19 at 05:15
-
No no, I'm checking for a if a window is focused, not a form. – RageRBoy Apr 15 '19 at 05:17
-
(form is minimized)[https://stackoverflow.com/questions/1052913/how-to-detect-when-a-windows-form-is-being-minimized] – Avinash Reddy Apr 15 '19 at 05:27
-
are you talking about an MDI container? what exactly do you mean by window? – Mong Zhu Apr 15 '19 at 05:35
-
An application window. For example: chrome – RageRBoy Apr 15 '19 at 05:44
-
so basically you want to find out whether there is another application window is topmost ? – Mong Zhu Apr 15 '19 at 06:09
-
Exactly! I want to know how. – RageRBoy Apr 15 '19 at 06:21
-
would [this post](https://stackoverflow.com/questions/35315788/how-do-i-get-the-topmost-status-of-every-window-in-c-sharp) be of any help ? – Mong Zhu Apr 15 '19 at 06:52
1 Answers
2
To check the state of a window (normal, maximized or minimized) when you have the windows caption name or the windows handle, you can use this code:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsZoomed(IntPtr hWnd);
enum WinState
{
None,
Maximized,
Minimized,
Normal,
}
private static WinState GetWindowState(IntPtr hWnd)
{
WinState winState = WinState.None;
if (hWnd != IntPtr.Zero)
{
if (IsIconic(hWnd))
{
winState = WinState.Minimized;
}
else if (IsZoomed(hWnd))
{
winState = WinState.Maximized;
}
else
{
winState = WinState.Normal;
}
}
return winState;
}
private static WinState GetWindowState(string windowCaption)
{
return GetWindowState((IntPtr)FindWindow(null, windowCaption));
}
Usage:
string windowCaption = "New Tab - Google Chrome";
WinState winState = GetWindowState(windowCaption);

Eliahu Aaron
- 4,103
- 5
- 27
- 37