Solved

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

Posted on 2001-07-19
11
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6300723
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
ID: 6300939
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
ID: 6301261
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 6

Expert Comment

by:JonFish85
ID: 6301270
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
ID: 6301426
Or use the GetDlgItem API function.
0
 
LVL 1

Expert Comment

by:khampton
ID: 6302678
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
ID: 6302713
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
ID: 6302945
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
ID: 6303232
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
ID: 6303234
P.S. I refer you back to my first post.
0
 

Author Comment

by:petlev
ID: 6303832
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

724 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