Código: Seleccionar todo
Option Explicit
'---------------------------------------------------------------------------------------
' Función : mEnumWindow
' Fecha : 09/10/2010 16:504
' Auor : Skyweb07
' Email : [email protected]
' WebPage : http://www.skyweb-productions.org
' Própsito : Obtiene información sobre las ventanas que estan abiertas y
' las añade a la listview.
' Referencias : http://msdn.microsoft.com/en-us/library/ms632611%28VS.85%29.aspx
' http://msdn.microsoft.com/en-us/library/ms633497%28VS.85%29.aspx
' http://www.nirsoft.net/utils/winlister.html
'
' Otro ejemplo
' parecido : http://www.recursosvisualbasic.com.ar/htm/listado-api/111-listar-ventana-de-window.htm
'---------------------------------------------------------------------------------------
Private Type POINTAPI
x As Long
y As Long
End Type
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type
Private Const SW_MAXIMIZE = 3
Private Const SW_MINIMIZE = 2
Private Const SW_NORMAL = 1
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) 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 Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Function mEnumWindows() As String
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Function
Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim hBuffer As String * 255 ' // Buffer.
Dim hPlace As WINDOWPLACEMENT ' // Variable que contendra la posición de la ventana.
Dim hPID As Long ' // Variable que contendra la ID del proceso de el Hilo de la ventana.
If IsWindowVisible(hwnd) Then ' // Si la ventana esta visible | NOTA : Si quieren que se listen todas las vetanas que estan ocultas solo quiten estas lineas.
GetWindowText hwnd, hBuffer, Len(hBuffer) ' // Obtenemos el Título de la ventana.
hBuffer = Left$(hBuffer, Len(hBuffer)) ' // Quitamos los carácteres Nulos del Título de la ventana.
If Trim$(Replace$(hBuffer, Chr$(0), vbNullString)) <> vbNullString Then ' // Si la ventana tiene algun carácter.
With frmMain.LV.ListItems.Add(, , hBuffer) ' // Añadimos a la Listview el Título de la ventana.
.SubItems(1) = CBool(IsWindowVisible(hwnd)) ' // Ponemos si la ventana esta visible o no. | NOTA : Cbool() devuelve un valor True o False.
hPlace.Length = Len(hPlace) ' // Establecemos el valor del Buffer.
GetWindowPlacement hwnd, hPlace ' // Obtenemos la posición de la ventana pasandole los parámetros de el Handle de la ventana y la variable que contiene la posición de la ventana.
Select Case hPlace.showCmd ' // Hacemos un Select Case solo para determinar el estado y la posición de la ventana.
Case SW_MAXIMIZE: .SubItems(2) = "Maximizado."
Case SW_MINIMIZE: .SubItems(2) = "Minimizado."
Case SW_NORMAL: .SubItems(2) = "(" & hPlace.rcNormalPosition.Left & "," & hPlace.rcNormalPosition.Top & ")" ' // Posición de la ventana (Izquierda,Arriba)
Case Else: .SubItems(2) = "(" & hPlace.rcNormalPosition.Left & "," & hPlace.rcNormalPosition.Top & ")" ' // Posición de la ventana (Izquierda,Arriba)
End Select
.SubItems(3) = "(" & hPlace.rcNormalPosition.Right - hPlace.rcNormalPosition.Left & "," & hPlace.rcNormalPosition.Bottom - hPlace.rcNormalPosition.Top & ")" ' // Posición de la ventana (Ancho,Alto)
.SubItems(4) = Hex$(hwnd) ' // Ponemos el Handle de la ventana.
hBuffer = Space$(255) ' // Reasignamos valores nulos al Buffer.
GetClassName hwnd, hBuffer, Len(hBuffer) ' // Obtenemos el nombre de la clase de la ventana.
hBuffer = Left$(hBuffer, InStr(1, hBuffer, Chr$(0))) ' // Quitamos los carácteres Nulos del nombre de la clase de la ventana.
.SubItems(5) = hBuffer ' // Lo pnemos en la Listview
GetWindowThreadProcessId hwnd, hPID ' // Obtenemos el Hilo de la ventana.
.SubItems(6) = String$(4, "0") & Hex$(hPID) ' // Lo ponemos en la Listview.
End With
End If
End If
EnumWindowsProc = 1 ' // Como es una función CallBack le asignamos 1 porque va a seguir enumerando ventanas hasta que la función devuelva 0.
End Function