Issue with SetForegroundWindow in .NET

后端 未结 4 842
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 07:17

I\'m using SetForegroundWindow API in .NET using PInvoke.

When I use the API while debugging in Visual Studio its works perfectly. But it doesn\'t work always when

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-02 07:30

    Register a Hot Key with RegisterHotKey. Choose carefully the Hot Key, as you must not interfere with existing (or future) application.

    When you need to steal the focus (but remember it's BAD), simulate the Hot Key with SendInput.

    Then, you will receive a WM_HOTKEY message, and during the processing of that message, you will be allowed to use SetForegroundWindow (I mean, it will success).

    You will have to store/remenber somewhere the HWND of the window to activate, between the call to SendInput and your processing of WM_HOTKEY, which is posted.

    References: Pressing a registered hotkey gives you the foreground activation love

提交回复
热议问题