Page 1 of 1

Real SetForegroundWindow()

Posted: Wed Sep 03, 2003 9:05 am
by Fred
Code updated For 5.20+

Here is a snippet I need for my work, to really put a window in the foreground, instead of flashing taskbar for Win98/2000 or XP. Warning, use it with care and only when needed, as it's not very smart to grab the user input without notice :)

Code: Select all

; Code by Elvis Rox erox@etree.com

Procedure ReallySetForegroundWindow(Window)

  hWnd = WindowID(Window)

  ; If the window is in a minimized state, maximize now
  
  If GetWindowLong_(hWnd, #GWL_STYLE) & #WS_MINIMIZE
    ShowWindow_(hWnd, #SW_MAXIMIZE)
    UpdateWindow_(hWnd)
  EndIf
  
  ; Check To see If we are the foreground thread
  
  foregroundThreadID = GetWindowThreadProcessId_(GetForegroundWindow_(), 0)
  ourThreadID = GetCurrentThreadId_()
  ; If not, attach our thread's 'input' to the foreground thread's
  
  If (foregroundThreadID <> ourThreadID)
    AttachThreadInput_(foregroundThreadID, ourThreadID, #TRUE);
  EndIf
  
  ; Bring our window To the foreground
  SetForegroundWindow_(hWnd)
  
  ; If we attached our thread, detach it now
  If (foregroundThreadID <> ourThreadID)
    AttachThreadInput_(foregroundThreadID, ourThreadID, #FALSE)
  EndIf  
  
  ; Force our window To redraw
  InvalidateRect_(hWnd, #NULL, #TRUE)
EndProcedure 

Re: Real SetForegroundWindow()

Posted: Wed Sep 03, 2003 9:20 am
by PB
> Here is a snippet I need for my work, to really put a window in the
> foreground, instead of flashing taskbar for Win98/2000 or XP.

Hey, check out my tip from June 2002:

viewtopic.php?t=3751

:twisted:

Posted: Wed Sep 03, 2003 3:57 pm
by Fred
Arf :)

Posted: Thu Sep 04, 2003 1:17 am
by PB
:lol:

Posted: Thu Sep 04, 2003 8:06 am
by Berikco
Image

Re: Real SetForegroundWindow()

Posted: Mon Feb 24, 2014 3:03 pm
by chi
rock-solid :!:

Code: Select all

#LSFW_UNLOCK = 2
#ASFW_ANY = -1

Procedure.i ForceWindowIntoForeground(window.i)
  Protected currentThread.i, activeWindow.i, activeProcess.i, activeThread.i, windowProcess.i, windowThread.i, oldTimeout.i, newTimeout.i
  currentThread = GetCurrentThreadId_();
  activeWindow = GetForegroundWindow_();
  activeThread = GetWindowThreadProcessId_(activeWindow, @activeProcess);
  windowThread = GetWindowThreadProcessId_(window, @windowProcess);
  If currentThread <> activeThread
    AttachThreadInput_(currentThread, activeThread, #True)
  EndIf
  If windowThread <> currentThread
    AttachThreadInput_(windowThread, currentThread, #True)
  EndIf
  oldTimeout = 0
  newTimeout = 0
  SystemParametersInfo_(#SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @oldTimeout, 0);
  SystemParametersInfo_(#SPI_SETFOREGROUNDLOCKTIMEOUT, 0, @newTimeout, 0);
  LockSetForegroundWindow_(#LSFW_UNLOCK);
  AllowSetForegroundWindow_(#ASFW_ANY);
  SetForegroundWindow_(window);
  If IsIconic_(window)
    ShowWindow_(window, #SW_RESTORE);
  EndIf
  SystemParametersInfo_(#SPI_SETFOREGROUNDLOCKTIMEOUT, 0, @oldTimeout, 0);
  If currentThread <> activeThread
    AttachThreadInput_(currentThread, activeThread, #False)
  EndIf
  If windowThread <> currentThread
    AttachThreadInput_(windowThread, currentThread, #False)
  EndIf
EndProcedure
cheers, chi

Re: Real SetForegroundWindow()

Posted: Mon Feb 24, 2014 3:46 pm
by chi
@PB: There are certain circumstances where the process is locked (LockSetForegroundWindow) or the lock time-out has not expired (SPI_GETFOREGROUNDLOCKTIMEOUT)...
More info @ http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Re: Real SetForegroundWindow()

Posted: Mon Feb 24, 2014 3:55 pm
by c4s
Offtopic:
Previous posting date wrote:Thu Sep 04, 2003
Your posting date wrote:Mon Feb 24, 2014
Wow, have some respect for the dead! :shock:

Re: Real SetForegroundWindow()

Posted: Mon Feb 24, 2014 4:03 pm
by chi
Code updated For 5.20+
Fred dug him up first ;)

Re: Real SetForegroundWindow()

Posted: Mon Feb 24, 2014 4:25 pm
by PB
:lol: