【发布时间】:2017-10-25 11:55:35
【问题描述】:
在我的 wpf 窗口中,我隐藏了窗口并在关闭时将其从任务栏中删除。
如何从正在运行的进程中激活该窗口。我尝试了很多方法,但都没有成功。
这是我激活隐藏窗口的示例代码。
private void checkIfProcessRunning()
{
// get the name of our process
string proc = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(proc);
// if there is more than one process...
if (processes.Length > 1)
{
// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us.
RzLogger.WriteLine("process are running count:" + processes.Length);
// get our process
Process p = Process.GetCurrentProcess();
int n = 0; // assume the other process is at index 0
// if this process id is OUR process ID...
if (processes[0].Id == p.Id)
{
RzLogger.WriteLine("other process are at 1");
// then the other process is at index 1
n = 1;
}
// get the window handle
IntPtr hWnd = processes[n].MainWindowHandle;
RzLogger.WriteLine("main handle is:" + hWnd);
// if iconic, we need to restore the window
if (IsIconic(hWnd))
{
RzLogger.WriteLine("is minimized");
ShowWindowAsync(hWnd, SW_SHOWNORMAL);
ShowWindowAsync(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
}
// bring it to the foreground
SetForegroundWindow(hWnd);
// exit our process
Application.Current.Shutdown();
return;
}
// ... continue with your application initialization here.
}
问题是我总是处理为 0。
有没有办法做到这一点?而且我不想在任务栏中显示任何内容
【问题讨论】:
-
您是在谈论您自己进程中的 WPF 窗口吗?或者您要恢复哪些窗口?
-
您可以使用进程间消息连接到您的其他实例并告诉它取消隐藏 - pipe messageing on msdn。本质上,您创建了一个侦听某个名称的管道服务器,如果您的应用程序重新启动,您使用全局命名互斥锁来检查它是否已启动 - 如果是,您会触发“取消隐藏”消息并终止自己。通过使用全局互斥锁,您根本不需要查询进程列表。
-
@mm8 是 WPF 窗口
-
那你为什么不直接访问窗口呢?
-
@mm8 很抱歉造成混淆,但问题是我的窗口被隐藏了,并且只有任务管理器中的进程。当我再次运行相同的 exe 时,我想显示第一个 exe 并关闭它。