Anyone know how i can get the window handler of a userform in VBA? In VB5/6 the userform has the property hwnd.
Printable View
Anyone know how i can get the window handler of a userform in VBA? In VB5/6 the userform has the property hwnd.
Hello!
Drop this into the code section of your userform.
Code:Option Explicit
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub UserForm_Click()
Dim P As POINTAPI
Dim l As Long
l = GetCursorPos(P)
'PURPOSE: Grab the handle where ever the Pointer is residing
Dim lng_Handle As Long
lng_Handle = WindowFromPoint(P.x, P.y)
MsgBox "The handle for this object is: " & lng_Handle
'PURPOSE: Get the Class name
Dim strData As String * 128
l = GetClassName(lng_Handle, strData, Len(strData))
MsgBox "The classname for this object is: " & Left$(strData, l)
End Sub
Thanks. Worked. Had to fix to code(or else i get a number):
Is there a way to get hwnd to the active window? The method you showed works fine, but if the mouse isnt over the program I won't get the hwnd to that window. I am using hWnd in Userform_Initialize.Code:Dim strData As String
strData = Space(128)