Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Force Window to Front (not blink in taskbar)

API Functions

Force Window to Front (not blink in taskbar)

by  craigsboyd  Posted    (Edited  )
Slighthaze = [color blue]NULL[/color]

You may have noticed that when you try and bring your application's window to the front using API calls (SetForeGroundWindow and BringWindowToTop) that it just blinks down in the taskbar. This applies to Windows 2000 and later Microsoft OS's (see MS Documentation below). Well, if you would rather have your applicaiton's window truly come to the front (and I'm sure you do) then here's a VFP workaround using API calls.

MS Documentation for SetForegroundWindow reads as follows:
"Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. Instead, SetForegroundWindow will activate the window (see SetActiveWindow) and call the FlashWindowEx function to notify the user."

...and it appears that the same is true for BringWindowToTop.

The only way Windows 2000 and Windows XP let you bring your application's window up to the front is if the thread it is running on is the thread of the foreground window at the time. So, you have to attach the thread of your application to the thread of the foreground window and then bring your application's window to the front. After that you need to detach your thread (this all only happens if the user has another application's window as the foreground active window). It looks like a lot, but really we are just tricking Windows into thinking that our application window is part of the foreground application so that it will let us bring our window up to the front rather than having it just blink in the taskbar. The key to this whole thing is the AttachThreadInput API calls.

Code:
DECLARE Long FindWindow In Win32API String, String

nHWND = FindWindow(Null,_screen.caption)

If nHWND >0
    =ForceForegroundWindow(nHWND)
ENDIF

FUNCTION ForceForegroundWindow(lnHWND)

    LOCAL nForeThread, nAppThread
    
    =decl()
    
    nForeThread = GetWindowThreadProcessId(GetForegroundWindow(), 0)
    nAppThread = GetCurrentThreadId()

    IF nForeThread != nAppThread
        AttachThreadInput(nForeThread, nAppThread, .T.)
        BringWindowToTop(lnHWND)
        ShowWindow(lnHWND,3)
        AttachThreadInput(nForeThread, nAppThread, .F.)
    ELSE
        BringWindowToTop(lnHWND)
        ShowWindow(lnHWND,3)
    ENDIF
    
ENDFUNC

FUNCTION Decl
*!*        DECLARE INTEGER SetForegroundWindow IN user32 INTEGER hwnd  
    DECLARE Long BringWindowToTop In Win32API Long

    DECLARE Long ShowWindow In Win32API Long, Long

    DECLARE INTEGER GetCurrentThreadId; 
        IN kernel32
     
    DECLARE INTEGER GetWindowThreadProcessId IN user32; 
        INTEGER   hWnd,; 
        INTEGER @ lpdwProcId  
     
    DECLARE INTEGER GetCurrentThreadId; 
        IN kernel32  
        
    DECLARE INTEGER AttachThreadInput IN user32 ;
        INTEGER idAttach, ;
        INTEGER idAttachTo, ;
        INTEGER fAttach

    DECLARE INTEGER GetForegroundWindow IN user32  
    
ENDFUNC
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top