Solved

Finding Window Handle

Posted on 2000-03-10
20
174 Views
Last Modified: 2010-05-02
After making the following call:

Call Shell(GetSetting(App.EXEName, "Configuration", "VideoButton" & Str(Index)), vbNormalFocus)

does anyone know a way in which I can grab the handle of the window without using the title of the window that is on the top bar of the window?
0
Comment
Question by:ChrisH
  • 9
  • 9
  • 2
20 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 total points
ID: 2604871
"HOWTO: Find a Window Handle from an Instance Handle"

http://support.microsoft.com/support/kb/articles/Q242/3/08.ASP
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2604919
ChrisH,

I already gave that to you in your "Window Precedence" question.... you posted this new question too soon. ;-)

Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2604931
By the way, I posted it a minute before Erick posted this answer, and almost an hour before you posted the question...


Cheers!®©

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Comment

by:ChrisH
ID: 2605211
I have a question.  I used this link.. and everything seems to be working fine, except the GetWindowText portion.  The handle to the window is returned just fine.. yet for some reason, nothing (zero) gets returned from the GetWindowText function.  Any idea why?  I used the exact same code the Microsoft site had.  Also.. do I need to get the name of the window if I have the handle?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605291
I tried it as well and it works.

MS forgot to add
Option Explicit
to the top of both the form's code and the module code.
Add it and see if there are any compile errors in your code.
You only need the hWnd of a window if you are using API which require it.  The Title is for the user only.
What are you doing with the shelled application that you need the hwnd for?

This worked for me:

Sub Command1_Click()
   Dim hInst As Long             ' Instance handle from Shell function.
   Dim hWndApp As Long           ' Window handle from GetWinHandle.
   Dim buffer As String          ' Holds caption of Window.
   Dim numChars As Integer       ' Count of bytes returned.
   
   ' Shell to an application
   hInst = Shell("notepad.exe")
   
   ' Begin search for handle
   hWndApp = GetWinHandle(hInst)
   
    If hWndApp <> 0 Then
        ' Init buffer
        buffer = Space$(128)
        ' Get caption of window
        numChars = GetWindowText(hWndApp, buffer, Len(buffer))
        buffer = Left(buffer, numChars)
        Debug.Print buffer
        ' Display window's caption
        MsgBox "You shelled to the Application: " & buffer
    End If
End Sub
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605301
Also, GetWindowText function assumes you are passing the handle of a normal window with a title bar.
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605321
Basically, I'm opening up internet explorer from my main app when the user clicks a button.  I want the IE to always be on top of the main application.  Actually.. this is just one of many cases that could possibly happen.  I have a registry setting which could hold any file, and I want to be able to query the registry for the setting and open it.. then give the opened window focus over the main application.
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605433
Yea.. with further testing I've found that if I launch the calculater or notepad, the window gets focus over the calling app.  However, when opening the internet explorer, the IE window will not take precedence.  Any thoughts to why one window would take focus where another won't?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605437
Well, you have specified vbNormalFocus in the Shell call, so the shelled app will open with focus.  It will not "be always on top" of your app if the user selects your app.  Is this what you want?
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605472
Yep.. I want it always on top.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605489
I do not see that behavior. IE opens with focus.
You can set your shelled app explicitly as the foreground window by calling:

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

'~~~
Call SetForeGroundWindow(hWndApp)
'~~~
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605524
Adjusted points to 200
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605525
IE does open with focus.. but it doesn't remain always ontop for some reason..
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605569
A window can be set to be always on top, but it will be always on top of all applications.  If this is what you want:

"HOWTO: Create a Form That Always Stays on Top"
http://support.microsoft.com/support/kb/articles/Q184/2/97.ASP

Just modify the code with the window handle you want to target:
lR = SetTopMostWindow(hWndApp, True)
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605599
Yea.. I was already doing that.. I've tracked the problem down to the fact that the handle I get from the process ID of the internet explorer is just plain wrong.  I don't understand why this would happen.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605702
Try this:
After the call to shell, insert a short pause to allow the application to load.  IE may be starting with a splash screen, etc.

'Module declare:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


'After Shell
Call Sleep(2000) 'Sleep 2 secs
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605796
That didn't work either.. I norrowed the problem down even furter.  It seems like there is a parent window that has the browser as a subwindow or something to this effect.. When I launched IE.. I got the process ID.. and then the handle.  When I look up the handle in Spy++, I get some "Auto Suggestion Drop Down Box" window that has that handle. If I open up the subdirectory of that window.. it shows several other windows including the Internet Explorer.  Now, the caption of the IE is "Internet Explorer".  Is there a way for me to find the handle of this Suggestion Drop box.. and then search for the Internet Explorer handle withing the subwindows of the Suggestion Drop box?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2605808
Actually, since you are shelling the app to show with focus, you could simply call GetForegroundWindow to get its handle.  This is more reliable for IE.

Code:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Sub Command1_Click()
   Dim hInst As Long
   Dim hWndApp As Long
   Dim buffer As String
   Dim numChars As Integer
   
   ' Shell to an application
   hInst = Shell("c:\program files\microsoft internet\ie20.exe", vbNormalFocus)
   ' Wait a bit
   Call Sleep(3000)
   ' Get the app with focus
   hWndApp = GetForegroundWindow
   If hWndApp <> 0 Then
      ' Init buffer
      buffer = Space$(128)
      ' Get caption of window
      numChars = GetWindowText(hWndApp, buffer, Len(buffer))
      ' Display window's caption
      MsgBox "You shelled to the Application: " & Left$(buffer, numChars)
   End If
End Sub
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605982
Thanks.. that did the trick!
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2606039
Glad to help
Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

778 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