We help IT Professionals succeed at work.

Bring app to front

Belazir
Belazir asked
on
1,137 Views
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:
http://www.codeproject.com/dialog/dlgboxtricks.asp
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

Help...
B
Comment
Watch Question

Shanmuga SundaramDirector of Software Engineering
CERTIFIED EXPERT

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

Author

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

Author

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

Author

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

Author

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

Commented:
Did you try the AppActivate() function?

Author

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

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

Commented:
Even a form on another project.

Author

Commented:
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.
B
CERTIFIED EXPERT
Author of the Year 2009

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

Author

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

Cheers
B

Author

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

Author

Commented:
Helllo

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.

Thanks
B

Author

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

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.

OR

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.