Mouse coordinates in window
Mouse coordinates in window
Hiya,
How do i get the position of the mouse inside the current window?
I try to use ExamineMouse() but that ends up locking (and hiding) the mouse in the 'screen' thats inside the window.
Thanks,
Tim
How do i get the position of the mouse inside the current window?
I try to use ExamineMouse() but that ends up locking (and hiding) the mouse in the 'screen' thats inside the window.
Thanks,
Tim
The ExamineMouse() command should only be used in games applications.
Instead use the WindowMouseX() and the WindowMouseY() functions.
Alternatively: (Win API)
Instead use the WindowMouseX() and the WindowMouseY() functions.
Alternatively: (Win API)
Code: Select all
GetCursorPos_(mouse.POINT)
xpos = mouse\x
I may look like a mule, but I'm not a complete ass.
-
- Enthusiast
- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
And just once more again (the LAST time) :
Because WindowMouseX / Y retrieve the coordinates from the upper left corner of the window inclusive titlebar/menubars/frameborders, etc., here's a way to get these information exclusive these values:
Because WindowMouseX / Y retrieve the coordinates from the upper left corner of the window inclusive titlebar/menubars/frameborders, etc., here's a way to get these information exclusive these values:
Code: Select all
;
; by Danilo, 30.04.2003 - german forum
; modified example-main loop by Froggerprogger
;
Procedure WindowClientMouseX()
; Returns X-Position of MouseCursor
; in the current window's client area
; or -1 if mouse cursor isnt in this area.
GetCursorPos_(mouse.POINT)
ScreenToClient_(WindowID(),mouse)
GetClientRect_(WindowID(),rect.RECT)
If mouse\x < 0 Or mouse\x > rect\right
ProcedureReturn -1
Else
ProcedureReturn mouse\x
EndIf
EndProcedure
Procedure WindowClientMouseY()
; Returns Y-Position of MouseCursor
; in the current window's client area
; or -1 if mouse cursor isnt in this area.
GetCursorPos_(mouse.POINT)
ScreenToClient_(WindowID(),mouse)
GetClientRect_(WindowID(),rect.RECT)
If mouse\y < 0 Or mouse\y > rect\bottom
ProcedureReturn -1
Else
ProcedureReturn mouse\y
EndIf
EndProcedure
OpenWindow(1,200,200,300,300,#PB_Window_SystemMenu,"MousePos")
CreateMenu(0, WindowID())
MenuTitle("a fake menu")
CreateGadgetList(WindowID())
TextGadget(0,4,4,192,20,"")
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
End
Default
SetGadgetText(0,"MouseX: "+Str( WindowClientMouseX()) + " MouseY: "+Str(WindowClientMouseY()))
Delay(10)
EndSelect
ForEver
Last edited by Froggerprogger on Wed Apr 07, 2004 12:00 am, edited 2 times in total.
%1>>1+1*1/1-1!1|1&1<<$1=1
You always post the old procedures...Froggerprogger wrote:And just once more again (the LAST time) :
Code: Select all
;
; by Danilo, 30.04.2003 - german forum
;
; modified 11.12.2003
;
Procedure WindowClientMouseX(win) ; added param, 11.12.03
; Returns X-Position of MouseCursor
; in the current window's client area
; or -1 if mouse cursor isnt in this area.
GetCursorPos_(mouse.POINT)
ScreenToClient_(WindowID(win),mouse)
GetClientRect_(WindowID(win),rect.RECT)
If mouse\x < 0 Or mouse\x > rect\right
ProcedureReturn -1
ElseIf mouse\y < 0 Or mouse\y > rect\bottom ; added 11.12.03
ProcedureReturn - 1
Else
ProcedureReturn mouse\x
EndIf
EndProcedure
Procedure WindowClientMouseY(win) ; added param, 11.12.03
; Returns Y-Position of MouseCursor
; in the current window's client area
; or -1 if mouse cursor isnt in this area.
GetCursorPos_(mouse.POINT)
ScreenToClient_(WindowID(win),mouse)
GetClientRect_(WindowID(win),rect.RECT)
If mouse\y < 0 Or mouse\y > rect\bottom
ProcedureReturn -1
ElseIf mouse\x < 0 Or mouse\x > rect\right ; added 11.12.03
ProcedureReturn - 1
Else
ProcedureReturn mouse\y
EndIf
EndProcedure
Why complicate if one can make simple
Nicolas
Code: Select all
;Procedure to return coordinate mouse in the aera client
Procedure WindowClientMouse(win)
Shared mouse.POINT
ValueReturn=#False
If GetCursorPos_(mouse.POINT)
If ScreenToClient_(win,mouse)
ValueReturn=#True
EndIf
EndIf
ProcedureReturn ValueReturn
EndProcedure
window.l=OpenWindow(0,200,200,200,200,#PB_Window_SystemMenu,"Aera client Mouse move")
CreateGadgetList(WindowID())
TextGadget(0,4,4,192,20,"")
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
Quit=1
Case #WM_MOUSEMOVE
If WindowClientMouse(window)
SetGadgetText(0,"MouseX: "+Str( mouse\x)+", MouseY:"+Str(mouse\y))
EndIf
EndSelect
Until Quit = 1
End
Because my procedures work exactly the same way thanNico wrote:Why complicate if one can make simple
WindowMouseX() and WindowMouseY(). The 2 functions
return the mouse coordinates X and Y for the specified window.
The difference is only that my procs return the client coordinates.
Using a shared variable isnt good for procedures you can use
in any program, just cut & paste.
When i already have a variable 'mouse' in my code, i have
to change your procedure. Your procedure may overwrite
my own variable 'mouse' if its also declared as 'POINT' -
thats an hard to find bug in 20.000 lines of code.
When i call your procedure in another procedure, i have to
declared 'Shared mouse.POINT' in every procedure i want
to use the function (or make it global).
Why make it complicated when you can make it simple?
It's also true for the name of procedures, it's not really a problem.
To avoid writing twice the same procedure and to declare variable global, you can also write this :
Friendly, Nicolas!
To avoid writing twice the same procedure and to declare variable global, you can also write this :
Code: Select all
;Procedure to return coordinate mouse in the aera client
Procedure WindowClientMouse(win.l,coordinate.s)
If GetCursorPos_(mouse.POINT)
If ScreenToClient_(win,mouse)
If coordinate="X"
ProcedureReturn mouse\x
ElseIf coordinate="Y"
ProcedureReturn mouse\y
EndIf
EndIf
EndIf
EndProcedure
window.l=OpenWindow(0,200,200,200,200,#PB_Window_SystemMenu,"Aera client Mouse move")
CreateGadgetList(WindowID())
TextGadget(0,4,4,192,20,"")
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
Quit=1
Case #WM_MOUSEMOVE
x=WindowClientMouse(window,"X")
y=WindowClientMouse(window,"Y")
SetGadgetText(0,"MouseX: "+Str( x)+", MouseY:"+Str(y))
EndSelect
Until Quit = 1
End
Certainly best solution:
I think that one made the tour of the problem.............
Code: Select all
;Procedure to return coordinate mouse in the aera client
Procedure MyWindowCallback(WindowID,Message,wParam,lParam)
Result=#PB_ProcessPureBasicEvents
Select Message
Case #WM_MOUSEMOVE
mouseX=lParam& $FFFF
mouseY=lParam >>16
SetGadgetText(0,"MouseX: "+Str( mouseX)+", MouseY:"+Str(mouseY))
EndSelect
ProcedureReturn Result
EndProcedure
window.l=OpenWindow(0,200,200,200,200,#PB_Window_SystemMenu,"Aera client Mouse move")
CreateGadgetList(WindowID())
TextGadget(0,4,4,192,20,"")
SetWindowCallback(@MyWindowCallback())
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
Quit=1
;------------------------------------
; The rest of your code here
;------------------------------------
EndSelect
Until Quit = 1
End
Pointer to a structure:
Nicolas
Code: Select all
Procedure WindowClientMouse(win.l,*mouse.POINT)
If GetCursorPos_(*mouse)
If ScreenToClient_(win,*mouse)
ProcedureReturn 1
EndIf
EndIf
EndProcedure
window.l=OpenWindow(0,200,200,200,200,#PB_Window_SystemMenu,"Aera client Mouse move")
CreateGadgetList(WindowID())
TextGadget(0,4,4,192,20,"")
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
Quit=1
Case #WM_MOUSEMOVE
If WindowClientMouse(window,@coordinate.POINT )
SetGadgetText(0,"MouseX: "+Str( coordinate\x)+", MouseY:"+Str(coordinate\y))
EndIf
EndSelect
Until Quit = 1
End