Solved

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

Posted on 2001-07-19
11
178 Views
Last Modified: 2012-08-14
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
Comment
Question by:petlev
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:JonFish85
Comment Utility
these controls have a "hWnd" property, so you can go:

Button1.hWnd
Text1.hWnd

etc. Hope this helps!
0
 
LVL 5

Expert Comment

by:KDivad
Comment Utility
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
 
LVL 1

Expert Comment

by:khampton
Comment Utility
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
 
LVL 6

Expert Comment

by:JonFish85
Comment Utility
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
 
LVL 22

Accepted Solution

by:
CJ_S earned 200 total points
Comment Utility
Or use the GetDlgItem API function.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:khampton
Comment Utility
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
 
LVL 1

Expert Comment

by:khampton
Comment Utility
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
 

Author Comment

by:petlev
Comment Utility
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
 
LVL 5

Expert Comment

by:KDivad
Comment Utility
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
 
LVL 5

Expert Comment

by:KDivad
Comment Utility
P.S. I refer you back to my first post.
0
 

Author Comment

by:petlev
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now