隐藏窗体制作托盘

综合网上的前辈的资料,自己经过琢磨整理代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AutoBackup
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.NotifyIcon Tray;
        
private System.Windows.Forms.ContextMenu TrayMenu;
        
private System.Windows.Forms.MenuItem menuItem1;
        
private System.Windows.Forms.MenuItem menuItem2;
        
private System.Windows.Forms.MenuItem menuItem3;
        
private System.Windows.Forms.MenuItem menuItem4;
        
private System.Windows.Forms.ImageList TrayImage;
        
private System.ComponentModel.IContainer components;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
#region Windows 窗体设计器生成的代码
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources 
= new System.Resources.ResourceManager(typeof(Form1));
            
this.Tray = new System.Windows.Forms.NotifyIcon(this.components);
            
this.TrayMenu = new System.Windows.Forms.ContextMenu();
            
this.menuItem1 = new System.Windows.Forms.MenuItem();
            
this.menuItem2 = new System.Windows.Forms.MenuItem();
            
this.menuItem3 = new System.Windows.Forms.MenuItem();
            
this.menuItem4 = new System.Windows.Forms.MenuItem();
            
this.TrayImage = new System.Windows.Forms.ImageList(this.components);
            
// 
            
// Tray
            
// 
            this.Tray.ContextMenu = this.TrayMenu;
            
this.Tray.Icon = ((System.Drawing.Icon)(resources.GetObject("Tray.Icon")));
            
this.Tray.Text = "AutoBackupServer";
            
this.Tray.Visible = true;
            
this.Tray.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Tray_MouseDown);
            
this.Tray.DoubleClick += new System.EventHandler(this.Tray_DoubleClick);
            
// 
            
// TrayMenu
            
// 
            this.TrayMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     
this.menuItem1,
                                                                                     
this.menuItem2,
                                                                                     
this.menuItem3,
                                                                                     
this.menuItem4}
);
            
// 
            
// menuItem1
            
// 
            this.menuItem1.Index = 0;
            
this.menuItem1.Text = "显示窗口";
            
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            
// 
            
// menuItem2
            
// 
            this.menuItem2.Index = 1;
            
this.menuItem2.Text = "隐藏窗口";
            
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            
// 
            
// menuItem3
            
// 
            this.menuItem3.Index = 2;
            
this.menuItem3.Text = "-";
            
// 
            
// menuItem4
            
// 
            this.menuItem4.Index = 3;
            
this.menuItem4.Text = "退出程序";
            
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
            
// 
            
// TrayImage
            
// 
            this.TrayImage.ImageSize = new System.Drawing.Size(1616);
            
this.TrayImage.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("TrayImage.ImageStream")));
            
this.TrayImage.TransparentColor = System.Drawing.Color.Transparent;
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.ClientSize = new System.Drawing.Size(292273);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            
this.Load += new System.EventHandler(this.Form1_Load);

        }

        
#endregion


        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            
this.Visible=false;
        }


        
private void Tray_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        
{
        
        }


        
private void menuItem1_Click(object sender, System.EventArgs e)
        
{
            
if(this.Visible==false)this.Visible=true;
        }


        
private void menuItem2_Click(object sender, System.EventArgs e)
        
{
            
if(this.Visible==true)this.Visible=false;
        }


        
private void menuItem4_Click(object sender, System.EventArgs e)
        
{
            
this.Close();
            Application.Exit();
        }


        
private void Tray_DoubleClick(object sender, System.EventArgs e)
        
{
            
this.Visible=true;
            
this.WindowState=System.Windows.Forms.FormWindowState.Maximized;
        }


        
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        
{
            
this.Visible=false;
            e.Cancel
=true;
        }




    }
posted @ 2005-05-30 19:23  泡面 @ 幸福  阅读(564)  评论(0编辑  收藏  举报