最小化するとトレイアイコンに隠れるプログラムを作成しました。実行できるインスタンスは 1 つだけであり、再度実行しようとすると、現在のインスタンスが表示されてアクティブになるはずです。私のコードは次のようになります。
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(isRunningAlready())
{
string exe_name = AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "").Replace(".EXE", ""
Process[] procList = Process.GetProcessesByName(exe_name);
BringProcessToFront(procList)
return;
}
Application.Run(new MainForm());
}
public static void BringProcessToFront(System.Diagnostics.Process[] processes)
{
foreach (System.Diagnostics.Process process in processes)
{
IntPtr handle = process.MainWindowHandle;
ShowWindow(handle, 1);
SetForegroundWindow(handle);
}
}
このコードは、ウィンドウが表示されていて非アクティブな場合は正常に機能しますが、トレイアイコンに隠されているプログラムでは何もしません。私は何を間違っていますか?前もって感謝します。