en el form
Código:
Option Explicit
Private Sub Command1_Click()
Erase Valores
nProcesos = 0
'Llama a Enumwindows para listar las ventanas y obtener la información
EnumWindows AddressOf Listar_Ventanas, 0
cargar
End Sub
Sub cargar()
Dim i As Integer
Dim LItem As ListItem 'variable para los subitems del listview
ListView1.ListItems.Clear
For i = 1 To nProcesos
With Valores(i)
If .captionWin <> "Program Manager" And .captionWin <> vbNullString _
And .captionWin <> "Project1" Then
' Si la ventana es visible
If IsWindowVisible(.HwndWin) Then
'Obtiene el ícono de la ventana
Call Obtener_Icono(.HwndWin)
'Agrega el Item y subitems al ListView
Set LItem = ListView1.ListItems.Add(, , .captionWin, , _
ImageList1.ListImages.Count)
LItem.SubItems(1) = .HwndWin
LItem.SubItems(2) = .Estado
LItem.SubItems(3) = .Left
LItem.SubItems(4) = .Right
LItem.SubItems(5) = .Top
LItem.SubItems(6) = .Bottom
End If
End If
End With
Next
End Sub
'Sub que obtiene el ícono de la ventana y luego lo coloca en el Listview
Private Sub Obtener_Icono(h As Long)
'Variable Rect para usar con el API setRect y Drawcaption
Dim rec As RECT
With Picture1
'Limpia el Picturebox
.Cls
.Picture = Nothing
Call SetRect(rec, 5, 7, 10, 10)
'Dibuja el icono en el picture pasandole al api el _
hwnd de la ventana
Call DrawCaption(h, .hDC, rec, 4)
.Refresh
Set .Picture = Picture1.Image
End With
' Almacena la imagen en el ImageList
ImageList1.ListImages.Add , , Picture:=Picture1
' Si el ImageList no está inicializado se enlaza al ListView
If ListView1.SmallIcons Is Nothing Then
Set ListView1.SmallIcons = ImageList1
End If
End Sub
Private Sub Form_Load()
With ListView1
'vista de reporte
.View = lvwReport
'Columnas
.ColumnHeaders.Add , , " Título de la ventana "
.ColumnHeaders.Add , , " HWND "
.ColumnHeaders.Add , , " Estado "
.ColumnHeaders.Add , , " Left "
.ColumnHeaders.Add , , " Right "
.ColumnHeaders.Add , , " Top "
.ColumnHeaders.Add , , " Bottom "
.ColumnHeaders(1).Width = 4000
.ColumnHeaders(2).Width = 1000
.ColumnHeaders(3).Width = 1200
.ColumnHeaders(4).Width = 700
.ColumnHeaders(5).Width = 800
.ColumnHeaders(6).Width = 800
.ColumnHeaders(7).Width = 800
End With
'Propiedades para el Picture invisible en el cual se dibuja _
el icono de la ventana mediante el Api DrawCaption
With Picture1
.ScaleMode = vbPixels
.Width = 260
.Height = 260
.AutoRedraw = True
.BorderStyle = 0
.BackColor = ListView1.BackColor
.Visible = False
End With
Me.Caption = " Información de ventanas de windows "
Command1.Caption = " Listar ventanas "
End Sub
en un modulo
Código:
Option Explicit
'Udt
'##############
Type ProcData
HwndWin As Long
captionWin As String
Estado As String
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type POINTAPI
x As Long
y As Long
End Type
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type WINDOWPLACEMENT
length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type
'Declaraciones Api
'#################
Declare Function DrawCaption Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long, _
pcRect As RECT, ByVal un As Long) As Long
Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 _
As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal _
lParam As Long) As Long
Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As _
WINDOWPLACEMENT) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, _
ByVal cch As Long) As Long
'Variables
'##########
Public Valores() As ProcData
Public nProcesos As Integer
'Función que enumera las ventanas de windows
Public Function Listar_Ventanas(ByVal Handle As Long, _
ByVal lParam As Long) As Boolean
Dim infoWin As WINDOWPLACEMENT
Dim buffer As String * 256
Dim l As Long
nProcesos = nProcesos + 1
'redimensiona el vector
ReDim Preserve Valores(1 To nProcesos)
With Valores(nProcesos)
.HwndWin = Handle
' Recupera el título de la ventana
l = GetWindowText(Handle, buffer, Len(buffer))
'Remplaza los Nulos
.captionWin = Replace(buffer, Chr(0), vbNullString)
infoWin.length = Len(infoWin)
Call GetWindowPlacement(Handle, infoWin)
'Guarda el estado de la ventana
Select Case infoWin.showCmd
Case 1
.Estado = "Normal"
Case 2
.Estado = "Minimizado"
Case 3
.Estado = "Maximizado"
End Select
'Guarda la posición de la ventana
.Left = infoWin.rcNormalPosition.Left
.Top = infoWin.rcNormalPosition.Top
.Right = infoWin.rcNormalPosition.Right
.Bottom = infoWin.rcNormalPosition.Bottom
End With
' Continúa buscando las demás ventanas
Listar_Ventanas = 1
End Function