[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
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?
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.
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:
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sSave As String
sSave = Strings.Space$(GetWindowTextLength(hwnd) + 1) 'Get the windowtext length
GetWindowText hwnd, sSave, Len(sSave)
sSave = Strings.Left$(sSave, Len(sSave) - 1)
If sSave Like "The Buttons Caption" Then
'Record The Handle For Use As Needed
EnumChildProc = False
Exit Function
End If
EnumChildProc = True 'continue enumeration
End Function
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
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
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.