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

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

[c-sharp] view plain copy print ?
  1. stringR_startPath=Application.ExecutablePath;
  2. if(checkBox1.Checked==true)
  3. {
  4. RegistryKeyR_local=Registry.LocalMachine;
  5. RegistryKeyR_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. RegistryKeyR_local=Registry.LocalMachine;
  15. RegistryKeyR_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(Exceptionex)
  21. {
  22. MessageBox.Show("您需要管理員權權限修改","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
  23. throw;
  24. }
  25. }


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

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

[c-sharp] view plain copy print ?
  1. #region右键菜单处理,显示 隐藏 退出
  2. privatevoidMinShowItem_Click(objectsender,EventArgse)
  3. {
  4. HideMainForm();
  5. }
  6. privatevoidShowItem_Click(objectsender,EventArgse)
  7. {
  8. ShowMainForm();
  9. }
  10. privatevoidExitItem_Click(objectsender,EventArgse)
  11. {
  12. ExitMainForm();
  13. }
  14. #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,EventArgse)
{
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,EventArgse)
{
System.Diagnostics.Process.Start(
" iexplore.exe " , " http://revit.5d6d.com " );
}
}
}
Winform开机启动 托盘显示 打开超链接

你可能感兴趣的:(WinForm)