如何从进程中恢复隐藏的 wpf 窗口
Posted
技术标签:
【中文标题】如何从进程中恢复隐藏的 wpf 窗口【英文标题】:How to restore the hidden wpf window from process 【发布时间】: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 并关闭它。 【参考方案1】:这是一个如何使用管道的示例。
如果您以非常快的速度启动实例,除此之外使用命名互斥体可以解决一些问题(在这种情况下,您最终可能会拥有超过 1 个服务器 - 所以尽早创建一个命名互斥体,如果存在的话 - 只需发送Show() 在管道上并关闭。
从 VS2017 默认的 Wpf-Project 创建。编译它,启动 Debug-Build,转到输出文件夹并再次启动 exe 以显示它的作用。
MainWindow.xaml
<Window x:Class="interproc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:interproc"
mc:Ignorable="d"
Title="MainWindow"
Height="350"
Width="525"
Loaded="Window_Loaded">
<Grid>
<TextBlock Name="tb"
Background="Yellow"
Text="..."
Margin="50" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.IO.Pipes;
using System.Threading;
using System.Windows.Threading;
namespace InterprocessCommunicationViaPipes
/// <summary>
/// Commands used to communiate via pipe
/// </summary>
public enum EPipeCommands : byte
None, Show, Hide, Close
;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
Title = DateTime.UtcNow.ToString("o");
/// <summary>
/// Name of the pipe used for interprocess communication
/// </summary>
private const string PipeName = "MyCoolApp";
/// <summary>
/// prevents double Close() calls
/// </summary>
private bool isClosing = false;
/// <summary>
/// Server
/// </summary>
private NamedPipeServerStream pipeServerStream = null;
/// <summary>
/// Thread server is running in
/// </summary>
private Thread ServerThread;
void ActOnPipeCommand(EPipeCommands c)
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new ThreadStart(
delegate
tb.Text += $"\nDateTime.UtcNow:o recieved c\n";
switch (c)
case EPipeCommands.None:
return;
case EPipeCommands.Hide:
Hide();
break;
case EPipeCommands.Show:
if (this.WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Visibility = Visibility.Visible;
break;
case EPipeCommands.Close when !isClosing:
Close();
break;
case EPipeCommands.Close:
tb.Text += $"Already closing.\n";
break;
default:
tb.Text += $"Unmapped pipe action: c.ToString()\n";
break;
));
/// <summary>
/// Server running?
/// </summary>
/// <returns></returns>
bool CheckIsRunning()
NamedPipeClientStream clientStream = new NamedPipeClientStream(PipeName);
try
clientStream.Connect(1000);
catch (TimeoutException)
tb.Text = $"No Server found.";
return false;
clientStream.WriteByte((byte)EPipeCommands.Show);
return true;
EPipeCommands InterpretePipeCommand(int v)
if (Enum.TryParse<EPipeCommands>($"v", out var cmd))
return cmd;
return EPipeCommands.None;
/// <summary> Creates the server, listens to connectiontrys,
/// reads 1 byte & disconnects </summary>
/// <param name="data"></param>
void PipeServer(object data)
pipeServerStream = new NamedPipeServerStream(
PipeName, PipeDirection.InOut,
2, PipeTransmissionMode.Byte);
do
pipeServerStream.WaitForConnection();
if (pipeServerStream.IsConnected && !isClosing)
ActOnPipeCommand(
InterpretePipeCommand(
pipeServerStream.ReadByte()));
pipeServerStream.Disconnect();
while (!isClosing);
private void Window_Loaded(object sender, RoutedEventArgs e)
if (CheckIsRunning())
Close();
else
ServerThread = new Thread(PipeServer);
ServerThread.Start();
tb.Text = "Starting new pipe server.\n";
Closing += (a, b) => isClosing = true;
【讨论】:
以上是关于如何从进程中恢复隐藏的 wpf 窗口的主要内容,如果未能解决你的问题,请参考以下文章
在WPF中,当在无边框窗口中拖动自定义的标题栏时,窗口会恢复,如何实现呢?
Windows 睡眠/恢复后 WPF Windows 无响应