home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch12 / mousepos / mousemov / mousemov.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-02-20  |  3.1 KB  |  103 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Mouse Track"
  4.    ClientHeight    =   2280
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   4680
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   2280
  10.    ScaleWidth      =   4680
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton Command1 
  13.       Caption         =   "Start"
  14.       Height          =   495
  15.       Left            =   1200
  16.       TabIndex        =   1
  17.       Top             =   1440
  18.       Width           =   2295
  19.    End
  20.    Begin VB.Label Label1 
  21.       Caption         =   "Tracking the mouse."
  22.       Height          =   615
  23.       Left            =   360
  24.       TabIndex        =   0
  25.       Top             =   240
  26.       Width           =   3855
  27.    End
  28. Attribute VB_Name = "Form1"
  29. Attribute VB_GlobalNameSpace = False
  30. Attribute VB_Creatable = False
  31. Attribute VB_PredeclaredId = True
  32. Attribute VB_Exposed = False
  33. Option Explicit
  34. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  35. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, _
  36.     ByVal yPoint As Long) As Long
  37. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
  38.     (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  39. Private Type POINTAPI
  40.         X As Long
  41.         Y As Long
  42. End Type
  43. Private gStop As Boolean
  44. Private Sub Command1_Click()
  45.     Dim mousePT As POINTAPI
  46.     Dim prevWindow As Long
  47.     Dim curWindow As Long
  48.     Dim X As Long
  49.     Dim Y As Long
  50.     Dim className As String
  51.     Dim retValue As Long
  52.     'Track mouse here
  53.     If Command1.Caption = "Start" Then
  54.         Command1.Caption = "Stop"
  55.         
  56.         gStop = False
  57.         prevWindow = 0
  58.         
  59.         'Track until user stops
  60.         Do
  61.             'Stop tracking
  62.             If gStop = True Then Exit Do
  63.             
  64.             Call GetCursorPos(mousePT)
  65.             
  66.             X = mousePT.X
  67.             Y = mousePT.Y
  68.             
  69.             'Get window under mouse
  70.             curWindow = WindowFromPoint(X, Y)
  71.             
  72.             If curWindow <> prevWindow Then
  73.             
  74.                 className = String$(256, " ")
  75.                 prevWindow = curWindow
  76.                 
  77.                 retValue = GetClassName(curWindow, className, 255)
  78.                 className = Left$(className, InStr(className, vbNullChar) - 1)
  79.                 
  80.                 If className = "SysListView32" Then
  81.                     Label1.Caption = "The mouse is over the desktop."
  82.                 Else
  83.                     Label1.Caption = "The mouse is over " & className
  84.                 End If
  85.                                 
  86.             End If
  87.             
  88.             
  89.             DoEvents
  90.             
  91.         Loop
  92.         
  93.            
  94.     'Stop tracking the mouse
  95.     Else
  96.         Command1.Caption = "Start"
  97.         gStop = True
  98.     End If
  99. End Sub
  100. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  101.     gStop = True
  102. End Sub
  103.