Getting window handle of unkown caption

Just starting out? Need help? Post your questions and find answers here.
roachofdeath
User
User
Posts: 78
Joined: Sun Apr 24, 2005 3:22 am

Getting window handle of unkown caption

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

Post 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:
--Kale

Image
roachofdeath
User
User
Posts: 78
Joined: Sun Apr 24, 2005 3:22 am

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

Post 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. :)
--Kale

Image
rjara
User
User
Posts: 14
Joined: Wed May 23, 2007 5:17 pm

Post 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?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
Last edited by netmaestro on Tue Dec 27, 2016 10:58 am, edited 1 time in total.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Getting window handle of unkown caption

Post 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 
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Getting window handle of unkown caption

Post by netmaestro »

Code: Select all

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

Code: Select all

hwnd = WindowFromPoint_( PeekQ(gPt) )
should serve equally well.
BERESHEIT
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Getting window handle of unkown caption

Post by MachineCode »

netmaestro wrote:hwnd = WindowFromPoint_( PeekQ(gPt) )
This is so much better and easier to read than the "<<" method! Thanks! :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Getting window handle of unkown caption

Post by IdeasVacuum »

Elegant! Thanks netmaestro. 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Getting window handle of unkown caption

Post by firace »

Can anyone see why netmaestro's program no longer works?
No errors, I'm only getting empty values.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Getting window handle of unkown caption

Post by Bisonte »

you have to replace :

Code: Select all

hwnd = WindowFromPoint_( gPt )
with

Code: Select all

hwnd = WindowFromPoint_( PeekQ(gPt) )
it works ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Getting window handle of unkown caption

Post 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: )
Post Reply