Results 1 to 7 of 7

Thread: [RESOLVED] Get A Handle For A Button Control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Resolved [RESOLVED] Get A Handle For A Button Control

    Shouldn't I be able to use EnumChildWindows to get the handles of all Button controls on a form?

    I'm currently passing the handle of my form to EnumChildWindows and all I'm not getting any handles for what I expected to be all the handles of each control on the form.

    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Get A Handle For A Button Control

    It should return all controls that have an hWnd property.. but in order for it to work you need to use a callback function.

    Assuming that you do have controls with hWnd's, what code are you currently using?

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get A Handle For A Button Control

    EnumChildWindows probably only enumerates direct children.

    children of children (e.g. a button inside a frame) wouldn't be returned.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Get A Handle For A Button Control

    None of my windows have handle properties.
    To get the handle to my main window, I enumerated all desktop children using enumchildwindows. I then tried to enumerate all children of my main window and got nothing.

    None of my windows have handles because I'm using VBA and VBA doesn't supply any handle properties; you have to get it yourself using API calls if you need it.

    Anyway, it seems to me that if I supply a valid handle to my main window I should be getting all child windows returned in my call back function.

    I know the call back function is working correctly because it is the same call back function I use to get the main windows handle. All I do is search for the window text like this:
    VB Code:
    1. Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    2.  
    3.     Dim sSave As String
    4.  
    5.     sSave = Strings.Space$(GetWindowTextLength(hwnd) + 1)   'Get the windowtext length
    6.     GetWindowText hwnd, sSave, Len(sSave)
    7.  
    8.     sSave = Strings.Left$(sSave, Len(sSave) - 1)
    9.  
    10.     If sSave Like "The Buttons Caption" Then
    11.             'Record The Handle For Use As Needed
    12.             EnumChildProc = False
    13.             Exit Function
    14.     End If
    15.  
    16.     EnumChildProc = True                            'continue enumeration
    17.  
    18. End Function

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get A Handle For A Button Control

    ok, EnumChildWindows will recurse children of children (my mistake), but since a lot of the MSForms controls (commandbuttons, textboxes etc.) are windowless you won't be able to use EnumChildWindows or FindWindowEx to get them.

    why do you need to do this anyway - perhaps there might be another answer if you explain your situation

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Get A Handle For A Button Control

    Thankyou Bushmobile.

    I hadn't thought about that possibility. The reason why I needed the handle was so I could set a hook and monitor for right clicks. Since there is no right click event procedure for these buttons, I needed to find some way to create a right-click event procedure. And, of course to monitor the messages sent to my hook procedure I need the button's handle.

    Well, if there's a better way and you're still out there, please let me know.

    For now, I'll go ahead and mark this as "resolved" so as to not to waste other peoples time.

    Thanks

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Get A Handle For A Button Control

    well, there will be right-click messages, but they will be handled by the parent window - use Spy++, or a similar window spy tool to monitor the messages being processed by the window and see if can figure out what's going on.

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