Cómo saber el número de clase de una ventana


Es útil para activar una aplicación con ShowWindow.

Declaramos la funciones :

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Introducimos el siguiente código :

Public Sub GetClassNameFromTitle()

Dim sInput As String
Dim hWnd As Long
Dim lpClassName As String
Dim nMaxCount As Long
Dim lresult As Long

' pad the return buffer for GetClassName
nMaxCount = 256
lpClassName = Space(nMaxCount

' Note: must be an exact match
sInput = InputBox("Enter the exact window title:"

' No validation is done as this is a debug window utility
hWnd = FindWindow(vbNullString, sInput)

' Get the class name of the window, again, no validation
lresult = GetClassName(hWnd, lpClassName, nMaxCount)
Debug.Print "Window: " & sInput
Debug.Print "Class name: " & Left$(lpClassName, lresult)

End Sub

Esto puede ser útil para poder activar una aplicación cuyo título cambia por lo que no podemos emplear la instrucción AppActivate de VB.

Para ello declararemos :

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOW = 5
Const SW_SHOWDEFAULT = 10
Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWNA = 8
Const SW_SHOWNOACTIVATE = 4
Const SW_SHOWNORMAL = 1

Y para activar la aplicación :

Dim hwnd as long, res as long

hwnd = FindWindow("NombreDeClase", vbNullString)
if hwnd > 0 then res = ShowWindow(hwnd, SW_SHOW)

Con los otros mensajes SW_ podemos indicar si la aplicación aparece maximizada, minimizada, con el foco, sin el foco, etc.