Enumerating windows

Just starting out? Need help? Post your questions and find answers here.
Antithesis
New User
New User
Posts: 5
Joined: Sat Aug 04, 2007 3:39 pm

Enumerating windows

Post by Antithesis »

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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

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
EndProcedure
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I use similar code here, but i only want the visible ones:
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)
--Kale

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

Post by Antithesis »

Thank you very much!

Also could you tell me how to get the Text value of a control if it has one? What I want is write an windows live messenger conversation logger so I want to find the control that contains the conversation and get its text value and check if it changes...
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

You can use the same code to get get the text from a control.
Antithesis
New User
New User
Posts: 5
Joined: Sat Aug 04, 2007 3:39 pm

Post by Antithesis »

Hmm I tried that but with no luck. I was hoping there's some other way. I think the problem is that the control I'm dealing with is some weird thing called DirectUIHWND which seems to contain the entire WLM chat window within itself.

Any suggestions?
SeregaZ
Enthusiast
Enthusiast
Posts: 629
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Enumerating windows

Post by SeregaZ »

so any news about read and control DirectUIhwnd after 2007?
eJan
Enthusiast
Enthusiast
Posts: 368
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: Enumerating windows

Post by eJan »

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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Enumerating windows

Post by srod »

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.
:wink:
I may look like a mule, but I'm not a complete ass.
SeregaZ
Enthusiast
Enthusiast
Posts: 629
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Enumerating windows

Post by SeregaZ »

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()
how it is help to read items and choice one of them in this DirectUIhwnd window?
Image
Post Reply