PureBasic Forum
http://forums.purebasic.com/english/

Real SetForegroundWindow()
http://forums.purebasic.com/english/viewtopic.php?f=12&t=7424
Page 1 of 1

Author:  Fred [ Wed Sep 03, 2003 9:05 am ]
Post subject:  Real SetForegroundWindow()

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:
; 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

Author:  PB [ Wed Sep 03, 2003 9:20 am ]
Post subject:  Re: Real SetForegroundWindow()

> 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:

http://forums.purebasic.com/english/vie ... php?t=3751

:twisted:

Author:  Fred [ Wed Sep 03, 2003 3:57 pm ]
Post subject: 

Arf :)

Author:  PB [ Thu Sep 04, 2003 1:17 am ]
Post subject: 

:lol:

Author:  Berikco [ Thu Sep 04, 2003 8:06 am ]
Post subject: 

Image

Author:  chi [ Mon Feb 24, 2014 3:03 pm ]
Post subject:  Re: Real SetForegroundWindow()

rock-solid :!:
Code:
#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

Author:  chi [ Mon Feb 24, 2014 3:46 pm ]
Post subject:  Re: Real SetForegroundWindow()

@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

Author:  c4s [ Mon Feb 24, 2014 3:55 pm ]
Post subject:  Re: Real SetForegroundWindow()

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:

Author:  chi [ Mon Feb 24, 2014 4:03 pm ]
Post subject:  Re: Real SetForegroundWindow()

Quote:
Code updated For 5.20+

Fred dug him up first ;)

Author:  PB [ Mon Feb 24, 2014 4:25 pm ]
Post subject:  Re: Real SetForegroundWindow()

:lol:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/