VB 擷取對象滑鼠經過與離開事件
在編寫程式時,有時,為了美化操作,或者進行特殊資料的提醒,比如,自定義tooltips,可以通過下面的代碼實作,或者,當滑鼠經過按鈕時,進行特殊提醒等操作,可以用如下方法實作:
注意:如果是按鈕,像下面的程式,背景是否表色還要看按鈕的Style是否是1,隻有是1,才會有如下圖效果。
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If 0 <= X And X <= Command1.Width And 0 <= Y And Y <= Command1.Height Then
SetCapture Command1.hwnd
‘下面寫上滑鼠經過的程式段
Command1.BackColor = vbRed
Else
ReleaseCapture
'下面寫滑鼠離開的程式段,這樣就可以優化自帶的MouseMove事件了
Command1.BackColor = &H8000000F
End If
End Sub