1

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.

RageRBoy
  • 37
  • 3

1 Answers1

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