Results 1 to 7 of 7

Thread: Click control to get Handle (Winspector style)

  1. #1
    Member
    Join Date
    Jun 10
    Posts
    45

    Click control to get Handle (Winspector style)

    Hi guys

    Id like to be able to click a button on an external window and a text box on my vb6 program be populated with that buttons handle, similar to the winspector drag and drop process.

    Im wondering if this is possible in VB6.

    Mods - Please move if this should be in the VB6 forum.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,780

    Re: Click control to get Handle (Winspector style)

    No. Clicking on a button of another application (un-related to your VB project) has no effect on your VB6 program. If, however, you have control of that other window (i.e. the source code) then you can get the button's handle by having that app send your VB program the button's handle. If you do not have any control then to get the handle of that button you will need to first get the handle of the other window by calling FindWindow API with the title bar name. Once you have the window handle you will need to set up a enumeration function in your program that loops through all the sub windows until it finds the button you need and then get the handle from there. This is complex code and I am not going to write the code to do that now as I don't have the time but I suggest you check into this by searching for how to get the handle of another window on the net (Google will have hits).
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  3. #3
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,137

    Re: Click control to get Handle (Winspector style)

    Quote Originally Posted by ripps View Post
    Hi guys

    Id like to be able to click a button on an external window and a text box on my vb6 program be populated with that buttons handle, similar to the winspector drag and drop process.

    Im wondering if this is possible in VB6.
    You'll need to use a few API calls. Add a vb timer to your form. In the timer use WindowFromPoint to get the handle of the control under the mouse cursor, use GetAsyncKeyState to check if the mouse button was clicked (I'd use a right or middle mouse click so you don't fire the apps button), when mouse click is detected stop the timer and set your text box to the value returned from WindowFromPoint.

  4. #4
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,780

    Re: Click control to get Handle (Winspector style)

    How's he going to know the X and Y for the WindowFromPoint (X, Y) function?
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  5. #5
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,137

    Re: Click control to get Handle (Winspector style)

    Quote Originally Posted by jmsrickland View Post
    How's he going to know the X and Y for the WindowFromPoint (X, Y) function?
    GetCursorPos , I thought that would be obvious! .

  6. #6
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,780

    Re: Click control to get Handle (Winspector style)

    Obvious to whom?
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  7. #7
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,780

    Re: Click control to get Handle (Winspector style)

    OK, taking what Edgemeal said here is the code that works

    Set your timer to Interval = 100

    Code:
    Private Type POINTAPI
      x As Long
      y As Long
    End Type
    
    Private MyPointAPI As POINTAPI
    
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    Private Const VK_RBUTTON = &H2
    
    Private ButtonhWnd As Long
    
    Private Sub Timer1_Timer()
     If GetAsyncKeyState(VK_RBUTTON) Then
       GetCursorPos MyPointAPI
       ButtonhWnd = WindowFromPoint(ByVal MyPointAPI.x, ByVal MyPointAPI.y)
       Text1.Text = ButtonhWnd
       Timer1.Enabled = False
     End If
    End Sub
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

Posting Permissions

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