您需要知道窗口类和/或标题,然后可以使用 FindWindow 获取窗口的窗口句柄:
[DllImport("coredll.dll", EntryPoint="FindWindowW", SetLastError=true)]
private static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);
使用窗口句柄,您可以使用 SetWindowPos 将窗口改回正常显示:
[DllImport("user32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
例如,如果窗口的类名是“App 3”
...
IntPtr handle;
try
{
// Find the handle to the window with class name x
handle = FindWindowCE("App 3", null);
// If the handle is found then show the window
if (handle != IntPtr.Zero)
{
// show the window
SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
}
}
catch
{
MessageBox.Show("Could not find window.");
}
要查找窗口的类和标题,请在启动应用程序 3 时启动 CE 远程工具“CE Spy”(VS 安装的一部分)。然后浏览窗口列表并查看应用程序 3 窗口。双击列表中的条目,您将获得应用程序 3 的类名和标题。
除了额外的 SetWindowPos,您还可以使用简单的ShowWindow API:
[DllImport("coredll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
enum ShowWindowCommands
{
Hide = 0,
Normal = 1,
ShowMinimized = 2,
Maximize = 3, // is this the right value?
ShowMaximized = 3,
ShowNoActivate = 4,
Show = 5,
Minimize = 6,
ShowMinNoActive = 7,
ShowNA = 8,
Restore = 9,
ShowDefault = 10,
ForceMinimize = 11
}
...
IntPtr handle;
try
{
// Find the handle to the window with class name x
handle = FindWindowCE("App 3", null);
// If the handle is found then show the window
if (handle != IntPtr.Zero)
{
// show the window
ShowWindow(handle, ShowWindowCommands.Normal);
}
}
catch
{
MessageBox.Show("Could not find window.");
}
有关 FindWindow 和 SetWindowPos 的 pinvoke 的详细信息,请参阅pinvoke.net 和 MSDN。关于 Win32 编程的最佳书籍是 Charles Petzold 的 Programming Windows。
当您开始该过程时,您需要操作系统在更改窗口之前给一些时间来解决应用程序(比如说 1-3 秒)。