Real SetForegroundWindow()

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Real SetForegroundWindow()

Post 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 
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Real SetForegroundWindow()

Post 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:
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Arf :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

:lol:
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

Image
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Real SetForegroundWindow()

Post 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
Et cetera is my worst enemy
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Real SetForegroundWindow()

Post 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
Et cetera is my worst enemy
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Real SetForegroundWindow()

Post 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:
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Real SetForegroundWindow()

Post by chi »

Code updated For 5.20+
Fred dug him up first ;)
Et cetera is my worst enemy
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Real SetForegroundWindow()

Post by PB »

:lol:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply