VB.net在Wince5.0下实现自定义控件的MouseLeave事件
主要就是要获取当前鼠标位置是否还在控件的rectangle区域内;实现步骤:
1.获取当前鼠标的屏幕坐标用:Control.MousePosition.X, Control.MousePosition.Y。
2.获取控件的rectangle区域的屏幕坐标 Dim rect As Rectangle = Me.RectangleToScreen(Me.ClientRectangle)。
3.判断当前获取的鼠标是否在上面获取的控件的屏幕坐标区域内If rect.Contains(Control.MousePosition.X, Control.MousePosition.Y) = False Then RaiseEvent mouseleave()。
在自定义控件时加上一个定时器,初始设置为不可用。间隔100ms,并且在自定义控件的onpaint方法中重写重绘按钮表面代码,即重写这个方法:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
重绘表面代码
end sub
判断鼠标当前是否离开代码如下:
Private Sub PictureButton_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Me.Timer1.Enabled = True
End Sub
Public Event mouseleave()
Dim flagleave As Boolean = False
Public Sub mouseleave_leave() Handles Me.mouseleave
Me.mouseover = False
Me.Invalidate()
flagleave = False
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If flagleave = False Then
flagleave = True
Me.mouseover = True
Me.Invalidate()
End If
Dim rect As Rectangle = Me.RectangleToScreen(Me.ClientRectangle)
If rect.Contains(Control.MousePosition.X, Control.MousePosition.Y) = False Then
RaiseEvent mouseleave()
End If
End Sub
摘自 执着于我的执着