明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

鼠標(biāo)編程小技巧二則

[摘要]作者:土人 一.通過(guò)鼠標(biāo)在屏幕上的移動(dòng)來(lái)控件程序界面本例通過(guò)鼠標(biāo)在屏幕上的移動(dòng)來(lái)控制程序窗體的顯示與隱藏:當(dāng)鼠標(biāo)移動(dòng)到窗體所在區(qū)域時(shí)窗體顯示,反之隱藏起來(lái)。僅需一條API函數(shù):GetCursorPos。注意:如果需要將API函數(shù)置于模塊中請(qǐng)對(duì)代碼作相應(yīng)修改。要嘗試本例,需給標(biāo)準(zhǔn)EXE工程缺省添加一...
作者:土人
一.通過(guò)鼠標(biāo)在屏幕上的移動(dòng)來(lái)控件程序界面

本例通過(guò)鼠標(biāo)在屏幕上的移動(dòng)來(lái)控制程序窗體的顯示與隱藏:當(dāng)鼠標(biāo)移動(dòng)到窗體所在區(qū)域時(shí)窗體顯示,反之隱藏起來(lái)。僅需一條API函數(shù):GetCursorPos。注意:如果需要將API函數(shù)置于模塊中請(qǐng)對(duì)代碼作相應(yīng)修改。要嘗試本例,需給標(biāo)準(zhǔn)EXE工程缺省添加一個(gè)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 '鼠標(biāo)在窗體區(qū)域之外時(shí)
Else
Me.Visible = True '鼠標(biāo)在窗體區(qū)域之內(nèi)時(shí)
End If
End Sub

二.獲得Mouse_Exit事件

所謂Mouse_Exit事件,是指鼠標(biāo)指針離開(kāi)某一控件所應(yīng)發(fā)生的事件。本例是通過(guò)Form_MouseMove事件來(lái)判斷鼠標(biāo)指針是在窗體之內(nèi)還是窗體之外的,你可根據(jù)需要作相應(yīng)改動(dòng)。請(qǐng)給窗體缺省創(chuàng)建一個(gè)按鈕(用于觀察效果)。

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 = "鼠標(biāo)指針在窗體范圍內(nèi)"
Command1.Enabled = True
SetCapture Me.hWnd
Else
Me.Caption = "鼠標(biāo)指針在窗體范圍外"
Command1.Enabled = False
ReleaseCapture
End If
End Sub