托盘图标设置
新建一个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 }
禁用多进程启动
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 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 }