Jim Tat

Jim Tat

  • NA
  • 52
  • 47.2k

How To Check If A Window Is Minimized In C#

Sep 6 2016 6:06 AM
Hello, I need to check if a window is minimized in c# Here is what I did:
  1. {  
  2.     class RestoreWindow  
  3.     {  
  4.         [DllImportAttribute("user32.dll")]  
  5. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
  6.    
  7. [DllImportAttribute("user32.dll")]  
  8. public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);  
  9.    
  10. [DllImportAttribute("user32.dll")]  
  11. public static extern bool SetForegroundWindow(IntPtr hWnd);  
  12.    
  13. [DllImport("user32.dll")]  
  14. [return: MarshalAs(UnmanagedType.Bool)]  
  15. static extern bool IsIconic(IntPtr hWnd);  
  16.    
  17. public static void MaximizeWindowIfMinized(string windowName,Form f1)  
  18. {  
  19.     try  
  20.     {  
  21.         var instance = FindWindow(null, windowName);  
  22.    
  23.         if (IsIconic(instance))  
  24.         {  
  25.             Button btn = new Button();  
  26.             btn.Width = 10;  
  27.             btn.Height = 10;  
  28.             f1.Controls.Add(btn);  
  29.             MessageBox.Show("Window is minimized");  
  30.    
  31.            // ShowWindow(instance, 1);  
  32.             //SetForegroundWindow(instance);  
  33.         }  
  34.     }  
  35.     catch (Exception ex)  
  36.     {  
  37.         MessageBox.Show(ex.Message);  
  38.     }  
  39. }  
  40.     }  
  41. }  
But when I compile nothing happen , do you have any idea ?  

Answers (6)