托盘图标设置
新建一个NotifyIcon,会在托盘处显示一个图标。
NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标。
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
private NotifyIcon _notifyIcon;
private void SetNotifyIcon()
{
this._notifyIcon = new NotifyIcon();
this._notifyIcon.BalloonTipText = "翻译小工具";
this._notifyIcon.ShowBalloonTip();
this._notifyIcon.Text = "集成金山、有道非官方数据的翻译工具";
this._notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this._notifyIcon.Visible = true;
//打开菜单项
MenuItem open = new MenuItem("打开");
open.Click += new EventHandler(ShowMainWindow);
//退出菜单项
MenuItem exit = new MenuItem("退出");
exit.Click += new EventHandler(Close);
//关联托盘控件
MenuItem[] childen = new MenuItem[] { open, exit };
_notifyIcon.ContextMenu = new ContextMenu(childen); this._notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) =>
{
if (e.Button == MouseButtons.Left) ShowMainWindow(o, e);
});
} private void ShowMainWindow(object sender, EventArgs e)
{
_mainWindow.Visibility = Visibility.Visible;
_mainWindow.ShowInTaskbar = true;
_mainWindow.Activate();
} private void Close(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
禁用多进程启动
//禁止双进程
bool canCreateNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out canCreateNew))
{
if (!canCreateNew)
{
this.Shutdown();
}
}
删除原有进程
/// <summary>
/// 删除原有进程
/// </summary>
/// <param name="processName"></param>
private void KillProcess(string processName)
{
try
{
//删除所有同名进程
Process currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(processName).Where(process => process.Id != currentProcess.Id);
foreach (Process thisproc in processes)
{
thisproc.Kill();
}
}
catch (Exception ex)
{
}
}
设置开机自启动
关于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中完整代码:
public partial class App : Application
{
MainWindow _mainWindow;
public App()
{
KillProcess(System.Windows.Forms.Application.ProductName); SetAppAutoRun(true); Startup += App_Startup;
} private void App_Startup(object sender, StartupEventArgs e)
{
_mainWindow = new MainWindow();
SetNotifyIcon();
} #region 托盘图标 private NotifyIcon _notifyIcon;
private void SetNotifyIcon()
{
this._notifyIcon = new NotifyIcon();
this._notifyIcon.BalloonTipText = "翻译小工具";
this._notifyIcon.ShowBalloonTip();
this._notifyIcon.Text = "集成金山、有道非官方数据的翻译工具";
this._notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this._notifyIcon.Visible = true;
//打开菜单项
MenuItem open = new MenuItem("打开");
open.Click += new EventHandler(ShowMainWindow);
//退出菜单项
MenuItem exit = new MenuItem("退出");
exit.Click += new EventHandler(Close);
//关联托盘控件
MenuItem[] childen = new MenuItem[] { open, exit };
_notifyIcon.ContextMenu = new ContextMenu(childen); this._notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) =>
{
if (e.Button == MouseButtons.Left) ShowMainWindow(o, e);
});
} private void ShowMainWindow(object sender, EventArgs e)
{
_mainWindow.Visibility = Visibility.Visible;
_mainWindow.ShowInTaskbar = true;
_mainWindow.Activate();
} private void Close(object sender, EventArgs e)
{
Application.Current.Shutdown();
} #endregion #region 删除原有进程 /// <summary>
/// 删除原有进程
/// </summary>
/// <param name="processName"></param>
private void KillProcess(string processName)
{
try
{
//删除所有同名进程
Process currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(processName).Where(process => process.Id != currentProcess.Id);
foreach (Process thisproc in processes)
{
thisproc.Kill();
}
}
catch (Exception ex)
{
}
} #endregion #region 开机自启动 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.CurrentUser, autoRun, exeName, executablePath);
if (!success)
{
success = SetAutoRun(Registry.LocalMachine, 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;
} #endregion
}