How To Check If A Window Is Minimized?

Tatsu

New member
Joined
Jun 30, 2016
Messages
4
Programming Experience
Beginner
Hello, I need to check if a window is minimized in c# Here is what I did:
C#:
{
    class RestoreWindow
    {
        [DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
 
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
 
public static void MaximizeWindowIfMinized(string windowName,Form f1)
{
    try
    {
        var instance = FindWindow(null, windowName);
 
        if (IsIconic(instance))
        {
            Button btn = new Button();
            btn.Width = 10;
            btn.Height = 10;
            f1.Controls.Add(btn);
            MessageBox.Show("Window is minimized");
 
           // ShowWindow(instance, 1);
            //SetForegroundWindow(instance);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
    }
}
But when I compile nothing happen , do you have any idea ?
 
It is inaccurate to say "nothing happen". You may not have seen anything specific happen as the user but the code certainly did something. As the developer, it's up to you to determine exactly what the code did and where exactly that differed from your expectation. That's what debugging is for.

Do you know how to debug? If so, do it. If not, now is the time to learn. You would start by placing a breakpoint (F9) at the head of that method. When the method is called, execution will break at that point. You can then step through the code line by line (F10), allowing you to determine whether execution takes the path you expect. You can also use the Autos, Locals and Watch windows and various other tools to examine the state at each step, allowing you to determine whether variables contain the values you expect, etc.

That will allow to determine exactly what went wrong and from that you can determine why. If you can't determine why on your own, then you can post here with ALL the relevant information and we might have a chance of determining the issue. You'll want to examine the handle returned by FindWindow and then use some tool like Spy++ to make sure that it is the handle of the window you were looking for.
 
Hello, my aim is my c# application is when I an exe file is open then a button is created on my winform and when the window of the exe is closed the button disappear. the function of the button will be if we click on the button the window is minimized then the window will be restored. Here is what I tried :

private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(hwnd, ref placement);
return placement;
}

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowPlacement(
IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
internal enum ShowWindowCommands : int
{
Hide = 0,
Normal = 1,
Minimized = 2,
Maximized = 3,
}

[Serializable]
[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPLACEMENT
{
public int length;
public int flags;
public ShowWindowCommands showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
public static void MaximizeWindowIfMinized(string windowName,Timer tim,FlowLayoutPanel f1)
{
tim.Start();
Button btn = new Button();
Process[] procs = Process.GetProcesses();
try
{


foreach (Process proc in procs)
{
if (proc.ProcessName.Contains(windowName))
{
var placement = GetPlacement(proc.MainWindowHandle);
Console.WriteLine(placement.showCmd.ToString());

if (placement.showCmd == ShowWindowCommands.Normal)
{

btn = new Button();
// btn.Width = 150;
// btn.Height = 120;
f1.Controls.Add(btn);
// btn.Left = 500;
// btn.Visible = true;
// btn.Click += button1_Click(windowName);
tim.Stop();
}
tim.Start();

if (placement.showCmd == ShowWindowCommands.Hide)
{
btn.Visible = false;
tim.Stop();

}

}
}
}
catch
{

}

}
 
Back
Top Bottom