C# winForm程序开机启动和托盘显示

这段时间一直在学习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事件然后

代码
 
   
private void 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事件

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

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

代码
 
   
private void 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();
}
}

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

private void hideMenuItem_Click( object sender, EventArgs e)
{
this .Show();
this .WindowState = FormWindowState.Normal;
this .Activate();
}

 

转载于:https://www.cnblogs.com/fresh1985/archive/2010/11/19/fresh.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值