Results 1 to 3 of 3

Thread: How do i get hwnd in VBA?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    Anyone know how i can get the window handler of a userform in VBA? In VB5/6 the userform has the property hwnd.

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    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
    Chemically Formulated As:
    Dr. Nitro

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Thanks. Worked. Had to fix to code(or else i get a number):

    Code:
    Dim strData As String
    strData = Space(128)
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width