⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmouseleave.frm

📁 判断鼠标是否在焦点上
💻 FRM
字号:
VERSION 5.00
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "COMCTL32.OCX"
Begin VB.Form Form1 
   Caption         =   "Capture MouseLeave Event"
   ClientHeight    =   4260
   ClientLeft      =   2100
   ClientTop       =   1725
   ClientWidth     =   5895
   LinkTopic       =   "Form1"
   ScaleHeight     =   4260
   ScaleWidth      =   5895
   Begin VB.CommandButton Command2 
      Caption         =   "Command2"
      Height          =   495
      Left            =   3720
      TabIndex        =   2
      Top             =   1800
      Width           =   2055
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   495
      Left            =   3720
      TabIndex        =   1
      Top             =   960
      Width           =   2055
   End
   Begin ComctlLib.StatusBar StatusBar1 
      Align           =   2  'Align Bottom
      Height          =   495
      Left            =   0
      TabIndex        =   0
      Top             =   3765
      Width           =   5895
      _ExtentX        =   10398
      _ExtentY        =   873
      SimpleText      =   ""
      _Version        =   327680
      BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7} 
         NumPanels       =   1
         BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
            TextSave        =   ""
            Key             =   ""
            Object.Tag             =   ""
         EndProperty
      EndProperty
      MouseIcon       =   "TestMouseLeave.frx":0000
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' 标题: 在VB中捕捉MouseLeave事件
' 作者: James Guo
' 您可以自由地在您的应用程序应用该代码,无需征得作者的同意.
' 欢迎光临VB天堂, 一个VB程序员的VB主页.
' 地址:  http://www.zhanjiang.gd.cn/personal/jamgsguo
' Email: jamesguo@usa.net

Option Explicit
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)
    ' 没有使用MouseLeave事件,当鼠标快速移动时,程序来不及响应
    StatusBar1.Panels(1).Text = "This is Command1"
End Sub

Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim MouseOver As Boolean
    
    '判断当前鼠标位置是否在Command2上
    MouseOver = (0 <= X) And (X <= Command2.Width) And (0 <= Y) And (Y <= Command2.Height)
    If MouseOver Then
        ' MouseOver Event
        ' 假如鼠标在Command2上, 则利用SetCapture将每一个鼠标事件都传递给Command2
        ' 并在StatusBar1.Panels(1)上显示帮助信息
        StatusBar1.Panels(1).Text = "This is Command2"
        SetCapture Command2.hWnd
    Else
        ' MouseLeave Event
        ' 假如鼠标不在Command2上, 则利用SetCapture释放鼠标捕捉
        ' 并清除在StatusBar1.Panels(1)上的帮助信息
        StatusBar1.Panels(1).Text = ""
        ReleaseCapture
    End If
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StatusBar1.Panels(1).Text = ""
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -