Gabriel
Gabriel

Reputation: 83

.NET (C#) Window Minimize Event

Hey, I'm really stuck with my project here... I need to know when any open window has been minimized / restored and Handle the event in my own App. Any ideas?

Edit: Musigenesis is right, i do want to know when OTHER applications are minimized/restored

Upvotes: 3

Views: 5125

Answers (3)

Darryl Bassett
Darryl Bassett

Reputation: 1

//For cases where you create the window such as:
  protected         Form           gv_AO_frmControlWindow = null;
    gv_AO_frmControlWindow = new Form();
    * * * code to add buttons and such
  // Add handler that is executed when window is minimized/maximized
  gv_AO_frmControlWindow.Resize += new EventHandler(gv_AO_frmControlWindow_Resize);
  gv_AO_frmControlWindow.ShowInTaskbar = false;
  // Show window
  gv_AO_frmControlWindow.showDiaglog.


    // ****************************************************************
    private void gv_AO_frmControlWindow_Resize(object sender, EventArgs e) {
      if ( gv_AO_frmControlWindow.WindowState == FormWindowState.Minimized ) {
        notifyIcon.Visible = true;
        }
      else {
        notifyIcon.Visible = false;
        }
      return;
      } // END OF gv_AO_frmControlWindow_Resize()
    // ****************************************************************
    private void notifyIcon_MouseClick(object sender, MouseEventArgs e) {
      gv_AO_frmControlWindow.WindowState = FormWindowState.Normal;  
      return;
      } // END OF notifyIcon_MouseClick()

//Assuming you wanted a system tray icon to maximize window:
    private System.Windows.Forms.NotifyIcon notifyIcon    = new NotifyIcon();

      notifyIcon.Icon = new Icon( "C:\\SysTray.ico");
      notifyIcon.Text = "MyWindow";
      notifyIcon.MouseClick += new MouseEventHandler(this.notifyIcon_MouseClick);

// Hope this helps.
// Darryl H. Bassett

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75376

I think you would need to use the SetWindowsHookEx Win32 API function (along with a few others). Basically, you would iterate through all open windows in the OS and hook into their resizing events.

Obligatory comment: are you sure you need to do this? While I think this is theoretically possible, it sounds like a pretty bad idea, and counter to the way applications in Windows are supposed to behave.

Update: I think "Show Desktop" in Windows works kind of like this, except that it iterates through all the open windows and then uses SendMessage to minimize them if open (total guess on my part).

Update 2: this is a tough one, and I'm very curious to know how this could be done (I'm 100% sure that it is possible). I'll keep an eye on this question, and if no one comes up with an answer in the next day or two, I'll post it again and offer a bounty on it (you could do that, but you need to have some reputation points of your own to offer as a bounty).

Upvotes: 6

Traveling Tech Guy
Traveling Tech Guy

Reputation: 27849

Assuming you're using Windows Forms, you can handle the OnSizeChanged event, and test the WindowState

Upvotes: 2

Related Questions