Winform开机启动 托盘显示 打开超链接

近段时间忙于写一个winform小程序,对于擅长写web程序的我谈不上是在分享,这里所写的相当于我的备忘录,呵呵。。
1)开机启动

[c-sharp] view plain copy print ?
  1. string R_startPath = Application.ExecutablePath;  
  2.             if (checkBox1.Checked == true)  
  3.             {  
  4.                 RegistryKey R_local = Registry.LocalMachine;  
  5.                 RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");  
  6.                 R_run.SetValue("MT128Stock", R_startPath);  
  7.                 R_run.Close();  
  8.                 R_local.Close();  
  9.             }  
  10.             else  
  11.             {  
  12.                 try  
  13.                 {  
  14.                     RegistryKey R_local = Registry.LocalMachine;  
  15.                     RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");  
  16.                     R_run.DeleteValue("MT128Stock"false);  
  17.                     R_run.Close();  
  18.                     R_local.Close();  
  19.                 }  
  20.                 catch (Exception ex)  
  21.                 {  
  22.                     MessageBox.Show("您需要管理員權權限修改""提示", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  23.                     throw;  
  24.                 }  
  25.   
  26.             }  


2)托盘显示
向winfom中插入NotifyIcon控件notifyIcon1
[c-sharp] view plain copy print ?
  1. protected override void OnSizeChanged(EventArgs e)  
  2.         {  
  3.   
  4.             if (this.WindowState == FormWindowState.Normal)  
  5.             {  
  6.                 this.Width = 920;  
  7.                 this.Height = 530;  
  8.             }  
  9.   
  10.             base.OnSizeChanged(e);  
  11.         }  
  12.         protected override void OnClosing(CancelEventArgs e)  
  13.         {  
  14.             //base.OnClosing(e);  
  15.             e.Cancel = true;  
  16.   
  17.             HideMainForm();  
  18.   
  19.         }  
  20.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)  
  21.         {  
  22.             if (this.Visible)  
  23.             {  
  24.                 HideMainForm();  
  25.             }  
  26.             else  
  27.             {  
  28.                 ShowMainForm();  
  29.             }  
  30.   
  31.         }  
  32.         private void HideMainForm()  
  33.         {  
  34.             this.Hide();  
  35.         }  
  36.   
  37.         private void ShowMainForm()  
  38.         {  
  39.             this.WindowState = FormWindowState.Normal;  
  40.             this.Show();  
  41.             this.Activate();  
  42.         }  
  43.   
  44.         private void ExitMainForm()  
  45.         {  
  46.             if (MessageBox.Show("您確認要退出股票接收程序嗎?""確認退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)  
  47.             {  
  48.                 this.notifyIcon1.Visible = false;  
  49.                 this.Close();  
  50.                 this.Dispose();  
  51.                 Application.Exit();  
  52.             }  
  53.         }  

 

3)托盘显示的右击菜单
向winfom中插入ContextMenuStrip控件contextMenuStrip1,再将notifyIcon1的ContextMenuStrip属性值设为contextMenuStrip1,并添加多项相关Items

[c-sharp] view plain copy print ?
  1. #region 右键菜单处理,显示 隐藏 退出  
  2.         private void MinShowItem_Click(object sender, EventArgs e)  
  3.         {  
  4.             HideMainForm();  
  5.   
  6.         }  
  7.   
  8.         private void ShowItem_Click(object sender, EventArgs e)  
  9.         {  
  10.             ShowMainForm();  
  11.         }  
  12.   
  13.         private void ExitItem_Click(object sender, EventArgs e)  
  14.         {  
  15.             ExitMainForm();  
  16.         }  
  17.  
  18.         #endregion 
通过在Microsft.Win32命名空间的Registry可以在注册表中设置注册表项中的名称/值对的值。
在注册表的"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"中
存储应用程序名和路径可以实现程序的自启动。代码如下:
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  AutoRun
{
    
public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }
        
// 设置开机启动
         private   void  btnSet_Click( object  sender, EventArgs e)
        {
            Microsoft.Win32.Registry.SetValue(
@" HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run "
                , Application.ProductName, Application.StartupPath 
+  Application.ProductName);
        }
        
// C# WinForm打开超链接
         private   void  Form1_Load( object  sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(
" iexplore.exe " " http://revit.5d6d.com " );
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值