using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
notifyIcon1.Icon = new Icon("2d.ico");//指定一个图标
notifyIcon1.Visible = false;
notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
} private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)//最小化
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
} } private void notifyIcon1_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
this.Activate();
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true; } } }我用上面的代码,但是却没法实现。点最小化的时候也没有最小化到托盘,点托盘的小图标的时候也不显示窗体。希望达人能告知原因。
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
notifyIcon1.Icon = new Icon("2d.ico");//指定一个图标
notifyIcon1.Visible = false;
notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
} private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)//最小化
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
} } private void notifyIcon1_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
this.Activate();
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true; } } }我用上面的代码,但是却没法实现。点最小化的时候也没有最小化到托盘,点托盘的小图标的时候也不显示窗体。希望达人能告知原因。
解决方案 »
- rdlc报表里添加中文字符就乱码,显示的都是方块
- 有知道这句VB含义的人不?
- C#中如何使用POI导入导出文件(excel,txt,csv格式)
- [竹子]问个GDI+问题:Graphics.DrawString()有显明锯齿, 怎么解决?
- HashTable和HashMap的区别??
- GridView1.Rows[e.RowIndex].Cells[3].Text.Trim()==""为什么是true
- 新手求助ADO.NET的使用方法
- 我现在看一个C#的程序,里面有很多类和接口,请问有没有什么工具或方法能把这些类之间的继承关系扯清楚???
- 求教一个简单的字符串判断问题,高分给
- 大侠们!我有个最严重的.net的问题!想请教各位!!!谢谢先!!!
- 每天需要在12点或傍晚7点传送数据,这样做行吗?
- 求C#读xml的方法
{
if (this.WindowState == FormWindowState.Minimized)//最小化
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
} }
这个方法你为什么还去判断是否最小化勒
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)//最小化
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
} }
{
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
this.Activate();
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true; }
在上面的方法中添加下面的设置
this.Visible = true;
{
if(this.WindowState==FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
} private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState=FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
notifyIcon1.Visible = false;
}这个是可以的
直接在NotifyIcon控件属性里设置图标和是否可见。
然后把NotifyIcon控件的notifyIcon1_MouseClick事件实现。刚测试通过。只要不要在Form_Load 里面做这些。
首先用使用Winform控件 NotifyIcon 常用属性:Ico是显示在托盘时的图标,Text是鼠标停放在托盘显示图标上时显示的文字说明,ContextMenuStrip 是鼠标右击事件的菜单,这个大家应该都知道,其他的属性有兴趣自己查查看吧。
窗体的SizeChanged事件中如下:
private void FormMain_SizeChanged(object sender, EventArgs e)
{
//如果点击最小化按钮时,窗体隐藏,显示图标
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
NotifyIcon控件的鼠标双击事件如下(当窗体最小化时,再重新打开窗体):
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;//弹出MainForm
this.WindowState = FormWindowState.Normal;//窗体恢复正常
}
这样一个winfrom程序最小化托盘就实现了,还有一些细微的判断自己可以酌情添加。
下面是我根据自己的需求添加的功能,在关闭窗体的时候提示是否关闭
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确认关闭吗?", "确认关闭", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Dispose();
Application.Exit();
}
else
{
e.Cancel = true;
this.WindowState = FormWindowState.Normal;
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
在托盘显示图标加的右键退出菜单
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确认退出吗?", "确认退出", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Dispose();
Application.Exit();
}
else
{
this.WindowState = FormWindowState.Normal;
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}