在 Visual Basic中可以通过HWnd属性获取UserForms 的句柄,HWnd属性提供对窗体的窗口句柄的访问。
在使用与窗口相关的Windows API时,需要窗口句柄。 但是和 Visual Basic不同的是VBA UserForms不具有HWnd属性
以下例程可让您获取VBA UserForms窗口句柄。
GetUserFormHandle
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function CoCreateGuid Lib "ole32.dll" (G As GUID) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" (G As GUID, _
ByVal str As String, _
ByVal cchMax As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Function GetGUID() As String
Dim G As GUID
Dim S As String
S = String(76, vbNullChar)
CoCreateGuid G
StringFromGUID2 G, S, Len(S)
S = StrConv(S, vbFromUnicode)
GetGUID = S
End Function
Private Function GetUserFormHandle(ByVal UF As Object)
Dim S As String
Dim OrigCaption As String
S = GetGUID()
OrigCaption = UF.Caption
UF.Caption = S
GetUserFormHandle = FindWindow(vbNullString, S)
UF.Caption = OrigCaption
End Function
Private Sub UserForm_Initialize()
MsgBox "UserForm window handle: 0x" + Hex(GetUserFormHandle(Me))
End Sub
在使用与窗口相关的Windows API时,需要窗口句柄。 但是和 Visual Basic不同的是VBA UserForms不具有HWnd属性
以下例程可让您获取VBA UserForms窗口句柄。
GetUserFormHandle
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function CoCreateGuid Lib "ole32.dll" (G As GUID) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" (G As GUID, _
ByVal str As String, _
ByVal cchMax As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Function GetGUID() As String
Dim G As GUID
Dim S As String
S = String(76, vbNullChar)
CoCreateGuid G
StringFromGUID2 G, S, Len(S)
S = StrConv(S, vbFromUnicode)
GetGUID = S
End Function
Private Function GetUserFormHandle(ByVal UF As Object)
Dim S As String
Dim OrigCaption As String
S = GetGUID()
OrigCaption = UF.Caption
UF.Caption = S
GetUserFormHandle = FindWindow(vbNullString, S)
UF.Caption = OrigCaption
End Function
Private Sub UserForm_Initialize()
MsgBox "UserForm window handle: 0x" + Hex(GetUserFormHandle(Me))
End Sub