关于VB6可视对象类名的问题
VB6中的窗体及控件等,基本上都有它自己特定的“类名”。
比如:
窗体的类名是 ThunderRT6FormDC
图片框的类名是 ThunderRT6PictureBoxDC
编辑框的类名是 ThunderRT6TextBox
不知道哪位高人,能否知道VB6中,有没它的‘内部常量’来表示这些类名的?
[最优解释]
上面的代码在没引用TLBINF32.DLL文件时无法运行,修正如下:
Public Sub EnumVBObjectClassName(frmTest As Form)
Dim i As Long, j As Long
Dim tApp As Object
Dim ti As Object
Dim cci As Object
Dim mi As Object
Dim strClassName As String
Dim ctl As Control
On Error Resume Next
If frmTest Is Nothing Then Exit Sub
Debug.Print "Form", GetWindowClassName(frmTest.hwnd)
Set tApp = CreateObject("TLI.TLIApplication")
Set ti = tApp.TypeLibInfoFromRegistry("{FCFB3D2E-A0FA-1068-A738-08002B3371B5}", 6, 0, 9)
For Each cci In ti.CoClasses
For i = 1 To cci.Interfaces.Count
For j = 1 To cci.Interfaces(i).Members.Count
Set mi = cci.Interfaces(i).Members(j)
If mi.Name = "hWnd" And mi.InvokeKind = 2 Then
Set ctl = frmTest.Controls.Add("VB." + cci.Name, "test_" + cci.Name + "_1")
If Err.Number = 0 And ctl.hwnd <> 0 Then
Debug.Print cci.Name, GetWindowClassName(ctl.hwnd)
frmTest.Controls.Remove ctl
Else
Err.Clear
End If
Exit For
End If
Next
Next
Next
End Sub
[其他解释]
木有,内部只需要使VB6中的类名就可以了啊,你要的话,GetWindowClass之类的函数来获取就可以了啊
------其他解决方案--------------------
不会有常量,原因很简单,OCX可以是官方的也可以是第三方的,如果官方需要定义一个常量来代表一个对象的话,那么第三方怎么办?你还让不让第三方开发OCX?
它不像message可以有常量来定义出所有消息的名称,因为这东西只是官方的,没听说过还能有第三方的自定义一个系统消息名出来!(当然了什么HOOK之类出来的名称这东西不能算.......)
[其他解释]
既然是内部常量,当然是外部不可访问的。
VB 设计上就是对基础的 API 进行封装,使得程序员只需要关心 Form、PictureBox、TextBox。
[其他解释]
GetWindowClass之类的函数来获取就可以了啊
[其他解释]
先坐沙发上等回复。
[其他解释]
遍历VB的控件就行了,代码如下:
Option Explicit
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Sub Form_Load()
EnumVBObjectClassName Me
End Sub
Public Sub EnumVBObjectClassName(frmTest As Form)
Dim i As Long, j As Long
Dim tApp As TLI.TLIApplication
Dim ti As Object
Dim cci As Object
Dim mi As Object
Dim strClassName As String
Dim ctl As Control
On Error Resume Next
If frmTest Is Nothing Then Exit Sub
Debug.Print "Form", GetWindowClassName(frmTest.hwnd)
Set tApp = CreateObject("TLI.TLIApplication")
Set ti = tApp.TypeLibInfoFromRegistry("{FCFB3D2E-A0FA-1068-A738-08002B3371B5}", 6, 0, 9)
For Each cci In ti.CoClasses
For i = 1 To cci.Interfaces.Count
For j = 1 To cci.Interfaces(i).Members.Count
Set mi = cci.Interfaces(i).Members(j)
If mi.Name = "hWnd" And mi.InvokeKind = INVOKE_PROPERTYGET Then
Set ctl = frmTest.Controls.Add("VB." + cci.Name, "test_" + cci.Name + "_1")
If Err.Number = 0 And ctl.hwnd <> 0 Then
Debug.Print cci.Name, GetWindowClassName(ctl.hwnd)
frmTest.Controls.Remove ctl
Else
Err.Clear
End If
Exit For
End If
Next
Next
Next
End Sub
Private Function GetWindowClassName(ByVal hwnd As Long) As String
Dim strBuffer As String
strBuffer = String(255, vbNullChar)
GetWindowClassName = VBA.Left(strBuffer, GetClassName(hwnd, strBuffer, Len(strBuffer)))
End Function