using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Runtime.InteropServices;
namespace W_TestDemo
{
//NotifyIcon为托盘控件
public partial class Form2 : Form
{
//这里在窗体上没有拖拽一个NotifyIcon控件,而是在这里定义了一个变量
private NotifyIcon notifyIcon = null;
public Form2()
{
InitializeComponent();
//调用初始化托盘显示函数
InitialTray();
RegisterHotKey(this.Handle, 10, MODKEY.ALT, Keys.X);
RegisterHotKey(this.Handle, 11, MODKEY.ALT, Keys.S);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
switch (m.WParam.ToInt32())
{
case 10:
{
this.Visible = true;
this.Activate();
}
break;
case 11:
{
this.Visible = true;
this.Activate();
}
break;
default:
break;
}
return;
}
base.WndProc(ref m);
}
//要定义热键的窗口的句柄
//定义热键ID(不能与其它ID重复)int id,
//标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效KeyModifiers fsModifiers, Keys vk //定义热键的内容
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr wnd, int id, MODKEY mode, Keys vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr wnd, int id);
[Flags()]
public enum MODKEY
{
None = 0,
ALT = 0x0001,
CTRL = 0x0002,
SHIFT = 0x0004,
WIN = 0x0008,
}
private void Form1_Load(object sender, EventArgs e)
{
//这里写其他代码
}
/// <summary>
/// 窗体关闭的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
//通过这里可以看出,这里的关闭其实不是真正意义上的“关闭”,而是将窗体隐藏,实现一个“伪关闭”
this.Hide();
}
private void InitialTray()
{
//隐藏主窗体
this.Hide();
//实例化一个NotifyIcon对象
notifyIcon = new NotifyIcon();
//托盘图标气泡显示的内容
notifyIcon.BalloonTipText = "正在后台运行";
//托盘图标显示的内容
notifyIcon.Text = "窗体托盘后台运行测试程序";
//注意:下面的路径可以是绝对路径、相对路径。但是需要注意的是:文件必须是一个.ico格式
notifyIcon.Icon = new System.Drawing.Icon("C:/Users/veepoo-cd/Desktop/veepoo.ico"); //绝对路径
//notifyIcon.Icon =
//true表示在托盘区可见,false表示在托盘区不可见
notifyIcon.Visible = true;
//气泡显示的时间(单位是毫秒)
notifyIcon.ShowBalloonTip(2000);
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
////设置二级菜单
//MenuItem setting1 = new MenuItem("二级菜单1");
//MenuItem setting2 = new MenuItem("二级菜单2");
//MenuItem setting = new MenuItem("一级菜单", new MenuItem[]{setting1,setting2});
//帮助选项,这里只是“有名无实”在菜单上只是显示,单击没有效果,可以参照下面的“退出菜单”实现单击事件
MenuItem help = new MenuItem("帮助");
help.Click += new EventHandler(help_Click);
//关于选项
MenuItem about = new MenuItem("关于");
about.Click += new EventHandler(about_Click);
//退出菜单项
MenuItem exit = new MenuItem("退出");
exit.Click += new EventHandler(exit_Click);
////关联托盘控件
//注释的这一行与下一行的区别就是参数不同,setting这个参数是为了实现二级菜单
//MenuItem[] childen = new MenuItem[] { setting, help, about, exit };
MenuItem[] childen = new MenuItem[] {help,about,exit};
notifyIcon.ContextMenu = new ContextMenu(childen);
//窗体关闭时触发
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
}
/// <summary>
/// 鼠标单击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
//鼠标左键单击
if (e.Button == MouseButtons.Left)
{
//如果窗体是可见的,那么鼠标左击托盘区图标后,窗体为不可见
if (this.Visible == true )
{
//this.Visible = false;
}
else
{
this.Visible = true;
this.Activate();
}
}
}
/// <summary>
/// 帮助选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void help_Click(object sender, EventArgs e)
{
MessageBox.Show("帮助,哈哈");
}
/// <summary>
/// 关于选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void about_Click(object sender, EventArgs e)
{
MessageBox.Show("关于,哈哈");
}
/// <summary>
/// 退出选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exit_Click(object sender, EventArgs e)
{
//退出程序
System.Environment.Exit(0);
}
}
}
Winform托盘控件NotifyIcon
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Xamarin XAML语言教程构建ControlTemplate控件模板 控件模板ControlTemplate...
- 项目地址:https://github.com/razerdp/FriendCircle一起撸个朋友圈吧这是本文所...
- 最近做的一个项目,需要用户自定义报表和打印功能,了解了许多关于.NET报表插件,最终选择了FastReport这款...