I have a Console / Form hybrid application in C#, and at the moment, i have to rely on user32.dll to show/hide the console window. But I can't seem to find a way in which i can determine if the console window is hidden or visible (without storing the value myself)
Asked
Active
Viewed 1.4k times
4 Answers
22
The IsWindowVisible function retrieves the visibility state of the specified window.
C# Signature from pinvoke.net:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
-
2That will not check whenever the window is in fact visible. Read the msdn article. – devoured elysium May 14 '10 at 01:07
-
1I think you need [DllImport(USER32)] public static extern bool IsIconic(IntPtr hWnd); – Kate Sep 01 '14 at 11:09
-
2"Any drawing to a window with the WS_VISIBLE style will not be displayed if the window is obscured by other windows or is clipped by its parent window." - Which means this will return true regardless if it can be seen. – David Jan 18 '17 at 10:02
-
FYI (to get your window handle): `var source = new WindowInteropHelper(myWindow).Handle;` – Arsen Khachaturyan Dec 14 '20 at 18:01
-
how to set winForm Visible to true? at Program.cs, I try ShowWindow is Unsuccessful – Ruyut Jun 09 '21 at 05:38
1
Had the same issue now, solved it this way:
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point lpPoint);
var mainForm = this; // or any other form you like to check
bool windowIsInvisible =
WindowFromPoint(new Point(mainForm.Left, mainForm.Top)) != mainForm.Handle || // topleft invisible
WindowFromPoint(new Point(mainForm.Left + mainForm.Width - 1, mainForm.Top)) != mainForm.Handle || // topright invisible
WindowFromPoint(new Point(mainForm.Left, mainForm.Top + mainForm.Height - 1)) != mainForm.Handle || // downleft invisible
WindowFromPoint(new Point(mainForm.Left + mainForm.Width -1, mainForm.Top + mainForm.Height -1)) != mainForm.Handle; // downright invisible

Karsten
- 21
- 2
0
I use this function in a C# console application to determine if the program was launched with or without a console window visible (e.g. via System.Diagnostics.Process.Start()
with CreateNoWindow = true
).
public static bool IsConsoleVisible()
{
try
{
return Console.WindowHeight > 0;
}
catch (System.IO.IOException ex)
{
if (ex.Message.Contains("The handle is invalid."))
{
return false;
}
else
{
throw ex;
}
}
}
Perhaps this will apply.

Paul
- 3,725
- 12
- 50
- 86
0
I didn't have success with IsWindowVisible
.
turns out I needed IsIconic
which means "if window minimized"
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
public static bool GetIsWindowMinimized(IntPtr hWnd)
{
return IsIconic(hWnd);
}

Nben
- 127
- 7