Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Microsoft: Visual FoxPro FAQ

API Functions

Force Window to Front (not blink in taskbar)
Posted: 4 Oct 03 (Edited 18 Feb 05)

Slighthaze = NULL

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

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close