托盤圖標設置
新建一個NotifyIcon,會在托盤處顯示一個圖標。
NotifyIcon.Icon可以直接設置一個ico圖片,也可以延用原有程序的圖標。
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
1 private NotifyIcon _notifyIcon; 2 private void SetNotifyIcon() 3 { 4 this._notifyIcon = new NotifyIcon(); 5 this._notifyIcon.BalloonTipText = "翻譯小工具"; 6 this._notifyIcon.ShowBalloonTip(2000); 7 this._notifyIcon.Text = "集成金山、有道非官方數據的翻譯工具"; 8 this._notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath); 9 this._notifyIcon.Visible = true; 10 //打開菜單項 11 MenuItem open = new MenuItem("打開"); 12 open.Click += new EventHandler(ShowMainWindow); 13 //退出菜單項 14 MenuItem exit = new MenuItem("退出"); 15 exit.Click += new EventHandler(Close); 16 //關聯托盤控件 17 MenuItem[] childen = new MenuItem[] { open, exit }; 18 _notifyIcon.ContextMenu = new ContextMenu(childen); 19 20 this._notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) => 21 { 22 if (e.Button == MouseButtons.Left) ShowMainWindow(o, e); 23 }); 24 } 25 26 private void ShowMainWindow(object sender, EventArgs e) 27 { 28 _mainWindow.Visibility = Visibility.Visible; 29 _mainWindow.ShowInTaskbar = true; 30 _mainWindow.Activate(); 31 } 32 33 private void Close(object sender, EventArgs e) 34 { 35 Application.Current.Shutdown(); 36 }
禁用多進程啟動
1 //禁止雙進程 2 bool canCreateNew; 3 using (System.Threading.Mutex m = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out canCreateNew)) 4 { 5 if (!canCreateNew) 6 { 7 this.Shutdown(); 8 } 9 }
刪除原有進程
1 /// <summary> 2 /// 刪除原有進程 3 /// </summary> 4 /// <param name="processName"></param> 5 private void KillProcess(string processName) 6 { 7 try 8 { 9 //刪除所有同名進程 10 Process currentProcess = Process.GetCurrentProcess(); 11 var processes = Process.GetProcessesByName(processName).Where(process => process.Id != currentProcess.Id); 12 foreach (Process thisproc in processes) 13 { 14 thisproc.Kill(); 15 } 16 } 17 catch (Exception ex) 18 { 19 } 20 }
設置開機自啟動
關於C#開機自動啟動程序的方法,修改注冊表:
1. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
2.HKEY_Current_User\Software\Microsoft\Windows\CurrentVersion\Run
系統有默認選擇注冊表位置。如果LocalMachine設置異常,則可以在CurrentUser中設置開機啟動項。
當然通過管理員權限,也是可以在LocalMachine設置的。
private void SetAppAutoRun(bool autoRun) { try { string executablePath = System.Windows.Forms.Application.ExecutablePath; string exeName = Path.GetFileNameWithoutExtension(executablePath); SetAutoRun(autoRun, exeName, executablePath); } catch (Exception e) { } } private bool SetAutoRun(bool autoRun, string exeName, string executablePath) { bool success = SetAutoRun(Registry.LocalMachine, autoRun, exeName, executablePath); if (!success) { success = SetAutoRun(Registry.CurrentUser, autoRun, exeName, executablePath); } return success; } private bool SetAutoRun(RegistryKey rootKey, bool autoRun, string exeName, string executablePath) { try { RegistryKey autoRunKey = rootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); if (autoRunKey == null) { autoRunKey = rootKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); } if (autoRunKey != null) { if (autoRun) //設置開機自啟動 { autoRunKey.SetValue(exeName, $"\"{executablePath}\" /background"); } else //取消開機自啟動 { autoRunKey.DeleteValue(exeName, false); } autoRunKey.Close(); autoRunKey.Dispose(); } } catch (Exception e) { rootKey.Close(); rootKey.Dispose(); return false; } rootKey.Close(); rootKey.Dispose(); return true; }
App.cs中完整代碼:

1 public partial class App : Application 2 { 3 MainWindow _mainWindow; 4 public App() 5 { 6 KillProcess(System.Windows.Forms.Application.ProductName); 7 8 SetAppAutoRun(true); 9 10 Startup += App_Startup; 11 } 12 13 private void App_Startup(object sender, StartupEventArgs e) 14 { 15 _mainWindow = new MainWindow(); 16 SetNotifyIcon(); 17 } 18 19 #region 托盤圖標 20 21 private NotifyIcon _notifyIcon; 22 private void SetNotifyIcon() 23 { 24 this._notifyIcon = new NotifyIcon(); 25 this._notifyIcon.BalloonTipText = "翻譯小工具"; 26 this._notifyIcon.ShowBalloonTip(2000); 27 this._notifyIcon.Text = "集成金山、有道非官方數據的翻譯工具"; 28 this._notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath); 29 this._notifyIcon.Visible = true; 30 //打開菜單項 31 MenuItem open = new MenuItem("打開"); 32 open.Click += new EventHandler(ShowMainWindow); 33 //退出菜單項 34 MenuItem exit = new MenuItem("退出"); 35 exit.Click += new EventHandler(Close); 36 //關聯托盤控件 37 MenuItem[] childen = new MenuItem[] { open, exit }; 38 _notifyIcon.ContextMenu = new ContextMenu(childen); 39 40 this._notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) => 41 { 42 if (e.Button == MouseButtons.Left) ShowMainWindow(o, e); 43 }); 44 } 45 46 private void ShowMainWindow(object sender, EventArgs e) 47 { 48 _mainWindow.Visibility = Visibility.Visible; 49 _mainWindow.ShowInTaskbar = true; 50 _mainWindow.Activate(); 51 } 52 53 private void Close(object sender, EventArgs e) 54 { 55 Application.Current.Shutdown(); 56 } 57 58 #endregion 59 60 #region 刪除原有進程 61 62 /// <summary> 63 /// 刪除原有進程 64 /// </summary> 65 /// <param name="processName"></param> 66 private void KillProcess(string processName) 67 { 68 try 69 { 70 //刪除所有同名進程 71 Process currentProcess = Process.GetCurrentProcess(); 72 var processes = Process.GetProcessesByName(processName).Where(process => process.Id != currentProcess.Id); 73 foreach (Process thisproc in processes) 74 { 75 thisproc.Kill(); 76 } 77 } 78 catch (Exception ex) 79 { 80 } 81 } 82 83 #endregion 84 85 #region 開機自啟動 86 87 private void SetAppAutoRun(bool autoRun) 88 { 89 try 90 { 91 string executablePath = System.Windows.Forms.Application.ExecutablePath; 92 string exeName = Path.GetFileNameWithoutExtension(executablePath); 93 SetAutoRun(autoRun, exeName, executablePath); 94 } 95 catch (Exception e) 96 { 97 } 98 } 99 private bool SetAutoRun(bool autoRun, string exeName, string executablePath) 100 { 101 bool success = SetAutoRun(Registry.CurrentUser, autoRun, exeName, executablePath); 102 if (!success) 103 { 104 success = SetAutoRun(Registry.LocalMachine, autoRun, exeName, executablePath); 105 } 106 return success; 107 } 108 private bool SetAutoRun(RegistryKey rootKey, bool autoRun, string exeName, string executablePath) 109 { 110 try 111 { 112 RegistryKey autoRunKey = rootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); 113 if (autoRunKey == null) 114 { 115 autoRunKey = rootKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); 116 } 117 if (autoRunKey != null) 118 { 119 if (autoRun) //設置開機自啟動 120 { 121 autoRunKey.SetValue(exeName, $"\"{executablePath}\" /background"); 122 } 123 else //取消開機自啟動 124 { 125 autoRunKey.DeleteValue(exeName, false); 126 } 127 autoRunKey.Close(); 128 autoRunKey.Dispose(); 129 } 130 } 131 catch (Exception e) 132 { 133 rootKey.Close(); 134 rootKey.Dispose(); 135 return false; 136 } 137 rootKey.Close(); 138 rootKey.Dispose(); 139 return true; 140 } 141 142 #endregion 143 }