WPF程序最小化至系统托盘之后,启动第二个实例直接激活已打开程序的两种实现
首先明确一点,如果使用Hide(),Show()来实现,它内部会改变Visibility的值,当我们第二次运行激活上个实例的时候无法直接通过ShowWindow直接激活窗口(网上也有说能激活的,但是我测试的时候发现虽然能激活,但是窗口是全黑的。。。) 所以就想了另外的方法实现这个功能。
还是直接撸代码吧(如有不足之处,请各位大佬轻喷。。指正一下小弟):
第一种:最小化的时候直接让窗口最小化,然后任务栏图标设为false。
主窗口 cs:
1 public partial class MainWindow : Window 2 { 3 /// <summary> 4 /// 系统图标 5 /// </summary> 6 private NotifyIcon _notifyIcon = null; 7 public MainWindow() 8 { 9 InitializeComponent(); 10 InitialTray(); 11 WindowState = WindowState.Maximized; 12 WindowStartupLocation = WindowStartupLocation.CenterOwner; 13 userControl11.popNumKeyboard.IsOpen = isOpenLittleKeyboard;//小键盘是否显示 14 } 15 /// <summary> 16 /// 初始化系统托盘图标 17 /// </summary> 18 private void InitialTray() 19 { 20 _notifyIcon = new NotifyIcon(); 21 _notifyIcon.BalloonTipText = "自助打印程序运行中...";//托盘气泡显示内容 22 _notifyIcon.Text = "自助打印程序"; 23 _notifyIcon.Visible = true;//托盘按钮是否可见 24 _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath); 25 _notifyIcon.ShowBalloonTip(1000); 26 27 ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem { Text = "退出", Size = new System.Drawing.Size(107, 24) }; 28 toolStripMenuItem.Click += ToolStripMenuItem_Click; 29 30 ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); 31 contextMenuStrip.Size = new System.Drawing.Size(108, 28); 32 contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripMenuItem }); 33 34 _notifyIcon.ContextMenuStrip = contextMenuStrip; 35 _notifyIcon.MouseClick += notifyIcon_MouseClick; 36 this.StateChanged += MainWindow_StateChanged; 37 } 38 private void ToolStripMenuItem_Click(object sender, EventArgs e) 39 { 40 this.Close(); 41 } 42 43 #region 窗口状态改变 44 /// <summary> 45 /// 窗口状态改变 46 /// </summary> 47 /// <param name="sender"></param> 48 /// <param name="e"></param> 49 private void MainWindow_StateChanged(object sender, EventArgs e) 50 { 51 if (this.WindowState == WindowState.Minimized) 52 { 53 //this.Visibility = Visibility.Hidden; 54 this.ShowInTaskbar = false; 55 this._notifyIcon.Visible = true; 56 } 57 } 58 #endregion 59 #region 托盘图标鼠标单击事件 60 /// <summary> 61 /// 托盘图标鼠标单击事件 62 /// </summary> 63 /// <param name="sender"></param> 64 /// <param name="e"></param> 65 private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) 66 { 67 if (e.Button == MouseButtons.Left) 68 { 69 70 this.ShowInTaskbar = true; 71 this.WindowState = WindowState.Normal; 72 this.Activate(); 73 //} 74 } 75 } 76 #endregion 77 }
入口cs:(两种方法通用)
1 static class Program 2 { 3 [DllImport("user32.dll")] 4 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 5 6 [DllImport("user32.dll ", SetLastError = true)] 7 static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab); 8 9 [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)] 10 public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); 11 12 public static IntPtr formhwnd; 13 /// <summary> 14 /// 应用程序的主入口点。 15 /// </summary> 16 [STAThread] 17 static void Main(string[] args) 18 { 19 string proc = Process.GetCurrentProcess().ProcessName; 20 Process[] processes = Process.GetProcessesByName(proc); 21 // 该程序未运行, 22 if (processes.Length <= 1) 23 { 24 WpfSelfServiceExe.App app = new WpfSelfServiceExe.App(); 25 app.StartupUri = new Uri("MainWindow.xaml", System.UriKind.Relative); 26 app.Run(); 27 } 28 else 29 { 30 foreach (Process process in processes) 31 { 32 if (process.Id != Process.GetCurrentProcess().Id) 33 { 34 if (process.MainWindowHandle.ToInt32() == 0) 35 { 36 formhwnd = FindWindow(null, "MainWindow"); 37 ShowWindow(formhwnd, 3);//1代表窗体执行this.WindowState = FormWindowState.Normal; 38 SwitchToThisWindow(formhwnd, true); 39 break; 40 } 41 else 42 { 43 // 如果窗体没有隐藏,就直接切换到该窗体并带入到前台,因为窗体除了隐藏到托盘,还可以最小化 44 SwitchToThisWindow(process.MainWindowHandle, true); 45 break; 46 } 47 } 48 } 49 } 50 } 51 }
第二种方法:主页cs中也用ShowWindow来操作显示和隐藏界面,入口cs和上面通用!!
1 /// <summary> 2 /// MainWindow.xaml 的交互逻辑 3 /// </summary> 4 public partial class MainWindow : Window 5 { 6 [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)] 7 public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); 8 /// <summary> 9 /// 系统图标 10 /// </summary> 11 private NotifyIcon _notifyIcon = null; 12 13 public MainWindow() 14 { 15 InitializeComponent(); 16 InitialTray(); 17 WindowState = WindowState.Maximized; 18 WindowStartupLocation = WindowStartupLocation.CenterOwner; 19 userControl11.popNumKeyboard.IsOpen = isOpenLittleKeyboard;//小键盘是否显示 20 } 21 /// <summary> 22 /// 返回按钮 23 /// </summary> 24 /// <param name="sender"></param> 25 /// <param name="e"></param> 26 private void ReturnBtn_Click(object sender, RoutedEventArgs e) 27 {
ShowWindow(Process.GetCurrentProcess().MainWindowHandle, 0); 28 } 29 /// <summary> 30 /// 初始化系统托盘图标 31 /// </summary> 32 private void InitialTray() 33 { 34 _notifyIcon = new NotifyIcon(); 35 _notifyIcon.BalloonTipText = "自助打印程序运行中...";//托盘气泡显示内容 36 _notifyIcon.Text = "自助打印程序"; 37 _notifyIcon.Visible = true;//托盘按钮是否可见 38 _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath); 39 _notifyIcon.ShowBalloonTip(1000); 40 41 ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem { Text = "退出", Size = new System.Drawing.Size(107, 24) }; 42 toolStripMenuItem.Click += ToolStripMenuItem_Click; 43 44 ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); 45 contextMenuStrip.Size = new System.Drawing.Size(108, 28); 46 contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripMenuItem }); 47 48 _notifyIcon.ContextMenuStrip = contextMenuStrip; 49 _notifyIcon.MouseClick += notifyIcon_MouseClick; 50 } 51 52 private void ToolStripMenuItem_Click(object sender, EventArgs e) 53 { 54 this.Close(); 55 } 56 57 #region 托盘图标鼠标单击事件 58 /// <summary> 59 /// 托盘图标鼠标单击事件 60 /// </summary> 61 /// <param name="sender"></param> 62 /// <param name="e"></param> 63 private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) 64 { 65 if (e.Button == MouseButtons.Left) 66 { 67 if (WindowState == WindowState.Minimized) 68 { 69 WindowState = WindowState.Normal; 70 } 71 this.Activate(); 72 ShowWindow(new System.Windows.Interop.WindowInteropHelper(this).Handle, 3); 74 } 75 } 76 #endregion 77 }