VB中缺少一個(gè)MouseLeave事件,給編程帶來許多不便。例如,當(dāng)我們想令鼠標(biāo)移動(dòng)到一個(gè)命令按鈕時(shí)令其Caption屬性為一個(gè)名稱,離開時(shí)又是另一個(gè)名稱,我們通常只能這么處理:用兩個(gè)MouseMove事件:一個(gè)是命令按鈕的,另一個(gè)可能是窗體的,也可能是命令按鈕周邊的其它控件的。——這無疑很麻煩,而且當(dāng)控件較為緊湊時(shí)往往達(dá)不到預(yù)期的目的。有沒有什么辦法可以判斷鼠標(biāo)已經(jīng)從某一個(gè)控件中移走呢?請看下面的例子。
在標(biāo)準(zhǔn)EXE工程中缺省創(chuàng)建一個(gè)TextBox,然后鍵入以下代碼:
Option Explicit
’申明API函數(shù)——
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
’通過Text1的MouseMove事件判斷鼠標(biāo)指針位置
Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseLeave As Boolean
MouseLeave = (0 <= X) And (X <= Text1.Width) And (0 <= Y) And (Y <= Text1.Height)
If MouseLeave Then
Text1 = "Inside"
SetCapture Text1.hWnd
Else
Text1 = "Outside"
ReleaseCapture
End If
End Sub
運(yùn)行程序,把鼠標(biāo)移到Text1,Text1顯示:Inside,移開則顯示:Outside,可見已經(jīng)達(dá)到目的了。
此法適合于一切具有hWnd屬性的控件(如下所列):
ProgressBar控件,Slider控件,StatusBar控件,TabStrip控件,ToolbarControl,Animation控件,UpDown控件,DBCombo控件,DBList控件,SSTab控件,PicturtClip控件,RichTextBox控件,PropertyPage對象,UserControl對象,UserDocument對象,CheckBox控件,ComboBox控件,CommandButton控件,DirListBox控件,DriveListBox控件,F(xiàn)ileListBox控件,F(xiàn)orm對象,F(xiàn)orms集合,F(xiàn)rame控件,HscrollBar,VScrollBar控件,ListBox控件,MDIForm對象,OptionButton控件,PictureBox控件,TextBox控件,OLEContainer控件。
在標(biāo)準(zhǔn)EXE工程中缺省創(chuàng)建一個(gè)TextBox,然后鍵入以下代碼:
Option Explicit
’申明API函數(shù)——
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
’通過Text1的MouseMove事件判斷鼠標(biāo)指針位置
Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseLeave As Boolean
MouseLeave = (0 <= X) And (X <= Text1.Width) And (0 <= Y) And (Y <= Text1.Height)
If MouseLeave Then
Text1 = "Inside"
SetCapture Text1.hWnd
Else
Text1 = "Outside"
ReleaseCapture
End If
End Sub
運(yùn)行程序,把鼠標(biāo)移到Text1,Text1顯示:Inside,移開則顯示:Outside,可見已經(jīng)達(dá)到目的了。
此法適合于一切具有hWnd屬性的控件(如下所列):
ProgressBar控件,Slider控件,StatusBar控件,TabStrip控件,ToolbarControl,Animation控件,UpDown控件,DBCombo控件,DBList控件,SSTab控件,PicturtClip控件,RichTextBox控件,PropertyPage對象,UserControl對象,UserDocument對象,CheckBox控件,ComboBox控件,CommandButton控件,DirListBox控件,DriveListBox控件,F(xiàn)ileListBox控件,F(xiàn)orm對象,F(xiàn)orms集合,F(xiàn)rame控件,HscrollBar,VScrollBar控件,ListBox控件,MDIForm對象,OptionButton控件,PictureBox控件,TextBox控件,OLEContainer控件。