Buenas tengo un problema con una función (Realmente llevo mucho tiempo sin programar y ando haciendo pequeñas "funciones" para generalizarme de nuevo con VB6) lo que mi funcion hace es listar las ventanas, ver si estan visible si es correcto obtiene el caption y luego la almacena en un array.
El problema es que la función no me sirve, no puedo obtener los valores del array para listarlo, no se si es que no manejo bien el array... Busque ayuda (bastante) en Internet y la mayoría de ejemplos lista todo los caption de una vez en algún control pero no consigo un ejemplo en Array.
Aqui anexo el codigo esto va en un Modulo .Bas:
' Lista las ventanas
Public Declare Function EnumWindows Lib "user32" ( _
ByVal wndenmprc As Long, _
ByVal lParam As Long) As Long
' Obtener el título de la ventana
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
'LISTA VENTANAS
Public ListEnumWindow() As String
Public Proc As Integer
Public Function mEnumWindows(ByVal wHwnd As Long, ByVal lParam As Long) As Long
Dim wLen As Long
Dim GetText As Long
Dim wText As String
If IsWindowVisible(wHwnd) <> 0 Then
wLen = GetWindowTextLength(wHwnd)
If wLen > 0 Then
wText = Space$(wLen + 1)
GetWindowText wHwnd, wText, Len(wText)
Proc = Proc + 1
ReDim Preserve ListEnumWindow(1 To Proc)
ListEnumWindow(Proc) = wText
Else
'COMANDO ELSE
End If
Else
'COMANDO ELSE
End If
End Function
Y para ejecutarlo lo hago de la siguiente manera:
EnumWindows AddressOf mEnumWindows, 0&
Sera que me pueden brinda una mano en que hago mal?
Gracias y saludos.
IsWindowVisible? :-(
mira este ejemplo
Option Explicit
'Esta función Api devuelve un valor Boolean indicando si la ventana es una ventana visible
Private Declare Function IsWindowVisible _
Lib "user32" ( _
ByVal hwnd As Long) As Long
'Esta función retorna el número de caracteres del caption de la ventana
Private Declare Function GetWindowTextLength _
Lib "user32" _
Alias "GetWindowTextLengthA" ( _
ByVal hwnd As Long) As Long
'Esta devuelve el texto. Se le pasa el hwnd de la ventana, un buffer donde se
'almacenará el texto devuelto, y el Lenght de la cadena en el último parámetro
'que obtuvimos con el Api GetWindowTextLength
Private Declare Function GetWindowText _
Lib "user32" _
Alias "GetWindowTextA" ( _
ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
'Esta es la función Api que busca las ventanas y retorna su handle o Hwnd
Private Declare Function GetWindow _
Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wFlag As Long) As Long
Private Declare Function SetWindowText Lib "user32.dll" Alias _
"SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
'Constantes para buscar las ventanas mediante el Api GetWindow
Private Const GW_HWNDFIRST = 0&
Private Const GW_HWNDNEXT = 2&
Private Const GW_CHILD = 5&
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Procedimiento que lista las ventanas visibles de Windows
Private Sub Listar()
Dim buf As Long, handle As Long, titulo As String, lenT As Long, ret As Long
Dim retval As Long
List1.Clear
'Obtenemos el Hwnd de la primera ventana, usando la constante GW_HWNDFIRST
handle = GetWindow(hwnd, GW_HWNDFIRST)
'Este bucle va a recorrer todas las ventanas.
'cuando GetWindow devielva un 0, es por que no hay mas
Do While handle <> 0
'Tenemos que comprobar que la ventana es una de tipo visible
If IsWindowVisible(handle) Then
'Obtenemos el número de caracteres de la ventana
lenT = GetWindowTextLength(handle)
'si es el número anterior es mayor a 0
If lenT > 0 Then
'Creamos un buffer. Este buffer tendrá el tamaño con la variable LenT
titulo = String$(lenT, 0)
'Ahora recuperamos el texto de la ventana en el buffer que le enviamos
'y también debemos pasarle el Hwnd de dicha ventana
ret = GetWindowText(handle, titulo, lenT + 1)
titulo$ = Left$(titulo, ret)
'si el titulo de la ventana es igual a x titulo cambias y pones lo que sea.
If titulo = "titulo de ares" Then
' return value
retval = SetWindowText(handle, "Hola Mundo")
End If
'La agregamos al ListBox
List1.AddItem titulo$
End If
End If
'Buscamos con GetWindow la próxima ventana usando la constante GW_HWNDNEXT
handle = GetWindow(handle, GW_HWNDNEXT)
Loop
End Sub
Private Sub Command1_Click()
'Llamamos a la función Listar
Call Listar
End Sub
saludos
Hola DanyFirex, muchas gracias por tu respuesta ya logre solucionar parte del problema que tenia (Volvi a hacer el codigo desde 0) lo unico que no e logrado y no entiendo (o recuerdo) porque.. Es que quiero que todos los nombres se me almacenen en un Array en un modulo y que desde el Form pueda acceder al array, pero no puedo hacer eso...
Por casualidad sabes debido a que no puedo obtener los datos desde el array o sera que los agrego mal? Uso el Redim Preserver
Cita de: BRASEED en 11 Agosto 2013, 09:08 AM
debido a que no puedo obtener los datos desde el array o sera que los agrego mal? Uso el Redim Preserver
Private Function mEnumWindows(ByVal wHwnd As Long, ByVal lParam As Long) As Boolean
ReDim Preserve ListEnumWindow(Proc)
mEnumWindows = True '(antes de End Function)
FORM
Option Explicit
Private Sub Form_Load()
AutoRedraw = True
End Sub
Private Sub Command1_Click()
Cls
Call MakeArray
Dim x As Long
For x = 1 To UBound(ListEnumWindow)
Print ListEnumWindow(x)
Next
End Sub
MODULO
Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal wndenmprc 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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public ListEnumWindow() As String
Private Proc As Integer
Public Sub MakeArray()
Proc = 0
Erase ListEnumWindow
Call EnumWindows(AddressOf mEnumWindows, 0&)
End Sub
Private Function mEnumWindows(ByVal wHwnd As Long, ByVal lParam As Long) As Boolean
Dim wLen As Long
Dim wText As String
If IsWindowVisible(wHwnd) Then
wLen = GetWindowTextLength(wHwnd)
If wLen > 0 Then
wText = Space$(wLen + 1)
GetWindowText wHwnd, wText, Len(wText)
Proc = Proc + 1
ReDim Preserve ListEnumWindow(Proc)
ListEnumWindow(Proc) = Replace(wText, Chr(0), "")
End If
End If
mEnumWindows = True
End Function