[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

How to get a handle to a control using Control ID?

I want to be able to get a handle for controls like button, checkbox, radio button, editbox, and all I've got is a Control ID. Is there a function I could use within VB to get a handle to a control?
0
petlev
Asked:
petlev
  • 3
  • 3
  • 2
  • +2
1 Solution
 
JonFish85Commented:
these controls have a "hWnd" property, so you can go:

Button1.hWnd
Text1.hWnd

etc. Hope this helps!
0
 
KDivadCommented:
Depends on the control in question. If it is in your app, then it's a simple as Jon's post. If not, then an idea of how you are getting the control ID might help.

There are a rather large number of ways to get an hWnd. Your code to get the control ID may be easily modified to get the hWnd while it's getting the ID.
0
 
khamptonCommented:
Please note that not all controls have a hwnd.  Some of them are "lightweight" and are not considered true windows.  Examples are the command button, the label, etc.  

I'm not sure what a control ID is.  But any control on your form either has a hwnd or not.  If not, (and you can tell this by the method described by previous experts) nothing can be done to get it because it doesn't exit.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JonFish85Commented:
khampton:

both the label and the command button have hWnd properties as far as I know! at least in vb6. some examples would be the Shape and Line controls... Correct me im wrong tho!
0
 
CJ_SCommented:
Or use the GetDlgItem API function.
0
 
khamptonCommented:
My error, the command button has a hwnd but the label does not.  Nor does the shape and line controls.  Also, the image control doesn't have a hwnd.
0
 
khamptonCommented:
If by control id you mean caption, here is how to use an API call to get the hwnd:

Private Declare Function FindWindow Lib "user32" Alias _
         "FindWindowA" (ByVal lpClassName As String, _
 ByVal lpWindowName As String) As Long


...

private function GetWindowHWND(byval Caption as string)as long

    GetWindowHwnd =FindWindow(vbNullString,Caption)
end function
0
 
petlevAuthor Commented:
The controls in question are of other application.
Controls like button have handles, ordinals, IDs; unlike handle control ID is a static unique number assigned to a control when control is created.
0
 
KDivadCommented:
I understand what a control ID is. I've played with it before.

No hWnd: Labels, Shapes, Lines, Images.

Labels should since Windows has a control called Static which is the same thing, but VB (5 at least) doesn't play that way.
0
 
KDivadCommented:
P.S. I refer you back to my first post.
0
 
petlevAuthor Commented:
GetDlgItem is a correct function to use. I tried it and it worked. The function requires a dialog control handle or parent window handle. CJ S has provided any examples but I was able to figure it out. Here is an implementation of the function.


Public Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private lngHwnd As Long

Private Function Get_Control_Handle(ByVal intControlID As Integer) As Long

   
On Error GoTo Error_Handler
   
    Dim lngHandle As Long

    lngHandle = GetForegroundWindow
   
    lngHandle = EnumChildWindows(lngHandle, AddressOf EnumWinProc, intControlID)
   
    Get_Control_Handle = lngHwnd
   
Function_Exit:
   
    Exit Function
   
Error_Handler:
   
    Get_Control_Handle = 0
   
End Function
'**************************************************************************************************
'
'
'**************************************************************************************************


Private Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
   
On Error GoTo Error_Handler

    Dim lngHandle As Long
   
    lngHandle = GetDlgItem(hwnd, lParam)
       
        If lngHandle <> 0 Then
       
            EnumWinProc = 0
            lngHwnd = lngHandle

        Else
           
            EnumWinProc = 1
           
        End If

Function_Exit:

    Exit Function
   
Error_Handler:
   
    EnumWinProc = 0

End Function

Private Function IclsButton_ButtonClick(ByVal intControlID As Integer, ByVal lngTimeout As Long) As Boolean
   
On Error GoTo Error_Handler
   
    Dim lngButtonHandle As Long
    Dim lngReturnValue As Long
   
    IclsButton_ButtonClick = True
   
    lngButtonHandle = Get_Control_Handle(intControlID)
   
    If lngButtonHandle <> 0 Then
   
        lngReturnValue = SendMessage(lngButtonHandle, WM_LBUTTONDBLCLK, 0, 0)
   
    Else
   
    End If
   
Function_Exit:

    Exit Function
   
Error_Handler:
   
    IclsButton_ButtonClick = False

End Function
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now