friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:43:34

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Minimise your application to the tray [C#]

Does exactly what it says on the tin.


Code:

// this variable goes in the main scope of your class
NotifyIcon TrayIcon = new NotifyIcon(); // create the icon to show in the tray

// put this in the constructor for your form
this.TrayIcon.Icon = new Icon(".\\icon.ico"); // apply an icon to it
this.TrayIcon.Visible = false; // make sure it isn't displaying by default
this.Resize += new EventHandler(Form_Resize); // add a resize event
this.TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);

// then put these 2 methods in your class
void Form_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    { // if the form has been minimised
        this.Hide(); // hide the form
        this.TrayIcon.Visible = true; // display the tray icon
    }
}

void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
    this.TrayIcon.Visible = false; // hide the tray icon
    this.Show(); // display the window
    this.WindowState = FormWindowState.Normal; // reset the form state from minimised
}

/** EXAMPLE USAGE **/
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DIC
{
    public partial class MainForm : Form
    {
        // this variable goes in the main scope of your class
        NotifyIcon TrayIcon = new NotifyIcon(); // create the icon to show in the tray
        public MainForm()
        {
            InitializeComponent();
            this.Icon = new Icon(".\\icon.ico");
            
            // put this in the constructor for your form
            this.TrayIcon.Icon = new Icon(".\\icon.ico"); // apply an icon to it
            this.TrayIcon.Visible = false; // make sure it isn't displaying by default
            this.Resize += new EventHandler(Form_Resize); // add a resize event
            this.TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);
        }

        void Form_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            { // if the form has been minimised
                this.Hide(); // hide the form
                this.TrayIcon.Visible = true; // display the tray icon
            }
        }
        
        void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.TrayIcon.Visible = false; // hide the tray icon
            this.Show(); // display the window
            this.WindowState = FormWindowState.Normal; // reset the form state from minimised
        }
    }
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 0.025 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)