Enumerating windows
-
Antithesis
- New User

- Posts: 5
- Joined: Sat Aug 04, 2007 3:39 pm
Enumerating windows
Hello. Could someone please explain to me how to use EnumWindows_() to list the currently open windows? I tried to do it the easy way with Droopy's lib, but it seems to have some sort of issue because any functions that return window names only return the first character of them.
The EnumWindows function enumerates all top-level windows on the screen by passing the handle of each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.
BOOL EnumWindows(
WNDENUMPROC lpEnumFunc, // pointer to callback function
LPARAM lParam // application-defined value
);
Parameters
lpEnumFunc
Points to an application-defined callback function. For more information, see the EnumWindowsProc callback function.
lParam
Specifies a 32-bit, application-defined value to be passed to the callback function.
The EnumWindowsProc function is an application-defined callback function that receives top-level window handles as a result of a call to the EnumWindows or EnumDesktopWindows function.
BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
);
Parameters
hwnd
Identifies a top-level window.
lParam
Specifies the application-defined value given in EnumWindows or EnumDesktopWindows.
Return Values
To continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE.
Code: Select all
; Enumerate all windows
Procedure EnumWindowsProc(hWnd, ID)
Debug hWnd
ProcedureReturn 1
EndProcedure
EnumWindows_(@EnumWindowsProc(), 0)
Code: Select all
; Get the title of a window
Procedure.s GetWindowText(hWnd)
Protected Buffer.s = Space(GetWindowTextLength_(hWnd))
GetWindowText_(hWnd, @Buffer, GetWindowTextLength_(hWnd)+SizeOf(Character))
ProcedureReturn Buffer
EndProcedureI use similar code here, but i only want the visible ones:
http://www.purebasic.fr/english/viewtopic.php?t=12314
http://www.purebasic.fr/english/viewtopic.php?t=12314
Code: Select all
Procedure UpdateAndFilterNewWindows(hWindow, Unused)
CurrentWindowTitle.s = Space(1024)
GetWindowText_(hWindow, CurrentWindowTitle, 1024)
If CurrentWindowTitle <> "" And CurrentWindowTitle <> "Program Manager"
If IsWindowVisible_(hWindow)
Debug CurrentWindowTitle
EndIf
EndIf
ProcedureReturn #True
EndProcedure
EnumWindows_(@UpdateAndFilterNewWindows(), 0)
-
Antithesis
- New User

- Posts: 5
- Joined: Sat Aug 04, 2007 3:39 pm
-
Antithesis
- New User

- Posts: 5
- Joined: Sat Aug 04, 2007 3:39 pm
-
SeregaZ
- Enthusiast

- Posts: 629
- Joined: Fri Feb 20, 2009 9:24 am
- Location: Almaty (Kazakhstan. not Borat, but Triple G)
- Contact:
Re: Enumerating windows
so any news about read and control DirectUIhwnd after 2007?
Re: Enumerating windows
I have found nice code:
Code: Select all
; PB: http://www.purebasic.fr/english/viewtopic.php?p=103024#p103024
Procedure GetDesktopWindows()
h = GetWindow_(GetDesktopWindow_(), #GW_CHILD)
Repeat
If IsWindowVisible_(h) = #True
c$ = Space(999) : GetWindowText_(h, c$, 999)
If c$ <> ""
Debug "Handle = " + Str(h) + ", Caption = " + c$
EndIf
EndIf
h = GetWindow_(h, #GW_HWNDNEXT)
Until h = 0
EndProcedure
GetDesktopWindows()
Win 11 25H2 Pro
Re: Enumerating windows
The EnumWindows function does not enumerate child windows.
This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.
I may look like a mule, but I'm not a complete ass.
-
SeregaZ
- Enthusiast

- Posts: 629
- Joined: Fri Feb 20, 2009 9:24 am
- Location: Almaty (Kazakhstan. not Borat, but Triple G)
- Contact:
Re: Enumerating windows
how it is help to read items and choice one of them in this DirectUIhwnd window?eJan wrote:I have found nice code:Code: Select all
; PB: http://www.purebasic.fr/english/viewtopic.php?p=103024#p103024 Procedure GetDesktopWindows() h = GetWindow_(GetDesktopWindow_(), #GW_CHILD) Repeat If IsWindowVisible_(h) = #True c$ = Space(999) : GetWindowText_(h, c$, 999) If c$ <> "" Debug "Handle = " + Str(h) + ", Caption = " + c$ EndIf EndIf h = GetWindow_(h, #GW_HWNDNEXT) Until h = 0 EndProcedure GetDesktopWindows()



