Mouse coordinates in window

Just starting out? Need help? Post your questions and find answers here.
TimmyTom
User
User
Posts: 36
Joined: Mon Aug 18, 2003 8:32 am

Mouse coordinates in window

Post by TimmyTom »

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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The ExamineMouse() command should only be used in games applications.

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.
TimmyTom
User
User
Posts: 36
Joined: Mon Aug 18, 2003 8:32 am

Post by TimmyTom »

Didn't even know those existed!

Thanks!

Tim
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

And just once more again (the LAST time) : :D

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
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

Froggerprogger wrote:And just once more again (the LAST time) : :D
You always post the old procedures... :D

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
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

Why complicate if one can make simple :!:

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
Nicolas :wink:
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

Nico wrote:Why complicate if one can make simple :!:
Because my procedures work exactly the same way than
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? 8)
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

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 :

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
Friendly, Nicolas!
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

Certainly best solution:

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
I think that one made the tour of the problem.............
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

Pointer to a structure:

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
Nicolas :D
Post Reply