Page 1 of 1

Getting window handle of unkown caption

Posted: Wed Sep 28, 2005 11:04 pm
by roachofdeath
Hey,

In some programs, you can click a button on the prorgam window, and drag it onto another window, and it will return the handle of the window the drag was released upon. Any ideas of how to accomplish this in PB?

Posted: Wed Sep 28, 2005 11:34 pm
by Kale
Run this code then mouse over a window, click to exit:

Code: Select all

Repeat
    MouseClick = GetAsyncKeyState_(#VK_LBUTTON)
    If MouseClick = 32768
    	End
    EndIf
        GetCursorPos_(@MouseCoords.POINT)
        hWindow = WindowFromPoint_(MouseCoords\x, MouseCoords\y)
        Debug hWindow
    Delay(1)
ForEver
:wink:

Posted: Thu Sep 29, 2005 8:57 pm
by roachofdeath
thank you, your code works great, but I'm having more troubles with getting it to the program because it's losing focus.

Code: Select all

Repeat

If looking.l=1
    If countdown<1
      handle.l = hWindow
      MessageRequester("",Str(handle.l))
      looking.l=0
    EndIf
  GetCursorPos_(@MouseCoords.POINT)
  hWindow = WindowFromPoint_(MouseCoords\x, MouseCoords\y)
  countdown=countdown-1
EndIf

  Select WaitWindowEvent()
  Case #PB_EventCloseWindow: End
  Case #PB_EventGadget
    Select EventGadgetID()
      Case #Button_23
        countdown=100
        looking.l=1
      EndSelect
  EndSelect
ForEver
As seen here, it's supposed to wait 100 after the button is clicked, then display the window handle, but it wont do it outside the program. I'd prefernot to do it this way, however. I would prefer to just have the user click and drag a button, and where the mouse button was released, it would detect the window handle of the window the mouse button was released on.

Posted: Thu Sep 29, 2005 9:05 pm
by Kale
Just start a loop when the mouse button was held down then when the mouse button is released use the 'WindowFromPoint_(MouseCoords\x, MouseCoords\y)' function to find out what window the cursor is over. :)

Posted: Thu Dec 20, 2007 11:53 am
by rjara
Just start a loop when the mouse button was held down then when the mouse button is released use the 'WindowFromPoint_(MouseCoords\x, MouseCoords\y)' function to find out what window the cursor is over. Smile
Kale, could you post an example of what you mean?

Posted: Thu Dec 20, 2007 5:19 pm
by netmaestro

Code: Select all

;==============================================================
; Program:         Digger
; Author:          Lloyd Gallant (netmaestro)
; Date:            December 20, 2007
; Updated:         December 27, 2016
; Target OS:       Microsoft Windows All
; Target Compiler: PureBasic 4.0 and later
; License:         Free, unrestricted, use at your own risk
;==============================================================

OpenWindow(0,0,0,440,64,"",#PB_Window_BorderLess|#WS_BORDER|#PB_Window_Invisible)
StickyWindow(0,1)
wclight =  RGB(255,255,223)
wcdark  =  RGB(235,235,203)
SetWindowColor(0,wclight)
ExamineDesktops()
screenw = DesktopWidth(0)
screenh = DesktopHeight(0)
TextGadget(0,0,0,100,16,"")
TextGadget(1,60,0,80,16,"Hwnd")
TextGadget(2,140,0,100,16,"Caption")
TextGadget(3,240,0,60,16,"Ctrl ID")
TextGadget(4,300,0,140,16,"Classname")
TextGadget(5,0,16,60,10,"")
TextGadget(6,0,26,60,16,"Window:")
TextGadget(7,0,42,60,22,"   Parent:")
TextGadget(8,60,26,80,16,"")
TextGadget(9,140,26,100,16,"")
TextGadget(10,240,26,60,16,"")
TextGadget(11,300,26,140,16,"")
TextGadget(12,60,42,80,16,"")
TextGadget(13,140,42,100,16,"")
TextGadget(14,240,42,60,16,"")
TextGadget(15,300,42,140,16,"")
For i=0 To 7
  SetGadgetColor(i,#PB_Gadget_BackColor,wcdark)
Next
For i=8 To 15
  SetGadgetColor(i,#PB_Gadget_BackColor,wclight)
Next
ShowWindow_(WindowID(0), #SW_SHOWNA)

Repeat
  Delay(1)
  ev=WindowEvent()
  x=DesktopMouseX():y=DesktopMouseY()
  If x > screenw-WindowWidth(0)
    wx = x-WindowWidth(0)
  Else
    wx = x+10
  EndIf
  If y >= screenh-WindowHeight(0)+20
    wy = y-WindowHeight(0)-10
  Else
    wy = y+20
  EndIf
  ResizeWindow(0, wx, wy, #PB_Ignore, #PB_Ignore)
  hwnd = WindowFromPoint_(x|(y<<32))
  If hwnd<>oldhwnd Or ev=#PB_Event_Repaint
    hwnd$ = Hex(hwnd)
    caption$ = Space(100)
    GetWindowText_(hwnd, @caption$, 99)
    ctrlid$ = Str(GetWindowLong_(hwnd, #GWL_ID))
    class$ = Space(140)
    GetClassName_(hwnd, @class$, 139)
    SetGadgetText(8, hwnd$)
    SetGadgetText(9, caption$)
    SetGadgetText(10, ctrlid$)
    SetGadgetText(11, class$)
    parent = GetParent_(hwnd)
    parent$ = Hex(parent)
    caption$ = Space(100)
    GetWindowText_(parent, @caption$, 99)
    ctrlid$ = Str(GetWindowLong_(parent, #GWL_ID))
    class$ = Space(140)
    GetClassName_(parent, @class$, 139)
    SetGadgetText(12, parent$)
    SetGadgetText(13, caption$)
    SetGadgetText(14, ctrlid$)
    SetGadgetText(15, class$)
    oldhwnd = hwnd    
    RedrawWindow_(WindowID(0),0,0,#RDW_UPDATENOW)
  EndIf

Until GetAsyncKeyState_(#VK_RBUTTON) & 32768 

Posted: Fri Dec 21, 2007 2:25 am
by PB
@netmaestro: Your Digger app is great, but two things I noticed:

(1) Even though StickyWindow(0,1) is used, it's still not topmost if I hover the
mouse over another topmost window. Not sure why.

(2) When I hover it over Calc.exe in Win XP, it doesn't show the calculation in
in the window (eg. the "0." bit when Calc is first launched). Any ideas? Also, if
you hover the mouse over Calc's "sqrt" button, Digger shows it as "sqt" for
some reason, without the "r".

Besides those, it's a handy little app. :)

Re: Getting window handle of unkown caption

Posted: Fri Sep 09, 2011 3:09 pm
by IdeasVacuum
Just tried netmaestro's digger in PBv4.51. Couple of things PB does not like, in particular WindowFromPoint_ wants a Windows Point structure.

So, I added a Point and the app runs very well without complaint, but it does not return any handles......... :shock: I assume this must be because I have not implemented the Point correctly - can anybody fix it?

Code: Select all

;==============================================================
; Program:         Digger
; Author:          Lloyd Gallant (netmaestro)
; Date:            December 20, 2007
; Target OS:       Microsoft Windows All
; Target Compiler: PureBasic 4.0 and later
; License:         Free, unrestricted, use at your own risk
;==============================================================
Global gPt.POINT

OpenWindow(0,0,0,440,64,"",#PB_Window_BorderLess|#WS_BORDER|#PB_Window_Invisible)
StickyWindow(0,1)
wclight =  RGB(255,255,223)
wcdark  =  RGB(235,235,203)
SetWindowColor(0,wclight)
ExamineDesktops()
screenw = DesktopWidth(0)
screenh = DesktopHeight(0)

TextGadget(0,0,0,100,16,"")
TextGadget(1,60,0,80,16,"Hwnd")
TextGadget(2,140,0,100,16,"Caption")
TextGadget(3,240,0,60,16,"Ctrl ID")
TextGadget(4,300,0,140,16,"Classname")
TextGadget(5,0,16,60,10,"")
TextGadget(6,0,26,60,16,"Window:")
TextGadget(7,0,42,60,22,"   Parent:")
TextGadget(8,60,26,80,16,"")
TextGadget(9,140,26,100,16,"")
TextGadget(10,240,26,60,16,"")
TextGadget(11,300,26,140,16,"")
TextGadget(12,60,42,80,16,"")
TextGadget(13,140,42,100,16,"")
TextGadget(14,240,42,60,16,"")
TextGadget(15,300,42,140,16,"")
For i=0 To 7
  SetGadgetColor(i,#PB_Gadget_BackColor,wcdark)
Next
For i=8 To 15
  SetGadgetColor(i,#PB_Gadget_BackColor,wclight)
Next
HideWindow(0,0)

Repeat
  Delay(1)
  ev=WindowEvent()
  gPt\x = DesktopMouseX()
  gPt\y = DesktopMouseY()
  If gPt\x > screenw-WindowWidth(0)
    wx = gPt\x - WindowWidth(0)
  Else
    wx = gPt\x + 10
  EndIf
  If gPt\y >= screenh-WindowHeight(0)+20
    wy = gPt\y - WindowHeight(0)-10
  Else
    wy = gPt\y + 20
  EndIf
  ResizeWindow(0, wx, wy, #PB_Ignore, #PB_Ignore)
  hwnd = WindowFromPoint_(gPt)
  If hwnd<>oldhwnd Or ev=#PB_Event_Repaint
    hwnd$ = Hex(hwnd)
    caption$ = Space(100)
    GetWindowText_(hwnd, @caption$, 99)
    ctrlid$ = Str(GetWindowLong_(hwnd, #GWL_ID))
    class$ = Space(140)
    GetClassName_(hwnd, @class$, 139)
    SetGadgetText(8, hwnd$)
    SetGadgetText(9, caption$)
    SetGadgetText(10, ctrlid$)
    SetGadgetText(11, class$)
    parent = GetParent_(hwnd)
    parent$ = Hex(parent)
    caption$ = Space(100)
    GetWindowText_(parent, @caption$, 99)
    ctrlid$ = Str(GetWindowLong_(parent, #GWL_ID))
    class$ = Space(140)
    GetClassName_(parent, @class$, 139)
    SetGadgetText(12, parent$)
    SetGadgetText(13, caption$)
    SetGadgetText(14, ctrlid$)
    SetGadgetText(15, class$)
    oldhwnd = hwnd   
    RedrawWindow_(WindowID(0),0,0,#RDW_UPDATENOW)
  EndIf

Until GetAsyncKeyState_(#VK_RBUTTON) & 32768 

Re: Getting window handle of unkown caption

Posted: Fri Sep 09, 2011 3:16 pm
by netmaestro

Code: Select all

hwnd = WindowFromPoint_( gPt\x | (gPt\y<<32) )
or:

Code: Select all

hwnd = WindowFromPoint_( PeekQ(gPt) )
should serve equally well.

Re: Getting window handle of unkown caption

Posted: Sat Sep 10, 2011 12:41 am
by MachineCode
netmaestro wrote:hwnd = WindowFromPoint_( PeekQ(gPt) )
This is so much better and easier to read than the "<<" method! Thanks! :)

Re: Getting window handle of unkown caption

Posted: Sun Sep 11, 2011 12:28 am
by IdeasVacuum
Elegant! Thanks netmaestro. 8)

Re: Getting window handle of unkown caption

Posted: Mon Aug 07, 2017 5:32 am
by firace
Can anyone see why netmaestro's program no longer works?
No errors, I'm only getting empty values.

Re: Getting window handle of unkown caption

Posted: Mon Aug 07, 2017 9:52 am
by Bisonte
you have to replace :

Code: Select all

hwnd = WindowFromPoint_( gPt )
with

Code: Select all

hwnd = WindowFromPoint_( PeekQ(gPt) )
it works ;)

Re: Getting window handle of unkown caption

Posted: Mon Aug 07, 2017 10:09 am
by firace
Bisonte wrote:you have to replace :

Code: Select all

hwnd = WindowFromPoint_( gPt )
with

Code: Select all

hwnd = WindowFromPoint_( PeekQ(gPt) )
it works ;)
Thanks Bisonte, indeed!

(And I should have read the posts above mine a little more carefully... :oops: )