Getting window handle of unkown caption
-
- User
- Posts: 78
- Joined: Sun Apr 24, 2005 3:22 am
Getting window handle of unkown caption
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?
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?
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

-
- User
- Posts: 78
- Joined: Sun Apr 24, 2005 3:22 am
thank you, your code works great, but I'm having more troubles with getting it to the program because it's losing focus.
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.
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
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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
@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.
(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.

-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Getting window handle of unkown caption
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.........
I assume this must be because I have not implemented the Point correctly - can anybody fix it?
So, I added a Point and the app runs very well without complaint, but it does not return any handles.........

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.
If it sounds simple, you have not grasped the complexity.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Getting window handle of unkown caption
Code: Select all
hwnd = WindowFromPoint_( gPt\x | (gPt\y<<32) )
Code: Select all
hwnd = WindowFromPoint_( PeekQ(gPt) )
BERESHEIT
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: Getting window handle of unkown caption
This is so much better and easier to read than the "<<" method! Thanks!netmaestro wrote:hwnd = WindowFromPoint_( PeekQ(gPt) )

Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Getting window handle of unkown caption
Elegant! Thanks netmaestro. 

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Getting window handle of unkown caption
Can anyone see why netmaestro's program no longer works?
No errors, I'm only getting empty values.
No errors, I'm only getting empty values.
Re: Getting window handle of unkown caption
you have to replace :
with
it works 
Code: Select all
hwnd = WindowFromPoint_( gPt )
Code: Select all
hwnd = WindowFromPoint_( PeekQ(gPt) )

Re: Getting window handle of unkown caption
Thanks Bisonte, indeed!Bisonte wrote:you have to replace :
withCode: Select all
hwnd = WindowFromPoint_( gPt )
it worksCode: Select all
hwnd = WindowFromPoint_( PeekQ(gPt) )
(And I should have read the posts above mine a little more carefully...
