winform恢复窗口前端显示

1.添加系统DLL引用

[DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

2.得到窗口句柄

应用程序运行时

Process instance = null;
Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历与当前进程名称相同的进程列表 foreach (Process process in processes) {   //如果实例已经存在则忽略当前进程   if (process.Id != current.Id)   {     //得到已经存在的进程     instance = process;
    break;
  }
}

应用程序运行中

Process instance = Process.GetCurrentProcess();

3.调用API

SwitchToThisWindow(instance.MainWindowHandle, true);

 

也可以使用SetForegroundWindow接口实现

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

//需要窗口的显示状态Visible=true
SetForegroundWindow(new IntPtr(window.Hwnd));
//SetForegroundWindow(window.Handle);

 

posted @ 2018-02-05 11:38  太阳底下淋雨  阅读(198)  评论(0编辑  收藏  举报