首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么判断鼠标点击在Frame之外

2012-04-05 
如何判断鼠标点击在Frame之外程序中设置一个frame,用来查询信息用。如何能做到,当鼠标点击在Frame之外的任

如何判断鼠标点击在Frame之外
程序中设置一个frame,用来查询信息用。如何能做到,当鼠标点击在Frame之外的任一位置时,frame可以隐藏。
在form_click()事件不好用,因为form中还有几个frame,鼠标点击时,form_click触发不了,只能挨个控件写_click事件吗,有没有高手给个更好的办法。

[解决办法]
以下这个小例子,可以实现捕获鼠标进入和离开Frame区域的消息

VB code
Option ExplicitPrivate Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function ReleaseCapture Lib "user32" () As LongPrivate Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)    If X > 0 And Y > 0 And X < Frame1.Width And Y < Frame1.Height Then '检测是否在控件范围之内        ReleaseCapture        SetCapture Frame1.hwnd '此函数的参数是控件的句柄        Frame1.Caption = "鼠标进入了Frame1"        Else            ReleaseCapture            Frame1.Caption = "鼠标离开了Frame1"    End IfEnd Sub
[解决办法]
建一个模块:
Option Explicit
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long '获得整个窗口的范围矩形
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSLLHOOKSTRUCT '鼠标HOOK时lParam指针指向的结构
Pt As POINTAPI
dwMouseData As Long
dwFlags As Long
dwTime As Long
dwExtraInfo As Long
End Type
Public Type RECT
L As Long '左上角x1
T As Long '左上角y1
R As Long '右下角x2
B As Long '右下角y2
End Type
Public Const WH_MOUSE_LL = 14
Public Const WM_LBUTTONDOWN = &H201
Public lHook(1) As Long, Wt As RECT

Public Function LowLevelMouseProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fEatKeystroke As Boolean
Dim mhs As MSLLHOOKSTRUCT
Select Case wParam
Case WM_LBUTTONDOWN
Call CopyMemory(mhs, ByVal lParam, Len(mhs))
Debug.Print Wt.L, Wt.R, Wt.T, Wt.B, mhs.Pt.x, mhs.Pt.y
If (mhs.Pt.x < Wt.L Or mhs.Pt.x > Wt.R Or mhs.Pt.y < Wt.T Or mhs.Pt.y > Wt.B) Then
Debug.Print "鼠标点在Frame1外面了"
End If
End Select
If fEatKeystroke Then
LowLevelMouseProc = -1
Else
LowLevelMouseProc = CallNextHookEx(lHook(1), nCode, wParam, ByVal lParam)
End If
End Function

窗体:

Private Sub Form_Load()
lHook(1) = SetWindowsHookEx(WH_MOUSE_LL, AddressOf LowLevelMouseProc, App.hInstance, 0)
GetWindowRect Form1.Frame1.hwnd, Wt
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnhookWindowsHookEx lHook(1)
End Sub

热点排行