我正在编写一个程序,用于显示/隐藏某个目标应用程序的窗口。早些时候我在测试它时发现了一些奇怪的问题。如果我以管理员身份运行目标应用程序(右键单击->属性,“兼容性”选项卡,“以管理员身份运行此程序”),它无法正常工作。
为了展示这个问题,我编写了一个简单的GUI应用程序称为“TargetApplication”,然后编写了以下代码来测试显示/隐藏此应用程序:
为了展示这个问题,我编写了一个简单的GUI应用程序称为“TargetApplication”,然后编写了以下代码来测试显示/隐藏此应用程序:
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文件:
您只需下载并运行TestApplication.exe,然后运行TestShowWindow.exe。您会发现将TestApplication.exe更改为以管理员身份运行会导致ShowWindow不再起作用。
当然,如果您不信任下载我的东西,您始终可以编译我的代码并在Windows中更改兼容性模式以在任何目标应用程序上进行测试。
顺便说一句,我不确定这是否有区别,但我正在运行Windows 8 Pro.64位。