Ooops
September 23, 2020, 06:59:36 AM

Author Topic: [bb] Improved Mouse Functions by sswift [ 1+ years ago ]  (Read 1142 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Improved Mouse Functions by sswift [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Improved Mouse Functions
Author : sswift
Posted : 1+ years ago

Description : This set of functions tells you where the mouse or a gadget is on the screen, and whether the mouse is over a gadget.

Update:
There are now functions to tell you where the mouse is relaitve to a gadget.  Unlike the Blitz MouseX & MouseY functions, these functions will work with any gadget, and will not crap out* on you at times.  

* (By crap out, I mean that the built in functions will sometimes stop returning coordinates if you click and hold near the edge of a canvas and then move outside it shortly thereafter.  Very annoying when you are trying to allow the user to click and drag things in the canvas.)


Code :
Code: BlitzBasic
  1. ; -----------------------------------------------------------------------------------------------------------------------------------
  2. ;
  3. ; This code allows you to determine where the mouse or a gadget is on the screen, and whether the mouse is over a gadget.
  4. ; To use this function, first place the following in the file "user32.decls" in your userlibs folder:
  5. ;
  6. ;       .lib "user32.dll"
  7. ;       GetCursorPos%(lpPoint*):"GetCursorPos"
  8. ;       ClientToScreen%(hWnd%, lpPoint*):"ClientToScreen"
  9. ;
  10. ; -----------------------------------------------------------------------------------------------------------------------------------
  11.  
  12.  
  13. Type TYPE_lpPoint
  14.         Field X
  15.         Field Y
  16. End Type
  17.  
  18. Global lpPoint.TYPE_lpPoint = New TYPE_lpPoint
  19.  
  20.  
  21. ; -----------------------------------------------------------------------------------------------------------------------------------
  22. ; This function returns TRUE if the mouse is over the specified gadget, and FALSE if it is not.
  23. ;
  24. ; Note:
  25. ; It has not been tested to see if it works properly with gadgets that are clipped by another gadget or window!
  26. ; -----------------------------------------------------------------------------------------------------------------------------------
  27. Function MouseOver(Gadget)
  28.  
  29.         Local Gx, Gy
  30.         Local Mx, My
  31.  
  32.         ; Get the screen coordinates of the gadget.
  33.                 Gx = GadgetScreenX(Gadget)
  34.                 Gy = GadgetScreenY(Gadget)
  35.                
  36.         ; Get the screen coordinates of the mouse cursor.
  37.                 Mx = MouseScreenX()
  38.                 My = MouseScreenY()
  39.        
  40.         ; Is the mouse within the rectangle defined by the gadget?
  41.                 If (Mx >= Gx) And (Mx < Gx+GadgetWidth(Gadget)) And (My >= Gy) And (My < Gy+GadgetHeight(Gadget)) Then Return True
  42.                 Return False
  43.        
  44. End Function
  45.  
  46.  
  47. ; -----------------------------------------------------------------------------------------------------------------------------------
  48. ; This function returns the X coordinates of a gadget on the screen.
  49. ; -----------------------------------------------------------------------------------------------------------------------------------
  50. Function GadgetScreenX(Gadget)
  51.  
  52.         Local HWnd
  53.  
  54.         ; Get a windows pointer to the gadget.
  55.                 HWnd = QueryObject(Gadget, 1)
  56.  
  57.         ; Set the point in the gadget which we want to find the screen coordinates of.
  58.                 lpPointX = 0
  59.                 lpPointY = 0
  60.                
  61.         ; Get the screen coordinates of the pixel in the gadget.
  62.                 ClientToScreen(HWnd, lpPoint)
  63.                 Return lpPointX
  64.  
  65. End Function
  66.  
  67.  
  68. ; -----------------------------------------------------------------------------------------------------------------------------------
  69. ; This function returns the Y coordinates of a gadget on the screen.
  70. ; -----------------------------------------------------------------------------------------------------------------------------------
  71. Function GadgetScreenY(Gadget)
  72.  
  73.         Local HWnd
  74.  
  75.         ; Get a windows pointer to the gadget.
  76.                 HWnd = QueryObject(Gadget, 1)
  77.  
  78.         ; Set the point in the gadget which we want to find the screen coordinates of.
  79.                 lpPointX = 0
  80.                 lpPointY = 0
  81.                
  82.         ; Get the screen coordinates of the pixel in the gadget.
  83.                 ClientToScreen(HWnd, lpPoint)
  84.                 Return lpPointY
  85.  
  86. End Function
  87.  
  88.  
  89. ; -----------------------------------------------------------------------------------------------------------------------------------
  90. ; This function returns the X coorindate of the mouse cursor on the screen.
  91. ; -----------------------------------------------------------------------------------------------------------------------------------
  92. Function MouseScreenX()
  93.         GetCursorPos(lpPoint)
  94.         Return lpPointX
  95. End Function
  96.  
  97.  
  98. ; -----------------------------------------------------------------------------------------------------------------------------------
  99. ; This function returns the Y coorindate of the mouse cursor on the screen.
  100. ; -----------------------------------------------------------------------------------------------------------------------------------
  101. Function MouseScreenY()
  102.         GetCursorPos(lpPoint)
  103.         Return lpPointY
  104. End Function
  105.  
  106.  
  107. ; -----------------------------------------------------------------------------------------------------------------------------------
  108. ; This function returns the X coordinate of the mouse relative to a gadget.
  109. ; -----------------------------------------------------------------------------------------------------------------------------------
  110. Function MouseGadgetX(Gadget)
  111.         Return MouseScreenX()-GadgetScreenX(Gadget)
  112. End Function
  113.  
  114.  
  115. ; -----------------------------------------------------------------------------------------------------------------------------------
  116. ; This function returns the X coordinate of the mouse relative to a gadget.
  117. ; -----------------------------------------------------------------------------------------------------------------------------------
  118. Function MouseGadgetY(Gadget)
  119.         Return MouseScreenY()-GadgetScreenY(Gadget)
  120. End Function


Comments :


TAS(Posted 1+ years ago)

 Thanks, Very useful


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal