【C#】WinForm中如何让窗口最小化时直接最小化到托盘,后台运行

详细代码,谢谢

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();
}

参考资料:MSDN

温馨提示:内容为网友见解,仅供参考
第1个回答  2009-09-22
3句话,搞定,收分。
from1.Visible = false;//隐藏窗口
notifyIcon1.Visible = true;//显示控件
//不过要显示,notifyIcon1托盘控件,要先指定它的Icon属性,搞定了。

【C#】WinForm中如何让窗口最小化时直接最小化到托盘,后台运行
1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。3.添加窗体最小化事件(首先需要添加事件引用):代码如下:this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);\/\/上面一行是主窗体InitializeComponent()方法中需要添加的...

C#winform中怎么将程序最小化到系统托盘
1.在form中添加一个NotifyIcon控件 2.把87.ico这个图标放在binDebug目录下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 将程序最小化到系统托盘 { public partial c...

C#winform如何最小化主窗口
1、首先输入代码:#region 私有方法 处理窗体的 显示 隐藏 关闭(退出)private void ExitMainForm(){ if (MessageBox.Show("您确定要退出化验数据接收程序吗?", "确认退出",MessageBoxButtons.OKCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.OK){ this.notifyIcon....

C#winform怎样实现托盘
双击托盘,窗体还原;右击窗体,出现托盘菜单,实现最小化,最大户,还原,退出等。这样的功能C#winform怎样实现呢 ?实现 WinForm中托盘菜单由NotifyIcon控件来实现,右键菜单由contextMenuStrip来实现,我们将二者相关联,即可实现我们所期望功能的托盘程序。添加控件 我们在需要托盘的form界面上拖入NotifyIcon和...

C# 如何将某一个应用程序最小化到任务托盘!
如果不是:这要取决于你使用的应用程序有没有最小化到托盘的功能.如果是:C#编写最小化时隐藏为任务栏图标的 Window appllication.1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。3.添加窗体最小化事件(首先需要添加事件引用):this....

C# winfrom开发中如何实现在程序已最小化到系统托盘
用notifyIcon控件可以实现,可以更换托盘的图标,不过图片格式为.ico所以一般的jpg图片要转换成ico才可用

c#窗体点击关闭按钮怎么最小化到托盘中而不退出程序?
region窗体最小化到状态栏\\x0d\\x0aprivatevoidMainForm_FormClosing(objectsender,EventArgse)\\x0d\\x0a{\\x0d\\x0aif(this.WindowState==FormWindowState.Minimized)\\x0d\\x0a{e.cancel=true;\/\/取消关闭窗体事件\\x0d\\x0anotifyIcon1.Visible=true;\\x0d\\x0athis.Hide();\\x0d\\x0athis....

c#窗体点击关闭按钮怎么最小化到托盘中而不退出程序?
可以在窗体的Closing事件中进行操作,大约如下:privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e){ \/\/ 取消关闭窗体 e.Cancel = true;\/\/ 将窗体变为最小化 this.WindowState = FormWindowState.Minimized;} 如果要实现最小化到托盘,则必须在窗体内加入控件notifyicon,并...

C# 程序运行后,立刻最小化到托盘
直接在你的主窗口主函数里面写就好,在InitializeComponent();之后写

C# winform 启动后自动最小化至托盘,如何去掉屏幕左下角缩小的那部分...
在窗体的WindowState 属性选择Minimized,而不是通过程序登录时候去设置 this.WindowState = FormWindowState.Minimized;后者就是你现在遇到的情况

相似回答