home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 June / Chip_1999-06_cd.bin / zkuste / VBasic / Data / Priklady / mouse.bas < prev    next >
BASIC Source File  |  1999-04-02  |  7KB  |  236 lines

  1. Option Explicit
  2.  
  3. ' ----------------------------------------------
  4. ' *        MouseEvent Related Declares         *
  5. ' ----------------------------------------------
  6. Private Const MOUSEEVENTF_ABSOLUTE = &H8000
  7. Private Const MOUSEEVENTF_LEFTDOWN = &H2
  8. Private Const MOUSEEVENTF_LEFTUP = &H4
  9. Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
  10. Private Const MOUSEEVENTF_MIDDLEUP = &H40
  11. Private Const MOUSEEVENTF_MOVE = &H1
  12. Private Const MOUSEEVENTF_RIGHTDOWN = &H8
  13. Private Const MOUSEEVENTF_RIGHTUP = &H10
  14.  
  15. Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cbuttons As Long, ByVal dwExtraInfo As Long)
  16.  
  17. ' ----------------------------------------------
  18. ' *     GetSystemMetrics Related Declares      *
  19. ' ----------------------------------------------
  20. Private Const SM_CXSCREEN = 0
  21. Private Const SM_CYSCREEN = 1
  22. Private Const TWIPS_PER_INCH = 1440
  23. Private Const POINTS_PER_INCH = 72
  24. Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex _
  25.     As Long) As Long
  26.  
  27. ' ----------------------------------------------
  28. ' *       GetWindowRect Related Declares       *
  29. ' ----------------------------------------------
  30. Private Type RECT
  31.         Left As Long
  32.         Top As Long
  33.         Right As Long
  34.         Bottom As Long
  35. End Type
  36. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
  37.     lpRect As RECT) As Long
  38.  
  39. Private lShowCursor As Long
  40. Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
  41.  
  42.  
  43. ' ----------------------------------------------
  44. ' *       Internal Constants and Types         *
  45. ' ----------------------------------------------
  46.  
  47. Private Const MOUSE_MICKEYS = 65535
  48.  
  49. Public Enum enReportStyle
  50.     rsPixels
  51.     rsTwips
  52.     rsInches
  53.     rsPoints
  54. End Enum
  55.  
  56. Public Enum enButtonToClick
  57.     btcLeft
  58.     btcRight
  59.     btcMiddle
  60. End Enum
  61.  
  62. 'Hides the cursor of mouse
  63. Public Sub HideMouse()
  64.    Dim result As Integer
  65.    Do
  66.       lShowCursor = lShowCursor - 1
  67.       result = ShowCursor(False)
  68.    Loop Until result < 0  
  69. End Sub
  70.  
  71. 'Gives back the cursor of mouse to prime place
  72. Public Sub RestoreMouse()
  73.    If lShowCursor > 0 Then
  74.       Do While lShowCursor <> 0
  75.          ShowCursor (False)
  76.          lShowCursor = lShowCursor - 1
  77.       Loop
  78.    ElseIf lShowCursor < 0 Then
  79.       Do While lShowCursor <> 0
  80.          ShowCursor (True)
  81.          lShowCursor = lShowCursor + 1
  82.       Loop
  83.    End If
  84. End Sub
  85.  
  86. ' Returns the screen size in pixels or, optionally, in others scalemode styles
  87. Public Sub GetScreenRes(ByRef X As Long, ByRef Y As Long, Optional ByVal _
  88.     ReportStyle As enReportStyle)
  89.  
  90.     X = GetSystemMetrics(SM_CXSCREEN)
  91.     Y = GetSystemMetrics(SM_CYSCREEN)
  92.     If Not IsMissing(ReportStyle) Then
  93.          If ReportStyle <> rsPixels Then
  94.             X = X * Screen.TwipsPerPixelX
  95.             Y = Y * Screen.TwipsPerPixelY
  96.             If ReportStyle = rsInches Or ReportStyle = rsPoints Then
  97.                 X = X \ TWIPS_PER_INCH
  98.                 Y = Y \ TWIPS_PER_INCH
  99.                 If ReportStyle = rsPoints Then
  100.                     X = X * POINTS_PER_INCH
  101.                     Y = Y * POINTS_PER_INCH
  102.                 End If
  103.             End If
  104.         End If
  105.     End If
  106. End Sub
  107.  
  108. ' Convert's the mouses coordinate system to a pixel position.
  109. Public Function MickeyXToPixel(ByVal mouseX As Long) As Long
  110.     Dim X As Long
  111.     Dim Y As Long
  112.     Dim tX As Single
  113.     Dim tmouseX As Single
  114.     Dim tMickeys As Single
  115.     
  116.     GetScreenRes X, Y
  117.     tX = X
  118.     tMickeys = MOUSE_MICKEYS
  119.     tmouseX = mouseX
  120.     
  121.     MickeyXToPixel = CLng(tmouseX / (tMickeys / tX))
  122.     
  123. End Function
  124.  
  125. ' Converts mouse Y coordinates to pixels
  126. Public Function MickeyYToPixel(ByVal mouseY As Long) As Long
  127.     Dim X As Long
  128.     Dim Y As Long
  129.     Dim tY As Single
  130.     Dim tmouseY As Single
  131.     Dim tMickeys As Single
  132.     
  133.     GetScreenRes X, Y
  134.     tY = Y
  135.     tMickeys = MOUSE_MICKEYS
  136.     tmouseY = mouseY
  137.     
  138.     MickeyYToPixel = CLng(tmouseY / (tMickeys / tY))
  139.     
  140. End Function
  141.  
  142. ' Converts pixel X coordinates to mickeys
  143. Public Function PixelXToMickey(ByVal pixX As Long) As Long
  144.     Dim X As Long
  145.     Dim Y As Long
  146.     Dim tX As Single
  147.     Dim tpixX As Single
  148.     Dim tMickeys As Single
  149.     
  150.     GetScreenRes X, Y
  151.     tMickeys = MOUSE_MICKEYS
  152.     tX = X
  153.     tpixX = pixX
  154.     
  155.     PixelXToMickey = CLng((tMickeys / tX) * tpixX)
  156.  
  157. End Function
  158.  
  159. ' Converts pixel Y coordinates to mickeys
  160. Public Function PixelYToMickey(ByVal pixY As Long) As Long
  161.     Dim X As Long
  162.     Dim Y As Long
  163.     Dim tY As Single
  164.     Dim tpixY As Single
  165.     Dim tMickeys As Single
  166.     
  167.     GetScreenRes X, Y
  168.     tMickeys = MOUSE_MICKEYS
  169.     tY = Y
  170.     tpixY = pixY
  171.     
  172.     PixelYToMickey = CLng((tMickeys / tY) * tpixY)
  173.  
  174. End Function
  175.  
  176. ' The function will center the mouse on a window
  177. ' or control with an hWnd property.  No checking
  178. ' is done to ensure that the window is not obscured
  179. ' or not minimized, however it does make sure that
  180. ' the target is within the boundaries of the
  181. ' screen.
  182. Public Function CenterMouseOn(ByVal hwnd As Long) As Boolean
  183.     Dim X As Long
  184.     Dim Y As Long
  185.     Dim maxX As Long
  186.     Dim maxY As Long
  187.     Dim crect As RECT
  188.     Dim rc As Long
  189.  
  190.     GetScreenRes maxX, maxY
  191.     rc = GetWindowRect(hwnd, crect)
  192.     
  193.     If rc Then
  194.         X = crect.Left + ((crect.Right - crect.Left) / 2)
  195.         Y = crect.Top + ((crect.Bottom - crect.Top) / 2)
  196.         If (X >= 0 And X <= maxX) And (Y >= 0 And Y <= maxY) Then
  197.             MouseMove X, Y
  198.             CenterMouseOn = True
  199.         Else
  200.             CenterMouseOn = False
  201.         End If
  202.     Else
  203.         CenterMouseOn = False
  204.     End If
  205. End Function
  206.  
  207. ' Simulates a mouse click
  208. Public Function MouseFullClick(ByVal MBClick As enButtonToClick) As Boolean
  209.     Dim cbuttons As Long
  210.     Dim dwExtraInfo As Long
  211.     Dim mevent As Long
  212.     
  213.     Select Case MBClick
  214.         Case btcLeft
  215.             mevent = MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP
  216.         Case btcRight
  217.             mevent = MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP
  218.         Case btcMiddle
  219.             mevent = MOUSEEVENTF_MIDDLEDOWN Or MOUSEEVENTF_MIDDLEUP
  220.         Case Else
  221.             MouseFullClick = False
  222.             Exit Function
  223.     End Select
  224.     mouse_event mevent, 0&, 0&, cbuttons, dwExtraInfo
  225.     MouseFullClick = True
  226.     
  227. End Function
  228.  
  229. Public Sub MouseMove(ByRef xPixel As Long, ByRef yPixel As Long)
  230.     Dim cbuttons As Long
  231.     Dim dwExtraInfo As Long
  232.     
  233.     mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, _
  234.         PixelXToMickey(xPixel), PixelYToMickey(yPixel), cbuttons, dwExtraInfo
  235.  
  236. End Sub