We help IT Professionals succeed at work.

Bring app to front

Belazir asked
Last Modified: 2010-04-29
I'm trying to use the below code to attach an app to the current foreground window, in order to get around SetForegroundWindow no longer working.  This is based on code I found here:
but I may have made a mistake in translation.

Should this work, and if so, what am I doing wrong?  The only difference is I'm trying to move a different window to the front rather than the one belonging to my app.

        FoundWindow = FindWindowEx(0, 0, vbNullString, "Equipment Explorer")
        If FoundWindow <> 0 Then
            'Workaround for SetForegroundWindow not working -
            'must attach EqE to current foreground window first
            lForegroundThread = GetWindowThreadProcessId(GetForegroundWindow(), 0)
            AttachThreadInput lForegroundThread, _
                GetWindowThreadProcessId(FoundWindow, 0), True
            SetForegroundWindow GetForegroundWindow() 'FoundWindow
            AttachThreadInput lForegroundThread, _
                GetWindowThreadProcessId(FoundWindow, 0), False
        End If

Watch Question

Shanmuga SundaramDirector of Software Engineering

if your only requirement is to bring your current window on the top then include one timer in your form and code as given below.

Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const HWND_TOPMOST = -1    'bring to top and stay there
Private Const HWND_NOTOPMOST = -2    'put the window into a normal position

Private Const SWP_NOMOVE = &H2    'don't move window
Private Const SWP_NOSIZE = &H1    'don't size window

Private Declare Function GetForegroundWindow Lib "user32" () As Long

Private Sub Form_Load()
Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
    If Me.hWnd <> GetForegroundWindow Then
        'Make this form be on top
        Call SetWindowPos(GetForegroundWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
        'Make the window on top below this form
        Call SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    End If

End Sub


Ah, you didn't read it through  : )

It's not my app I need to move to the front - my app is finding another app, which it then needs to bring to the front...


by the way, i'm not a beginner as the question says!  i'm "advanced", so i've tried all the obvious tricks, and i know bringing any window to the front is harder than it looks or ought to be...


it's all gone quiet here so i'll up the points


one more prod at the points, then i'll cancel the question and perhaps repost it - i guess nobody looks at questions over a certain age

Did you try the AppActivate() function?


AppActivate?  Is that a VB function?  I thought that was VBA only.  Either way I think it's relating to that application only isn't it?  I can't push a *different* running app to front?

AppActivate can be used with any form caption as the parameter.

Even a form on another project.


Hmmm... I'll give it a try but I'd be surprised if that works, when the APIs designed to do this sort of thing don't...

Leave with me though, it's worth a shot at least.
Author of the Year 2009

Hi Belazir.  Have you made any progress on this?
Director of Software Engineering
This one is on us!
(Get your first solution completely free - no credit card required)


I've not seen BringWindowToTop before, and oddly no-one's suggested it.  This seems to do something different for minimised windows, and perhaps that is the crux of the problem.

I will give this a shot and see how I get on.

Dan, I know this is taking some time to resolve, since I posted the question I've been dragged off onto other projects and have had no time to spend on this  : (   Not sure when I will either, so I'd appreciate some advice on what to do with the question - I will close it and award points if you want me to and test the code when I get chance, not sure that's the best approach for any party but let me know.



Sure, understand Dan.  I am indeed still active, just had my attention diverted.  I do intend to come back to this and I'm hoping it will be in the first couple of weeks in April.

Hope that's okay but please do let me know if you'd prefer me to delete the question at any point, I'll just have to make sure I've copied off the suggestion first.

Thanks & regards



Sorry it's been a while.  shasunder, I'm really keen to try this code out but I've moved to VB.NET since I last looked at this question, I don't actually have a VB6 environment left to play with.  I don't know if you have any expertise with .NET but I'd appreciate some pointers on how the Type statements change to Struct statements, as they seem to have been superseded.



I'll close this down, I haven't been able to pursue this and not sure when I'll get chance to.  Apologies.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.