我正在编写一个程序来显示/隐藏某些目标应用程序的窗口。我之前测试过它并注意到一些奇怪的事情。如果我以管理员身份运行目标应用程序(右键单击 - >属性,“兼容性”选项卡,“以管理员身份运行此程序”),它不起作用。
为了演示,我编写了一个名为“TargetApplication”的简单 GUI 应用程序,然后编写了以下代码来测试显示/隐藏该应用程序:
class Program
{
static void Main(string[] args)
{
IntPtr windowPtr = FindWindow(null, "TargetApplication");
ShowWindow(windowPtr, 0); // 0 = Hide
Console.WriteLine("The window is now hidden. Press Enter to restore");
Console.ReadLine();
ShowWindow(windowPtr, 9); // 9 = Restore
Console.WriteLine("The window is now restored. Press Enter to exit.");
Console.ReadLine();
}
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
如果我在没有管理员权限的情况下启动窗口应用程序,它将无法工作。
有人介意帮我测试一下吗?我已在此处上传了两个应用程序的 .exe:
测试显示窗口 下载 https://www.box.com/s/k0y8kcb1u8bjoxi4q60j
您所要做的就是下载它们并运行 TestApplication.exe,然后运行 TestShowWindow.exe。您会发现,将 TestApplication.exe 更改为以管理员身份运行会导致 ShowWindow 不再工作。
当然,如果您不信任下载我的东西,您可以随时编译我的代码并在 Windows 中您能够更改兼容模式的任何目标应用程序上进行测试。
附:我不确定这是否有影响,但我运行的是 Windows 8 Pro。 64 位。