Solved

Finding Window Handle

Posted on 2000-03-10
20
172 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
Comment Utility
"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
Comment Utility
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
Comment Utility
By the way, I posted it a minute before Erick posted this answer, and almost an hour before you posted the question...


Cheers!®©

0
 
LVL 1

Author Comment

by:ChrisH
Comment Utility
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
Comment Utility
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
Comment Utility
Also, GetWindowText function assumes you are passing the handle of a normal window with a title bar.
0
 
LVL 1

Author Comment

by:ChrisH
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yep.. I want it always on top.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Expert Comment

by:Erick37
Comment Utility
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
Comment Utility
Adjusted points to 200
0
 
LVL 1

Author Comment

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

Expert Comment

by:Erick37
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.. that did the trick!
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Glad to help
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

10 Experts available now in Live!

Get 1:1 Help Now