Results 1 to 2 of 2

Thread: Classic VB/API - How can I find a window outside of my program?

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow Classic VB/API - How can I find a window outside of my program?

    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.



    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 Private.
    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


    For a simple example, let's use Notepad. Notepad's window class name is simple: "Notepad". The class name of its textbox is "Edit".
    ' 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)


    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!
    Last edited by penagate; Feb 17th, 2006 at 03:48 AM.

  2. #2
    Banned
    Join Date
    Nov 2016
    Posts
    23

    Re: Classic VB/API - How can I find a window outside of my program?

    can i have this source code please

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width