无痕客

落花无情,流水无痕……

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 C#编写最小化时隐藏为任务栏图标的 Window appllication.

  
  1.设置WinForm窗体属性showinTask=false
  
  2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。
  
  3.添加窗体最小化事件(首先需要添加事件引用):

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

//上面一行是主窗体InitializeComponent()方法中需要添加的引用

private void Form1_SizeChanged(object sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
}
  4.添加点击图标事件(首先需要添加事件引用):

private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
  
  5.可以给notifyIcon添加右键菜单:

主窗体中拖入一个ContextMenu控件NicontextMenu,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.NicontextMenu = new System.Windows.Forms.ContextMenu();
this.menuItem_Hide = new System.Windows.Forms.MenuItem();
this.menuItem_Show = new System.Windows.Forms.MenuItem();
this.menuItem_Aubot = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();


this.notifyIcon1.ContextMenu = this.NicontextMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject( "NotifyIcon.Icon ")));
this.notifyIcon1.Text = " ";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);


this.NicontextMenu.MenuItems.AddRange(

new System.Windows.Forms.MenuItem[]
{
this.menuItem_Hide,
this.menuItem_Show,
this.menuItem_Aubot,
this.menuItem_Exit
}
);

//
// menuItem_Hide
//
this.menuItem_Hide.Index = 0;
this.menuItem_Hide.Text = "隐藏 ";
this.menuItem_Hide.Click += new System.EventHandler(this.menuItem_Hide_Click);
//
// menuItem_Show
//
this.menuItem_Show.Index = 1;
this.menuItem_Show.Text = "显示 ";
this.menuItem_Show.Click += new System.EventHandler(this.menuItem_Show_Click);
//
// menuItem_Aubot
//
this.menuItem_Aubot.Index = 2;
this.menuItem_Aubot.Text = "关于 ";
this.menuItem_Aubot.Click += new System.EventHandler(this.menuItem_Aubot_Click);
//
// menuItem_Exit
//
this.menuItem_Exit.Index = 3;
this.menuItem_Exit.Text = "退出 ";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);

protected override void OnClosing(CancelEventArgs e)
{
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
protected override void OnClosing(CancelEventArgs e)
{
//this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}

private void CloseCtiServer()
{
timer.Enabled = false;
DJ160API.DisableCard();
this.NotifyIcon.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}

private void HideCtiServer()
{
this.Hide();
}

private void ShowCtiServer()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();

}
private void CtiManiForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.CloseCtiServer();
}

private void menuItem_Show_Click(object sender, System.EventArgs e)
{
this.ShowCtiServer();
}

private void menuItem_Aubot_Click(object sender, System.EventArgs e)
{

}

private void menuItem_Exit_Click(object sender, System.EventArgs e)
{
this.CloseCtiServer();
}

private void menuItem_Hide_Click(object sender, System.EventArgs e)
{
this.HideCtiServer();
}

private void CtiManiForm_SizeChanged(object sender, System.EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.HideCtiServer();
}
}
private void notifyIcon1_DoubleClick(object sender,System.EventArgs e)
{
this.ShowCtiServer();
}
转载自:
http://www.tzwhx.com/newOperate/html/1/12/122/8784.html
 
  • C# 托盘程序编写
  • 作者:clown  文章来源:博客园  发布日期:2009-11-17 
  • 我使用的环境:Visual Studio 2005 Professoinal Edition
    注意:Windows窗体生成器生成的代码中不能加注释,即使加了注释也会被自动去掉!
    使用NotifyIcon控件,该控件的作用是程序运行时在Windows任务栏右侧的通知区域中(任务栏)显示图标。 使用contextMenuStrip控件,该控件可以关联到其它控件,作用是当右击关联的控件时显示菜单。 在NotifyIcon1的属性列表中的contextMenuStrip的下拉列表中选择你刚才创建的contextMenuStrip1菜单。你的托盘程序就拥有一个菜单了。 接下来的与通常图形界面程序的编写相同,首先在[设计]窗口中设计你的界面,然后双击做好的菜单或按钮进入代码窗口写对应的代码。 接下来是一些托盘程序所应具有的一些特别功能的代码
    为托盘程序设置双击托盘图标显示/隐藏窗口
    首先是在Windows 窗体设计器生成的代码中为托盘图标增加双击事件,具体做法是在具有托盘图标的Form的designer.cs中找到notifyIcon的部分,加入语句

    1this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

     其作用是重载notifyIcon1的双击事件,为其添加方法notifyIcon1_DoubleClick(注意,这里的notifyIcon1和notifyIcon1_DoubleClick可以由你自己设定,可能与这里不同)。
    接着我们来实现notifyIcon1_DoubleClick方法,在Form的Class中写方法:

      1private void notifyIcon1_DoubleClick(object sender, EventArgs e) 
     2
     3this.Show(); 
     4if (this.WindowState == FormWindowState.Minimized) 
     5this.WindowState = FormWindowState.Normal; 
     6else if (this.WindowState == FormWindowState.Normal) 
     7this.WindowState = FormWindowState.Minimized; 
     8this.Activate(); //激活窗体并为其赋予焦点 
     9}
     
    10

     最小化时隐藏窗体(隐藏任务栏上的项目)
    首先同样是修改窗体设计器自动生成的代码,在Form1(假设,可能不同)中增加语句
     

    1 this.Resize += new System.EventHandler(this.Form1_Resize);

     然后实现Form1_Resize方法:

     1private void Form1_Resize(object sender, System.EventArgs e) 
    2
    3if (this.WindowState == FormWindowState.Minimized) 
    4
    5this.Hide(); 
    6}
     
    7}

     将关闭按钮重载为最小化

      1protected override void OnFormClosing(FormClosingEventArgs e) 
     2
     3if (!CloseTag) 
     4
     5this.WindowState = FormWindowState.Minimized; 
     6e.Cancel = true
     7}
     
     8else 
     9e.Cancel = false
    10base.OnFormClosing(e); 
    11}
     
    12

     这里需要说明的是,我的Form1有一个Bool型私有变量CloseTag,另外我的程序有一个关闭按钮,该按钮才是真正的关闭程序。我的做法是当使用我的关闭按钮时将CloseTag设为True,否则CloseTag为false。这样就做到了完整的关闭重载。

  • posted on 2009-12-07 21:38  无痕客  阅读(2016)  评论(0编辑  收藏  举报