下面是完美运行的代码。如果应用程序已经在运行,它的最大化/恢复应用程序或在前面显示它.. 工作完美,但现在问题是当窗口最小化到系统托盘时。
this.showInTaskbar = false;
this.WindowState = System.Windows.WindowState.Minimized;
它没有恢复应用程序,我该怎么办?让它从系统托盘恢复/最大化?
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
private const int SW_SHOWMAXIMIZED = 1;
public NewDesign(){
InitializeComponent();
if (!EnsureSingleInstance()){
System.Environment.Exit(0);
}
}
static bool EnsureSingleInstance()
{
Process currentProcess = Process.GetCurrentProcess();
var runningProcess = (from process in Process.GetProcesses()
where
process.Id != currentProcess.Id &&
process.ProcessName.Equals(
currentProcess.ProcessName,
StringComparison.Ordinal)
select process).FirstOrDefault();
if (runningProcess != null)
{
ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
SetForegroundWindow(runningProcess.MainWindowHandle);
return false;
}
return true;
}