AutoHotkey Community

It is currently May 27th, 2012, 12:07 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: ClassNN for VBA
PostPosted: February 23rd, 2011, 9:37 am 
Offline

Joined: February 23rd, 2011, 9:32 am
Posts: 1
I wrote a module for VBA with functions to retrieve ClassNN in the same way as AutoHotkey do. I expect it helps you.

Code:
Option Explicit

' mClassNN
' Date: 02/21/2011
' Author: Efrén López Fernández

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Function IsWindow Lib "user32.dll" (ByVal hwnd As Long) As Boolean
Private Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Private Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long
Private Const GA_PARENT As Long = 1
Private Const GA_ROOT As Long = 2
Private Const GA_ROOTOWNER As Long = 3


Public Declare Function GetWindowInfo Lib "user32" (ByVal hwnd As Long, ByRef pwi As WINDOWINFO) As Boolean
Private Const WS_CHILD As Long = &H40000000

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type WINDOWINFO
    cbSize As Long
    rcWindow As RECT
    rcClient As RECT
    dwStyle As Long
    dwExStyle As Long
    cxWindowBorders As Long
    cyWindowBorders As Long
    atomWindowtype As Long
    wCreatorVersion As Long
End Type


Private classNameEnumChildN As String
Private countEnumChildN As Integer
Private foundEnumChildN As Boolean

Private foundEnumChildClassNN As Boolean
Private classNNChildClassNN As String
Private hwndChildClassNN As Long


Public Function GetN(hwnd As Long) As Integer
    Dim hwndAncestorRoot As Long
    hwndAncestorRoot = GetAncestor(hwnd, GA_ROOT)
   
    If Not IsWsChild(hwnd) Then
        GetN = -1
    Else
        Dim retVal As Long
        countEnumChildN = 0
        classNameEnumChildN = GetClassNameS(hwnd)
        foundEnumChildN = False
        retVal = EnumChildWindows(hwndAncestorRoot, AddressOf EnumChildNProc, hwnd)
        GetN = countEnumChildN
    End If

End Function

Private Function EnumChildNProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    If GetClassNameS(lhWnd) <> classNameEnumChildN Then
        EnumChildNProc = True
    Else
        countEnumChildN = countEnumChildN + 1
       
        If lhWnd = lParam Then
            EnumChildNProc = False
            foundEnumChildN = True
        Else
            EnumChildNProc = True
        End If
    End If
End Function

Public Function FindChildWindowClassNN(hparent As Long, classnn As String) As Long
    Dim retVal As Long
   
    'Iniciamos las variables
    foundEnumChildClassNN = False
    classNNChildClassNN = classnn
    hwndChildClassNN = 0
    retVal = EnumChildWindows(hparent, AddressOf EnumChildClassNNProc, hparent)
    FindChildWindowClassNN = hwndChildClassNN
End Function

Private Function EnumChildClassNNProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    If GetClassNN(lhWnd) = classNNChildClassNN Then
        foundEnumChildClassNN = True
        hwndChildClassNN = lhWnd
        'Para que pare
        EnumChildClassNNProc = False
    Else
        'Para que siga
        EnumChildClassNNProc = True
    End If
End Function

Public Function GetClassNN(hwnd As Long) As String
    Dim className As String
    Dim N As Integer
    className = GetClassNameS(hwnd)
    If className = vbNullString Then
        GetClassNN = vbNullString
    Else
        N = GetN(hwnd)
        If N > -1 Then
            GetClassNN = className & N
        Else
            GetClassNN = className
        End If
    End If
End Function

Public Function GetClassNameS(hwnd As Long) As String
    Dim retVal As Long
    Dim Buf As String * 255
    retVal = GetClassName(hwnd, Buf, 255)
    GetClassNameS = TrimNull(Buf)
End Function

Public Function GetWindowTextS(hwnd As Long) As String
    Dim retVal As Long
    Dim Buf As String * 255
    retVal = GetWindowText(hwnd, Buf, 255)
    GetWindowTextS = TrimNull(Buf)
End Function

Public Function TrimNull(startstr As String) As String
   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function

Public Function IsWsChild(hwnd As Long) As Boolean
    Dim wi As WINDOWINFO
    Dim retVal As Boolean
    retVal = GetWindowInfo(hwnd, wi)
    If retVal Then
        IsWsChild = ((wi.dwStyle And WS_CHILD) > 0)
    Else
        IsWsChild = False
    End If
End Function


Greetings from Spain.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group