110,560
社区成员
发帖
与我相关
我的任务
分享
// Program.cs
using System;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;
namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew);
// 如果该命名事件已经存在(存在有前一个运行实例),则
if (!createNew)
{
// 先写一些数据到注册表中,以便传递给前一个运行实例
Registry.SetValue(@"HKEY_CURRENT_USER\Software\MyMy", "", DateTime.Now.ToLongTimeString());
// 发事件通知
ProgramStarted.Set();
// 等一小会以便前一个运行实例接到通知后恢复显示(也可以采用等待事件通知的方式)
Thread.Sleep(200);
// 将焦点转移到前一个实例
foreach (Process p in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
{
if (p.MainWindowHandle != IntPtr.Zero)
{
SetForegroundWindow(p.MainWindowHandle);
}
}
// 就此退出第二个进程
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static EventWaitHandle ProgramStarted;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
}
}
using System;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
NotifyIcon notifyIcon1 = new NotifyIcon();
public Form1()
{
//InitializeComponent();
this.Controls.Add(new TextBox());
this.notifyIcon1.Text = "Double click me to show window";
this.notifyIcon1.Icon = System.Drawing.SystemIcons.Question;
this.notifyIcon1.DoubleClick += OnNotifyIconDoubleClicked;
this.SizeChanged += OnSizeChanged;
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
}
// 当最小化时,放到系统托盘。
void OnSizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Visible = false;
}
}
// 当双击托盘图标时,恢复窗口显示
void OnNotifyIconDoubleClicked(object sender, EventArgs e)
{
this.Visible = true;
this.notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Normal;
}
// 当收到第二个进程的通知时,从注册表中获得传入的参数,并恢复窗口显示
void OnProgramStarted(object state, bool timeout)
{
if (this.InvokeRequired)
{
this.Invoke(new WaitOrTimerCallback(OnProgramStarted), state, timeout);
}
else
{
object param = Registry.GetValue(@"HKEY_CURRENT_USER\Software\MyMy", "", string.Empty);
this.Text = param as string;
OnNotifyIconDoubleClicked(this, EventArgs.Empty);
}
}
}
}