Code Behind the Control Box Minimise button

oafc0000

Newcomer
Joined
Jan 28, 2003
Messages
18
Hi guys and girls

How do you put code behind the Form Control Box minimise and maximise buttons.

Basically I want to minimise the Form so it goes into the system tray but having to do it with a button at the moment coz I cant do it using the forms Control Box.

Any suggestions?

Thanks
 
Yep - still the Resize event. You want the form's WindowState property, as in:

C#:
private void Form1_Resize(object sender, System.EventArgs e)
{
	if(this.WindowState==FormWindowState.Minimized)
	{
		// Do your custom minimize to tray
		Debug.WriteLine("min");
	}
}
[edit]fixed code tags to use the [cs] [/cs] tags[/edit]
-Nerseus
 
Last edited by a moderator:
Hey guys, I've been fighting with minimizing to the tray for a bit, here's what I've got:


Code:
this.TrayIcon = new System.Windows.Forms.NotifyIcon(this.components);

Code:
this.TrayIcon.ContextMenu = this.TrayMenu;
this.TrayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("TrayIcon.Icon")));
this.TrayIcon.Text = "Remote File Manager";
this.TrayIcon.Visible = true;



Now, when I minimize the application, it shows up on the botton left corner of the desktop:

example.png


How can I get rid of that? Thanks for reading ^.^
 
I've never done Tray Icons before.. so this is just a shot in the dark. :) Would setting the form to visible = false work?

edit;
Browsing about (curious about Tray Icons now..) I found this;
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp06102002.asp

scroll down to the bottom...

The first two steps to do this are easy. You set the ShowInTaskbar property to false and the WindowState to minimized, and the window vanishes. That's not the whole story, however, as you can still get to the window using the ALT+TAB key combination.

After a few Google searches, I came across the code to fix this problem. You need to tell the window that it's a tool window, and then it isn't put into that list. I did that with a bit of code that calls out to the Win32® SendMessage() API.

When I was writing this up, I was complaining to a co-worker that I had to do this to make the window disappear. We did some more searching and found that one of the border styles you can set for a form is FixedToolWindow. I used that code instead of my SendMessage() code, and the window was gone. I thought that was the solution, but it turns out that if you bring up another window in the application, the main window will reappear. However, the main window won't show up again if you use the SendMessage() solution, so that's what I've kept. If you aren't going to bring up other windows, setting the border style should work fine.

 
Last edited:
Turning it to FixedToolWindow didn't work and I can't seem to find any of this code for SendMessage() to use as an example. You can't be serious in telling me something so seemingly simple is such a difficult feat for a programming language like c# =p
 
Okay, so maybe it was fairly easy. And the visible thing did work. I thought it was only available for form items, not the form itself.

this.Visible = false;
 
Back
Top