posts - 710,  comments - 81,  views - 260万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

这段时间一直在学习C#,看了书然后又在网上看了N多大神些的blog,然后自己学着做了一个像QQ托盘图标那样的小功能的Demo:

        (1)、在窗口上点击关闭按钮或者最小化时将托盘显示;

        (2)、双击托盘图标显示窗口;

        (3)、右键点击托盘图标提供三个菜单选项,“退出”、“隐藏”、“显示”;

        (4)、程序可以设置开机启动,隐藏任务栏显示。就这四个小功能。

1、建一个WinForm程序—TestIconForm,将其属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示;将MaximizeBox属性设置为false,屏蔽掉最大化按钮;把StartPosition属性改为CerternScreen,这样程序运行后,窗口将会居中显示。

2、在工具栏中的公共控件里,拖入NotifyIcon控件—testNotifyIcon,这个是程序运行任务栏右侧通知区域图标显示控件。

3、在工具栏中的菜单和工具栏里,拖入ContextMenuStrip—testContextMenuStrip,这个控件是右击时关联菜单。

4、右键testNotifyIcon选择属性,将其属性ContextMenuStrip改加为testContextMenuStrip,这个时候1和2两个步骤的两个控件就关联了,用于完成上面(3)功能。

5、右键testContextMenuStrip选择属性,进入Items,然后点击“添加”,这里添加三个菜单选项:exitMenuItem、hideMenuItem、showMenuItem,同时分别将其Text属性改为:退出、隐藏和显示。

准备工作就这些,下面是大致代码:

 

1)、双击TestIconForm,即添加Load事件然后

复制代码
privatevoid Form1_Load(object sender, EventArgs e)
{
testNotifyIcon.Icon
=new Icon("e:\\MyPicture\\testIcon.ico");
//取得程序路径
string startup = Application.ExecutablePath;

//class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装
RegistryKey rKey = Registry.LocalMachine;
RegistryKey autoRun
= rKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
try
{
autoRun.SetValue(
"BookServer", startup);
rKey.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message.ToString(),
"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
复制代码

添加Form_Closing,SizeChanged事件

复制代码

private void Form1_FormClosing(object sender, FormClosingEventArgs e) //关闭按钮事件
        {
            e.Cancel = true;
            this.Hide();
        }

        private void Form1_SizeChanged(object sender, EventArgs e) //最小化事件按钮
        {
            this.Hide();
        }

复制代码

 

2)、给testNotifyIcon添加MouseDoubleClick事件

复制代码

privatevoid testNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) // 左键双击,显示
{
if (e.Button == MouseButtons.Left)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
复制代码

 3)、进入TestIconForm单击testContextMenuStrip,然后可以看到“退出”、“隐藏”、“显示”,分别双击,添加相应的事件

复制代码

privatevoid exitMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要退出终端服务程序吗?", "确认", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
== DialogResult.OK)
{
testNotifyIcon.Visible
=false;
this.Close();
this.Dispose();
Application.Exit();
}
}

privatevoid showMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
}

privatevoid hideMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
复制代码

 

posted on   itprobie-菜鸟程序员  阅读(620)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-08-28 Intellij IDEA的Facets和Artifacts
2014-08-28 adf常用方法总结
点击右上角即可分享
微信分享提示