 |
VB本身没有提供这样的事件。只要鼠标进入控件就产生MouseMove事件,你可以用第一次触发MouseMove事件来代替MouseIn事件。
至于MouseLeave事件就麻烦,可以利用Form的MouseMove事件来代替控件的MouseLeave事件。
如果控件具有hWnd属性(Label没有这个属性,但CommandButtun有),也可以调用SetCapture和ReleaseCapture这两个Windows API函
数的方法来实现它。具体步骤如下:
1) 在VB中新建一个标准EXE工程;
2) 画出一个按钮Command1;
3) 在窗体Form1中定义Windows API的声明;
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
4) 在Command1的MouseMove事件中编写以下代码:
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseEnter As Boolean '鼠标进入的标志位
MouseEnter = (0 <= X) And (X <= Command1.Width) And (0 <= Y) And (Y <=
Command1.Height) '计算鼠标的移动是否在Command1里面
If MouseEnter Then '鼠标已经进入
Me.Caption = "Mouse In Button!"
SetCapture Command1.hWnd
Else '鼠标已经离开
Me.Caption = "Mouse Out!"
ReleaseCapture
End If
End Sub
另外一个简单的办法是使用免费的MouseTrap控件(http://www.halfx.com/downloads/MouseTrap.EXE)或者http://www.vbaccelerator.com/home/VB/Code/Libraries/Subclassing/Generating_MouseLeave_Events_for_a_Window/article.asp。
阳光的意见:
在周围放四个宽度尽量小的Label,当周围的Label的MouseMove发生时也就是放在中间控件MouseLeave事件。个人观点,仅供参考。
此问题由李海回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 标准控件, screen, button, combo, checkbox, listbox。
|