2013-04-18 91 views
0

我不明白为什么它不起作用?SetForegroundWindow不会激活我的窗口

static void ActivateApp(string processName) 
{ 
    Process[] p = Process.GetProcessesByName(processName); 

    // Activate the first application we find with this name 
    if (p.Any()) SetForegroundWindow(p[0].MainWindowHandle); 
    else 
    { 
     Console.WriteLine("Something wrong"); 
    } 
} 


    [STAThread] 
    static void Main(string[] args) 
    { 
     ActivateApp("Acrobat.exe"); 
    } 

输出:

有些事情不对

但我敢肯定,Acrobat.exe存在。

+2

将'Acrobat.exe'改为'Acrobat'。 –

+0

你的意思是不工作?例外?在控制台中出现“错误”的内容?请详细说明。 – jordanhill123

+0

谢谢@ByteBlast,jordanhill123,是有“蹊跷”,现在它走了,但我仍然窗口会显示不出来,也许我错了,但这种方法应该是把选择的过程Windows来所有其他Windows的顶部,对吧? – Ydhem

回答

2

有用于SetForegroundWindow()是否会实际工作一些奇怪的规则。

至少必须满足以下条件之一:

  • 过程是前台进程。
  • 该过程由前台进程启动。
  • 该进程收到最后一个输入事件。
  • 没有前台进程。
  • 正在调试前台进程。
  • 前景未锁定。
  • 前景锁定超时已过期(参见SPI_GETFOREGROUNDLOCKTIMEOUT在SystemParametersInfo)。
  • 没有菜单处于活动状态。

是这种情况?

的全部细节见the MSDN documentation