5 Replies - 856 Views - Last Post: 06 September 2016 - 05:27 AM Rate Topic: -----

#1 SniperCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-September 16

How to check if a window is minimized in C#

Posted 06 September 2016 - 02:16 AM

Hello, I need to check if a window is minimized in c# Here is what I did:

{
    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 ?

Is This A Good Question/Topic? 0
  • +

Replies To: How to check if a window is minimized in C#

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: How to check if a window is minimized in C#

Posted 06 September 2016 - 03:08 AM

Looks like basic debugging. We have nothing to go on. Could be wrong window name. Could be you just didn't call the method. Could be a dozen things.
See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2016
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 SniperCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-September 16

Re: How to check if a window is minimized in C#

Posted 06 September 2016 - 03:19 AM

Everything is correct, I called the method in a timer , even this nothing is showing when I minimize the window.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5921
  • View blog
  • Posts: 20,243
  • Joined: 05-May 12

Re: How to check if a window is minimized in C#

Posted 06 September 2016 - 04:01 AM

View PostSniperCode, on 06 September 2016 - 05:16 AM, said:

But when I compile nothing happen , do you have any idea ?

Yes. There is a difference between compiling the code and running the code. Of course nothing happens if you just compile it vs. running it.

View PostSniperCode, on 06 September 2016 - 06:19 AM, said:

Everything is correct, I called the method in a timer , even this nothing is showing when I minimize the window.


He asked you to debug the code. As there is a difference between compiling the code and running the code, there is a difference between running the code and debugging the code.

Did you set breakpoints? Do they get hit? Did you inspect the variables and return values? What were they?
Was This Post Helpful? 0
  • +
  • -

#5 SniperCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-September 16

Re: How to check if a window is minimized in C#

Posted 06 September 2016 - 04:38 AM

I find out another way to handle this but in the timer it is looping :
Here is the code :

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);

        private void timer1_Tick(object sender, EventArgs e)
        
        {

            Process[] procs = Process.GetProcesses();

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

                    if (placement.showCmd == ShowWindowCommands.Minimized)
                       MessageBox.Show("Minimizing");


                }
            }

        }

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5921
  • View blog
  • Posts: 20,243
  • Joined: 05-May 12

Re: How to check if a window is minimized in C#

Posted 06 September 2016 - 05:27 AM

I'm glad that you found an alternate solution.

It looks like you'd rather throw code at the screen to see what sticks instead of debugging a problem. Good luck with that. May your Google-fu always be strong that you'll find answers that somebody else has figured out instead of working on your own deductive and analytical skills to solve problems yourself.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1