作者:土人 一.通過鼠標在屏幕上的移動來控件程序界面
本例通過鼠標在屏幕上的移動來控制程序窗體的顯示與隱藏:當鼠標移動到窗體所在區域時窗體顯示,反之隱藏起來。僅需一條API函數:GetCursorPos。注意:如果需要將API函數置于模塊中請對代碼作相應修改。要嘗試本例,需給標準EXE工程缺省添加一個Timer控件。
Private Type POINTAPI x As Long y As Long End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Sub Form_Load() Me.Visible = False Timer1.Enabled = True Timer1.Interval = 100 End Sub
Private Sub Timer1_Timer() Dim lResult As Long Dim lpPoint As POINTAPI Dim iCounter As Integer lResult = GetCursorPos(lpPoint) If lpPoint.x < Me.Left \ Screen.TwipsPerPixelX Or lpPoint.x > (Me.Left + _ Me.Width) \ Screen.TwipsPerPixelX Or lpPoint.y < Me.Top \ _ Screen.TwipsPerPixelY Or lpPoint.y - 10 > (Me.Top + Me.Height) \ _ Screen.TwipsPerPixelY Then Me.Visible = False '鼠標在窗體區域之外時 Else Me.Visible = True '鼠標在窗體區域之內時 End If End Sub
二.獲得Mouse_Exit事件
所謂Mouse_Exit事件,是指鼠標指針離開某一控件所應發生的事件。本例是通過Form_MouseMove事件來判斷鼠標指針是在窗體之內還是窗體之外的,你可根據需要作相應改動。請給窗體缺省創建一個按鈕(用于觀察效果)。
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim MouseExit As Boolean MouseExit = (0 <= X) And (X <= Me.Width) And (0 <= Y) And (Y <= Me.Height) If MouseExit Then Me.Caption = "鼠標指針在窗體范圍內" Command1.Enabled = True SetCapture Me.hWnd Else Me.Caption = "鼠標指針在窗體范圍外" Command1.Enabled = False ReleaseCapture End If End Sub
|