In my app I want to bring another app's main window to the front.
The problem is when I call SetForegroundWindow the target window flashes on the taskbar without appearing to the front.

Any idea how to avoid this?
Posted on 2002-03-16 22:41:41 by cealnz
A brief look over in the Win32 API would suggest trying to use the "SetWindowPos" API first (setting its state to SHOW WINDOW), and then using the "SetForegroundWindow" API to bring it on top..

The SetWindowPos API actually makes mention of this to some extent in the remarks.. "If an application is not in the foreground, and should be in the foreground, it must call the SetForegroundWindow function."

Hope it helps..
:NaN:
Posted on 2002-03-18 07:32:24 by NaN
cealnz,

This works for me:

invoke ShowWindow, hWnd, SW_RESTORE
invoke SetForegroundWindow, hWnd

HTH

farrier
Posted on 2002-03-18 10:21:38 by farrier
SetWindowPos & ShowWindow will not solve this problem.
If the target window is flashing in the taskbar he is using
a version of windows later than 95. After 95 MS altered the
behaviour of SetForegroundWindow to activate the window
SetActiveWindow and then call FlashWindowEx to notify the
user that the window requires users attention. There is a
way to force an application to the foreground though, as
many MS appz themselves do! Here is a procedure I use to
perform this. The handle passed to the procedure is the
handle of the window you want to force to the foreground.
This works fine on WIN9se, but has not been tested on 2k
or xp (Would someone please test this on other Win versions
if they could please, I would like to know if works on other
versions as well)



ForceForegroundWnd PROC hWnd:HWND ; hWnd Is Handle Of Window To Set To Foreground

LOCAL ForegroundThreadID ; Foreground Window Thread ID
LOCAL hWndThreadID ; Requested Windows Thread ID

;--------------------------------
; Restore Window If Minimized |
;--------------------------------
Invoke IsIconic,hWnd ; Determine If Window Is Minimized
.IF eax!=NULL ; If Minimized
Invoke ShowWindow,hWnd,\ ; Restore Window
SW_RESTORE
Invoke UpdateWindow,hWnd ; Update Window
.ENDIF

;--------------------------------
; Get Window Thread ID's |
;--------------------------------
Invoke GetForegroundWindow ; Get Handle Of Current Foreground Window
Invoke GetWindowThreadProcessId,eax,NULL ; Get Foreground Windows Thread ID
mov ForegroundThreadID,eax ; Save Foreground Thread ID
Invoke GetWindowThreadProcessId,hWnd,NULL ; Get Requested Windows Thread ID
mov hWndThreadID,eax ; Save Requested Windows Thread ID

;--------------------------------
; Force Window To Foreground |
;--------------------------------
.IF eax!=ForegroundThreadID ; Requested Window Is Not Already Foreground Window
Invoke AttachThreadInput,\ ; Attach The Input Processing Mechanism
ForegroundThreadID,\ ; Of Foreground Window Thread To That
hWndThreadID,TRUE ; Of Window Thread To Bring To Foreground
Invoke SetForegroundWindow,hWnd; Bring Requested Window To Foreground
Invoke AttachThreadInput,\ ; Detatch Threads
ForegroundThreadID,\
hWndThreadID,FALSE
Invoke InvalidateRect,hWnd,\ ; Repaint Window
NULL,TRUE
.ENDIF
ret

ForceForegroundWnd ENDP


Willie
Posted on 2002-03-18 18:28:58 by WillASM
Actually, Win95 changed the behavior of SetForegroundWindow from the earlier NT 3 behavior. NT 4 kept the old behavior, and Win98 changed it back.
Posted on 2002-03-18 23:40:24 by tenkey