Get handle of child control of external window

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Get handle of child control of external window

Post by firace »

This window spy works well (move your mouse over an external window to get its handle and class name) except for some child controls; for instance checkboxes that are inside of a container or GroupBox control are not detected. Any ideas? (An easy way to test this code is to open the Purebasic IDE preferences window, then move your mouse over some of the controls)

Code: Select all

OpenWindow(0,30,30,440,330,"Underpointer Inspector", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget) : StickyWindow(0, 1)

FrameGadget(20, 10, 50, 400, 110, "Under pointer")
TextGadget(2,30,70 ,300,24,"")
TextGadget(9,30,100,300,24,"")
TextGadget(4,30,130,300,24,"")

TextGadget(5,30,190,300,24,"")
TextGadget(6,30,220,300,24,"")
TextGadget(7,30,250,300,24,"")

AddWindowTimer(0, 1, 70)


Repeat
  
  Event = WaitWindowEvent()
  
  If event = #PB_Event_Timer
    Classname$ = Space(#MAX_PATH)
    parentHandleClassname$ = Space(#MAX_PATH)
    GparentHandleClassname$ = Space(#MAX_PATH)
    Textname$ = Space(#MAX_PATH)
    
    desktop_x = DesktopMouseX()
    desktop_y = DesktopMouseY()
    GetCursorPos_ (@p.POINT) 
    handle = WindowFromPoint_( p\y<< 32 + p\x )
    ScreenToClient_ (handle,  p\y<< 32 + p\x )
    childhandle = ChildWindowFromPoint_(handle, p\y<< 32 + p\x)
    Debug "H/C " + handle + " - " + childhandle
    parentHandle = GetParent_(handle)
    SendMessage_(handle,#WM_GETTEXT,128,@Textname$) 
    
    GetClassName_(handle, @Classname$, #MAX_PATH-1)
    GetClassName_( parentHandle, @parentHandleClassname$,  #MAX_PATH-1)
    
    
    SetGadgetText(2,"Handle & Class = " + handle + " --- " + Classname$)
    SetGadgetText(4,"Caption = " + Textname$)   
    SetGadgetText(5,"Parent = " + parentHandle + " --- " +  parentHandleClassname$)
     
  EndIf
  
Until Event = #PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Get handle of child control of external window

Post by RASHAD »

Hi
Checked with a CheckBox inside a container and the container inside another container

Code: Select all

  If event = #PB_Event_Timer
    Classname$ = Space(#MAX_PATH)
    parentHandleClassname$ = Space(#MAX_PATH)
    GparentHandleClassname$ = Space(#MAX_PATH)
    Textname$ = Space(#MAX_PATH)
    
    desktop_x = DesktopMouseX()
    desktop_y = DesktopMouseY()
    GetCursorPos_ (p.POINT) 
    handle = WindowFromPoint_(p\y << 32 + p\x)
    ScreenToClient_ (handle,  p)
    childhandle = ChildWindowFromPoint_(handle, p\y<< 32 + p\x)
    Debug "H/C " + handle + " - " + childhandle
    parentHandle = GetParent_(handle)
    GetWindowText_(handle,@Textname$,#MAX_PATH)
    
    GetClassName_(handle, @Classname$, #MAX_PATH-1)
    GetClassName_( parentHandle, @parentHandleClassname$,  #MAX_PATH-1)
    
    
    SetGadgetText(2,"Handle & Class = " + handle + " --- " + Classname$)
    SetGadgetText(4,"Caption = " + Textname$)   
    SetGadgetText(5,"Parent = " + parentHandle + " --- " +  parentHandleClassname$)
     
  EndIf
Egypt my love
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Get handle of child control of external window

Post by firace »

Hi Rashad,

Unfortunately this does not seem to work for some controls in the PureBasic preferences window for example - only the parent handle is detected:

Image
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Get handle of child control of external window

Post by JHPJHP »

Hi firace,

You may need to rethink your approach; combining the function WindowFromPoint with the following code should give you the desired results.

Execute the following code; scroll until you find "Preferences" WindowClass_4 (expand)
- make sure the Preferences window is open before executing the code

Windows Services & Other Stuff\Other_Stuff\OtherStuff\EnumerateWindows.pb
Last edited by JHPJHP on Sun Jul 25, 2021 4:03 pm, edited 1 time in total.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Get handle of child control of external window

Post by firace »

Many thanks JHPJHP, I was going a different way indeed, but I will give this a try (when time permits, as I am on the go now)
Post Reply