How do I bring an unmanaged application window to front, and make it the active window for (simulated) user input

后端 未结 3 911
滥情空心
滥情空心 2020-12-03 14:27

I am assuming I need to use pinvoke but I am not sure which function calls are needed.

Scenario: a legacy application will be running, I will have Handle for that ap

3条回答
  •  臣服心动
    2020-12-03 15:07

    This has proved to be extremely reliable. The ShowWindowAsync function is specifically designed for windows created by a different thread. The SW_SHOWDEFAULT makes sure the window is restored prior to showing, then activating.

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool ShowWindowAsync(IntPtr windowHandle, int nCmdShow);
    
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool SetForegroundWindow(IntPtr windowHandle);
    

    Then making the calls:

    ShowWindowAsync(windowHandle, SW_SHOWDEFAULT);
    ShowWindowAsync(windowHandle, SW_SHOW);
    SetForegroundWindow(windowHandle);
    

提交回复
热议问题