windows forceforeground (bringWindowToTop) brings it to top but without being active (a forceForeGround that works with windows xp and windows 7)

Turns out that order is important here.
Correct order:

def set_foreground(hwnd)
if foreground_window != hwnd
foreground_thread = window_thread_process_id(foreground_window, nil)
other_thread = window_thread_process_id(hwnd, nil)
attach_thread_input(foreground_thread, other_thread, true)
set_foreground_window(hwnd)
set_active_window(hwnd)
bring_window_to_top(hwnd)
attach_thread_input(foreground_thread, other_thread, false)
end
end

Note well the order of these three:

set_foreground_window(hwnd)
set_active_window(hwnd)
bring_window_to_top(hwnd)

If you use these three in just this order, it appears to me that in windows 7 you don’t even *need* to do the attach_thread_input. I.e. you can put them *above* the thread stuff and it will work. Do it twice–why not?

2 thoughts on “windows forceforeground (bringWindowToTop) brings it to top but without being active (a forceForeGround that works with windows xp and windows 7)”

  1. also you may need to do

    set_foreground_window(hwnd)
    set_active_window(hwnd)
    bring_window_to_top(hwnd)

    *before* the thread manipulation stuff to get it to not stay on top even when you click back to the previously open window. phew!

    for google’s sake:

    WindowThreadProcessId
    AttachThreadInput
    SetForegroundWindow
    SetActiveWindow
    BringWindowToTop

  2. THANK YOU!!!!!!!! I have spent more time than I care to admit trying to figure out why BringWindowToTop was not bringing the window to the top! Much appreciated….

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.