[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Finding Window Handle

Posted on 2000-03-10
20
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
  • 2
20 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 800 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

650 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