Point in client rect
Point in client rect
I would like to know if the mouse is inside of the client area of the window. is this possible to do without using GetWindowRect_() and then adding the border and titlebar width based on the system metrics?
The window I'm doing this to is not a PB window so I cannot use WindowMouseX() and WindowMouseY().
The window I'm doing this to is not a PB window so I cannot use WindowMouseX() and WindowMouseY().
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Here's an example of what I was thinking of with GetClientRect_():
Code: Select all
OpenWindow(0, #PB_Ignore, #PB_Ignore, 300, 300, "")
in.b = #False
Repeat
ev = WaitWindowEvent(10)
GetCursorPos_(@pt.POINT)
ScreenToClient_(WindowID(0), @pt)
GetClientRect_(WindowID(0), @rc.RECT)
If PtInRect_(@rc, pt\x, pt\y)
If Not in : Debug "Entered client area" : EndIf
in = #True
Else
If in : Debug "Left client area" : EndIf
in = #False
EndIf
Until ev = #PB_Event_CloseWindow
I didn't know about ScreenToClient_(). Your example makes perfect sense, thank you. 
This is the function I wanted to make with this:

This is the function I wanted to make with this:
Code: Select all
Procedure PointInClientRect(hWnd, x, y)
Define Point.POINT
Point\x=x
Point\y=y
ScreenToClient_(hWnd, @Point)
GetClientRect_(hWnd, @rc.RECT)
If PtInRect_(@rc, Point\x, Point\y)
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Code: Select all
Procedure PointInClientRect(hWnd, x, y)
Define Point.POINT
Point\x=x
Point\y=y
MapWindowPoints_(0, hWnd, Point, 1)
GetClientRect_(hWnd, @rc.RECT)
If PtInRect_(@rc, Point\x, Point\y)
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure

I may look like a mule, but I'm not a complete ass.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Code: Select all
OpenWindow(0,0,0,320,240,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ButtonGadget(0,100,70,100,30,"Button")
TextGadget(1, 100,130,200,20,"Not in Window")
TextGadget(2, 100,160,200,20,"Not in Button")
Repeat
ev=WaitWindowEvent(1)
Select ev
Case #WM_MOUSEMOVE
;Button
GetCursorPos_(@cp.point)
GetWindowRect_(GadgetID(0),@br.rect)
If PtInRect_(br, cp\x, cp\y)
MapWindowPoints_(#Null, GadgetID(0),cp,1)
SetGadgetText(2, "In Button: "+Str(cp\x)+", "+Str(cp\y))
Else
SetGadgetText(2, "Not in Button")
EndIf
;Window
GetCursorPos_(@cp.point)
GetWindowRect_(WindowID(0),@br.rect)
If PtInRect_(br, cp\x, cp\y)
MapWindowPoints_(#Null, WindowID(0),cp,1)
SetGadgetText(1, "In Window: "+Str(cp\x)+", "+Str(cp\y))
EndIf
EndSelect
If WindowMouseX(0) = -1 Or WindowMouseY(0)=-1
If GetGadgetText(1)<>"Not in Window"
SetGadgetText(1, "Not in Window")
EndIf
EndIf
Until ev=#PB_Event_CloseWindow
BERESHEIT