Solved

Bring Window into focus

Posted on 2010-09-09
12
725 Views
Last Modified: 2013-12-26
I need the code in vb6 to bring a webpage in IE out of the tray and into focus and active and maximize and on top.
0
Comment
Question by:rjef
  • 6
  • 5
12 Comments
 
LVL 8

Expert Comment

by:ragnarok89
ID: 33640546
place this in a module :

Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long


place this in the event :

BringWindowToTop(WebBrowser1.Handle)

0
 

Author Comment

by:rjef
ID: 33640643
I am not using webbrowser1
0
 
LVL 6

Expert Comment

by:judgeking
ID: 33655807
I think this will do what you want:
Public Sub ActivateApp(Title As String, Optional Maximize As Boolean = False)

  Dim lHWnd As Long

  

  AppActivate Title, False 'ie: "Google"



  If Err.Number = 0 Then

    lHWnd = GetForegroundWindow

    

    If lHWnd <> 0 Then

      If Maximize Then

        SendMessage lHWnd, WmSysCommand, ScMaximize, 0

        Call ShowWindow(lHWnd, SwShowMaximized)

      Else

        SendMessage lHWnd, WmSysCommand, ScRestore, 0

        Call ShowWindow(lHWnd, SwRestore)

      End If

      

      BringWindowToTop lHWnd

    End If

  End If

End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:judgeking
ID: 33655811
Sorry, you'll need these declarations too:
Public Const SwShowMinNoActive = 7

Public Const SwShowNA = 8

Public Const SwRestore = 9

Public Const SwShowDefault = 10



Public Const ScSize = &HF000&

Public Const ScSeparator = &HF00F&

Public Const ScMove = &HF010&

Public Const ScMinimize = &HF020&

Public Const ScMaximize = &HF030&

Public Const ScClose = &HF060&

Public Const ScRestore = &HF120&

Public Const ScScreensave = &HF140&



Public Const WmSysCommand = &H112&



Public Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Open in new window

0
 

Author Comment

by:rjef
ID: 33661872
it doesn't work everytime.  Sometimes i have to call it 2 or 3 times to bring it back into focus after i have minimized it.  Also it doesn't maximize it at all.
0
 

Author Comment

by:rjef
ID: 33661904
maybe i am not doing it correclty.  I put the appactive sub into a command buton
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:judgeking
ID: 33662518
Looks like the whole Declare section didn't paste properly above, you're probably getting an error on SwShowMaximized not being defined.  Here's the whole thing again:

My form code is just:

Option Explicit

Private Sub Command1_Click()
  ActivateApp "Google", True
End Sub

Option Explicit



Public Const HwndTopmost = -1

Public Const HwndNoTopmost = -2

Public Const SwpNoMove = &H1

Public Const SwpNoSize = &H2

Public Const SwpNoActivate = &H10

Public Const SwpShowWindow = &H40



Public Const SwHide = 0

Public Const SwNormal = 1

Public Const SwShowMinimized = 2

Public Const SwShowMaximized = 3

Public Const SwShowNoActivate = 4

Public Const SwShow = 5

Public Const SwMinimize = 6

Public Const SwShowMinNoActive = 7

Public Const SwShowNA = 8

Public Const SwRestore = 9

Public Const SwShowDefault = 10



Public Const ScSize = &HF000&

Public Const ScSeparator = &HF00F&

Public Const ScMove = &HF010&

Public Const ScMinimize = &HF020&

Public Const ScMaximize = &HF030&

Public Const ScClose = &HF060&

Public Const ScRestore = &HF120&

Public Const ScScreensave = &HF140&



Public Const WmSysCommand = &H112&



Public Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long



Public Sub ActivateApp(Title As String, Optional Maximize As Boolean = False)

  Dim lHWnd As Long

  

  AppActivate Title, False 'ie: "Google"



  If Err.Number = 0 Then

    lHWnd = GetForegroundWindow

    

    If lHWnd <> 0 Then

      If Maximize Then

        SendMessage lHWnd, WmSysCommand, ScMaximize, 0

        Call ShowWindow(lHWnd, SwShowMaximized)

      Else

        SendMessage lHWnd, WmSysCommand, ScRestore, 0

        Call ShowWindow(lHWnd, SwRestore)

      End If

      

      BringWindowToTop lHWnd

    End If

  End If

End Sub

Open in new window

0
 

Author Comment

by:rjef
ID: 33662846
i had to put all your public const and public declares into a module.  It maximizes now put doesn't always bring it into focus on the first click.  I have to sometimes click several times.
0
 
LVL 6

Expert Comment

by:judgeking
ID: 33663159
You copied the code from my latest example?  If you're just maximizing an IE window, what's the name of the window?
0
 

Author Comment

by:rjef
ID: 33663477
yes  
i was using the google example
0
 

Author Comment

by:rjef
ID: 33663513
if i try to use your code directly i get the attached.
Presentation1.jpg
0
 
LVL 6

Accepted Solution

by:
judgeking earned 500 total points
ID: 33663621
Yeah, the main code needs to be in a module and just the Command1_Click is in the form.  But it works for me every time.  Maybe you could minimize your VB window, just to be safe:

Private Sub Command1_Click()
  Me.WindowState = vbMinimized
  ActivateApp "Google", True
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now