托盤圖標設置 新建一個NotifyIcon,會在托盤處顯示一個圖標。 NotifyIcon.Icon可以直接設置一個ico圖片,也可以延用原有程式的圖標。 notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application. ...
托盤圖標設置
新建一個NotifyIcon,會在托盤處顯示一個圖標。
NotifyIcon.Icon可以直接設置一個ico圖片,也可以延用原有程式的圖標。
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
1 public partial class MainWindow : Window 2 { 3 private NotifyIcon notifyIcon; 4 5 public MainWindow() 6 { 7 InitializeComponent(); 8 SetNotifyIcon(); 9 this.Hide(); 10 } 11 12 #region NotifyIcon 13 14 private void SetNotifyIcon() 15 { 16 this.notifyIcon = new NotifyIcon(); 17 this.notifyIcon.BalloonTipText = "磁碟清理工具"; 18 this.notifyIcon.ShowBalloonTip(2000); 19 this.notifyIcon.Text = "磁碟清理工具:每20天清理一次"; 20 this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath); 21 this.notifyIcon.Visible = true; 22 //打開菜單項 23 MenuItem open = new MenuItem("打開"); 24 open.Click += new EventHandler(Show); 25 //退出菜單項 26 MenuItem exit = new MenuItem("退出"); 27 exit.Click += new EventHandler(Close); 28 //關聯托盤控制項 29 MenuItem[] childen = new MenuItem[] { open, exit }; 30 notifyIcon.ContextMenu = new ContextMenu(childen); 31 32 this.notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) => 33 { 34 if (e.Button == MouseButtons.Left) this.Show(o, e); 35 }); 36 } 37 38 private void Show(object sender, EventArgs e) 39 { 40 this.Visibility = Visibility.Visible; 41 this.ShowInTaskbar = true; 42 this.Activate(); 43 } 44 45 private void Hide(object sender, EventArgs e) 46 { 47 this.ShowInTaskbar = false; 48 this.Visibility = Visibility.Hidden; 49 } 50 51 private void Close(object sender, EventArgs e) 52 { 53 System.Windows.Application.Current.Shutdown(); 54 } 55 56 #endregion 57 58 #region 視窗 59 60 private void MinimizeButton_OnClick(object sender, RoutedEventArgs e) 61 { 62 WindowState = WindowState.Minimized; 63 } 64 65 private void CloseButton_OnClick(object sender, RoutedEventArgs e) 66 { 67 this.Hide(); 68 } 69 70 private void HeaderGrid_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 71 { 72 if (e.ButtonState == MouseButtonState.Pressed) 73 { 74 this.DragMove(); 75 } 76 } 77 78 #endregion 79 }View Code
禁用多進程啟動
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 //得到所有打開的進程 8 try 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 //找到程式進程,kill之。 15 if (!thisproc.CloseMainWindow()) 16 { 17 thisproc.Kill(); 18 } 19 } 20 } 21 catch (Exception ex) 22 { 23 24 } 25 }
設置開機自啟動
1 private void SetAppAutoRun(bool autoRun) 2 { 3 if (autoRun) //設置開機自啟動 4 { 6 string path = System.Windows.Forms.Application.ExecutablePath; 7 RegistryKey rk = Registry.LocalMachine; 8 RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); 9 rk2.SetValue("JcShutdown", path); 10 rk2.Close(); 11 rk.Close(); 12 } 13 else //取消開機自啟動 14 {
16 RegistryKey rk = Registry.LocalMachine; 17 RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); 18 rk2.DeleteValue("JcShutdown", false); 19 rk2.Close(); 20 rk.Close(); 21 } 22 }
App.cs中完整代碼:
1 public partial class App : Application 2 { 3 public App() 4 { 5 //禁止雙進程 6 bool canCreateNew; 7 using (System.Threading.Mutex m = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out canCreateNew)) 8 { 9 if (!canCreateNew) 10 { 11 this.Shutdown(); 12 } 13 } 14 15 SetAppAutoRun(true); 16 17 Startup += App_Startup; 18 } 19 20 private void SetAppAutoRun(bool autoRun) 21 { 22 if (autoRun) //設置開機自啟動 23 { 24 MessageBox.Show("設置開機自啟動,需要修改註冊表", "提示"); // hovertree.com 25 string path = System.Windows.Forms.Application.ExecutablePath; 26 RegistryKey rk = Registry.LocalMachine; 27 RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); 28 rk2.SetValue("JcShutdown", path); 29 rk2.Close(); 30 rk.Close(); 31 } 32 else //取消開機自啟動 33 { 34 MessageBox.Show("取消開機自啟動,需要修改註冊表", "提示"); 35 RegistryKey rk = Registry.LocalMachine; 36 RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); 37 rk2.DeleteValue("JcShutdown", false); 38 rk2.Close(); 39 rk.Close(); 40 } 41 } 42 43 private void App_Startup(object sender, StartupEventArgs e) 44 { 45 new AutoCleanCacheHelper(CleanCacheVeiwModel.ViewModel).Start(); 46 } 47 }View Code