View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Window Class of UserForm controls

Bob,

Not all controls have HWnds. Most MSForms are windowless contrrols. As far
as I know, only the Listbox and Frame controls have distinct HWnds. All
other controls share the same HWnd. For example, create a form with a
variety of controls, and use the following code in the user form.

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
ByVal HWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Function ClassName(HWnd As Long) As String
Dim CN As String
Dim N As Long
CN = String$(256, vbNullChar)
N = GetClassName(HWnd, CN, 256)
ClassName = Left$(CN, N)
End Function

Private Sub CommandButton1_Click()
Dim Ctrl As MSForms.Control
Dim HWndColl As New Collection

On Error Resume Next
For Each Ctrl In UserForm1.Controls
Err.Clear
Ctrl.SetFocus
If Err.Number < 0 Then
Debug.Print "Control: " & Ctrl.Name & " cannot use SetFocus"
Else
Err.Clear
HWndColl.Add Item:=Ctrl.Name, Key:=CStr(GetFocus)
If Err.Number = 0 Then
Debug.Print Ctrl.Name & " has HWnd of " & CStr(GetFocus) & _
" ClassName: '" & ClassName(GetFocus) & "'"
Else
Debug.Print Ctrl.Name & " has the same HWnd as " & _
HWndColl(CStr(GetFocus)) & _
" ClassName: " & ClassName(GetFocus)
End If

End If
Next Ctrl
End Sub


You'll get output like

Control: Label1 cannot use SetFocus
ComboBox1 has HWnd of 15599310 ClassName: 'F3 Server 60000000'
ListBox1 has HWnd of 29427562 ClassName: 'F3 Server 60000000'
OptionButton1 has the same HWnd as ComboBox1 ClassName: 'F3 Server
60000000'
CommandButton1 has the same HWnd as ComboBox1 ClassName: 'F3 Server
60000000'
CommandButton2 has the same HWnd as ComboBox1 ClassName: 'F3 Server
60000000'
CommandButton3 has the same HWnd as ComboBox1 ClassName: 'F3 Server
60000000'
Frame1 has HWnd of 17434558 ClassName: 'F3 Server 60000000'
CommandButton4 has the same HWnd as ComboBox1 ClassName: 'F3 Server
60000000'


Note that the same HWnd is used by multiple controls and that the controls
are of the same Window Class 'F3 Server 60000000'.

Use the following code for CommandButton2:

Private Sub CommandButton2_Click()
Me.ListBox1.SetFocus
Debug.Print Me.ActiveControl.Name, CStr(GetFocus), ClassName(GetFocus)

Me.ComboBox1.SetFocus
Debug.Print Me.ActiveControl.Name, CStr(GetFocus), ClassName(GetFocus)

Me.CommandButton1.SetFocus
Debug.Print Me.ActiveControl.Name, CStr(GetFocus), ClassName(GetFocus)

End Sub

And you'll get output like
ListBox1 18352062 'F3 Server 60000000'
ComboBox1 16516814 'F3 Server 60000000'
CommandButton1 16516814 'F3 Server 60000000'

Note that all the class names are the same and that ComboBox1 and
CommandButton1 have the same HWnd.

The short answer to the original question is that MSForms controls don't
have HWnds.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Bob Phillips" wrote in message
...
Here is a way to get the hWnd of userfcorm controls.

First create a class, I call it clsHWnd, with this simple code

Option Explicit

Public Name As String
Public hWnd As Long

Then in the userform, declare a couple of APIs and a collection

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

Private Declare Function GetFocus Lib "user32" () As Long

Private ListBoxCollection As Collection

and load it in the initialize event; add this

Private Sub Userform_Initialize()
Dim ctl As msforms.Control
Dim listHWnd As clshWnd
Dim meHWnd As Long
Dim res As Long

meHWnd = FindWindow("ThunderDFrame", Me.Caption)
If meHWnd = 0 Then
Exit Sub
End If

Set ListBoxCollection = New Collection
For Each ctl In UserForm1.Controls
ctl.SetFocus
Set listHWnd = New clshWnd
listHWnd.hWnd = GetFocus
listHWnd.Name = ctl.Name
ListBoxCollection.Add Item:=listHWnd, Key:=listHWnd.Name
Next ctl

End Sub

You can just get hWnd from the collection, like so

MsgBox ListBoxCollection("ListBox1").hWnd

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Martin" wrote in message
...
Can anyone tell me the API Window Class of controls in a userform? Or,

even
better, point me to a definitive list of the window classes in MS Office?

I'm trying to find a way to to identify the hWnd of the active control

(not
easy in VBA) so I can use context sensitive help.