SetForegroundWindow does not activate my window

旧街凉风 提交于 2019-12-11 10:46:12

问题


I can't figure out why it is not working ?

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");
    }

Output :

Something is wrong

But I'm sure that Acrobat.exe exist.


回答1:


There are some weird rules for whether SetForegroundWindow() will actually work.

At least one of the following must be true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked.
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

Is this the case?

See the MSDN documentation for full details.



来源:https://stackoverflow.com/questions/16079440/setforegroundwindow-does-not-activate-my-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!