Win32 SetForegroundWindow unreliable

前端 未结 7 568
时光取名叫无心
时光取名叫无心 2020-12-05 08:17

I have a rather complex series of applications which depend on the ability to switch applications in the foreground.

My problem is, every 5 or 6 times of switching t

7条回答
  •  囚心锁ツ
    2020-12-05 08:31

    Your AttachThreadInput() hack is (I think) a known way to defeat the focus stealing counter-measures in Windows. You are using the wrong handle though, you want to attach to the thread that currently has the focus. Which won't be hApp, you wouldn't need this code otherwise.

    Use GetForegroundWindow() to get the handle to the window with the focus.

    AttachThreadInput(
        GetWindowThreadProcessId(GetForegroundWindow(), NULL),
        GetCurrentThreadId(), TRUE
    );
    

    Although I think the 2nd argument needs to be thread ID of hApp. Because you don't want to shove your own window if I understood correctly. Not sure if that can work.

提交回复
热议问题