IT学习联盟

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
IT学习联盟 门户 文章 VB 查看内容

VB.net在Wince5.0下实现自定义控件的MouseLeave事件

2012-8-18 17:28| 发布者: admin| 查看: 546| 评论: 0

主要就是要获取当前鼠标位置是否还在控件的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 


摘自  执着于我的执着 
Archiver|IT学习联盟| 网站地图

Powered by Discuz! X2.5© 2001-2010 世界学习室 大伟制作

GMT+8, 2018-2-25 05:36 , Processed in 0.100445 second(s), 12 queries .