Classic VB/API - How can I find a window outside of my program?
<style type="text/css">span.vbk{font-weight:bold;color:darkblue;}span.small{font-size:8pt;}div.vbc{margin:15px auto;width:85%; background:white;border:1px dashed gray;padding: 5px;font-family:'Lucida Console',monospace;}</style><div style="text-align: justify;font-size: 10pt;font-family:'Trebuchet MS','Segoe UI',Tahoma,sans-serif;padding:0 15px;">Often the need arises to get the handle of a window outside your app, at runtime, whether it be to send it a message, hook it, change its appearance, etc.
First, to find it you need to know either its class name or caption. Since window captions often change at runtime it is a far safer bet to go with the class name.
To get the class name of a window, use a tool such as the Window Finder in Microsoft Spy++, which comes with Visual Studio. If you don't have it, you can grab Joacim Andersson's Window Finder tool from the Codebank here.
<center><img src="http://www.vbforums.com/attachment.php?attachmentid=39866"/></center>
Drag and drop the "Finder tool" (circled) over the window you wish to obtain the class name for. A black rectangle should appear around the border of the window. The class name will then appear in the Find Window dialog. Write this down somewhere.
To find this window at runtime (if it exists), you use the FindWindow() API function, and pass the classname of the window. However, be aware that if it is a control, you need to find first the handle of the window, then the handle of the control using the FindWindowEx() function. This requires knowing the class name of both the window and the control, so you need to repeat the process described above twice.
The following definitions need to be added in the "General" - "Declarations" area of a form or module (class or standard) in your project. If it is a form or class module then they will need to be declared as <span class="vbk">Private</span>.
<div class="vbc">Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWndParent As Long, _
ByVal hWndChildAfter As Long, _
ByVal lpszClassName As String, _
ByVal lpszWindowName As String _
) As Long
</div>
For a simple example, let's use Notepad. Notepad's window class name is simple: "Notepad". The class name of its textbox is "Edit".
<div class="vbc">' Find Notepad's handle
Dim hWndNotepad As Long
hWndNotepad = FindWindow("Notepad", vbNullString)
' Since we are using the class name and not the caption,
' null is passed for the window name.
' Find the handle of the textbox, within the Notepad window
Dim hWndTextbox As Long
hWndTextbox = FindWindowEx(hWndNotepad, 0&, "Edit", vbNullString)
</div>
And that's it. You can then use the handle(s) you have obtained (along with one or more additional API calls) for any of the purposes I mentioned in the first paragraph, and more. Have fun! :)</div>
Re: Classic VB/API - How can I find a window outside of my program?
can i have this source code please