如何判断一个鼠标已离开一个控件如MOUSEMOVE

一元7毛6 2003-04-06 05:27:40
在判断一个鼠标已离开一个控件时,对于有hwnd属性的控件通常可以用SetCapture来进行处理。但是,如果没有hwnd属性,如Label,怎么办?
我的目的是用于一个自定义控件,考虑过用Hook,但是总感觉性能可能不会太好。
...全文
567 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingll 2003-04-07
  • 打赏
  • 举报
回复
怪,不是ScreenToClient 吗
怎么是ClientToScreen?
Intelement 2003-04-07
  • 打赏
  • 举报
回复
VB在这方面的表现不尽如人意
UserControl默认的方法和事件比较的少
如果想把内置子控件大多数或全部的方法和事件都向外界显露
则要声明的语句太多太多

再比如
做某个类似控件ComboBox的控件
要把它某个属性以类似ComboBox对象的List方法显露在IDE属性窗口中
则方法是不可行的

总之限制太多

.NET则在这方面先进很多
刘洪峰AIoT 2003-04-07
  • 打赏
  • 举报
回复
我做了个控件,是这样实现的

'*************************************************************************
'**函 数 名:UserControl_MouseMove
'**输 入:
'**输 出:无
'**功能描述:设置鼠标移动事件,鼠标离开事件
'**全局变量:
'**调用模块:
'**作 者:
'**日 期:
'**修 改 人:
'**日 期:
'**版 本:版本1.0
'*************************************************************************
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X >= 0 And X <= UserControl.Width And Y >= 0 And Y <= UserControl.Height Then
RaiseEvent MouseMove(Button, Shift, X, Y)
Call SetCapture(UserControl.hwnd) '捕获鼠标
Else
RaiseEvent MouseLeave
Call ReleaseCapture '释放鼠标
End If
End Sub
lingll 2003-04-06
  • 打赏
  • 举报
回复
无窗口控件的hwnd=0,所以setcapture无效

我有个办法,
用UserControl.ContainerHwnd获得装载UserControl1的容器hwnd
用UserControl.Extender.left,UserControl.Extender.top获得,UserControl1所在位置,
用GetCursorPos获得鼠标当前坐标(屏幕),
用ScreenToClient获得鼠标在容器的坐标
在timer控件的timer事件中判断鼠标是否在UserControl1里
一元7毛6 2003-04-06
  • 打赏
  • 举报
回复
JennyVenus() :
打不开该地址,发个到我邮箱中吧!
grass57735@sina.com
一元7毛6 2003-04-06
  • 打赏
  • 举报
回复
还有关于透明Usercontrol的问题,欢迎光临!
http://expert.csdn.net/Expert/topic/1626/1626484.xml?temp=.151745
用户 昵称 2003-04-06
  • 打赏
  • 举报
回复
http://zyl910vb.51.net/vb/gui/MouseLeave.htm

右击连接,目标另存为
注意把下载后的*.zip.jpg改名成*.zip
一元7毛6 2003-04-06
  • 打赏
  • 举报
回复
为了感谢大家的支持,我将代码帖出来:
Private Sub timDetectPos_Timer()
Dim hwndContainer As Long
Dim posCurrent As POINTAPI
Dim posContainer As POINTAPI
Dim R As RECT
Dim lRet As Long

'获得鼠标当前坐标(屏幕)
GetCursorPos posCurrent
'获得鼠标在容器的坐标
hwndContainer = UserControl.ContainerHwnd
lRet = ClientToScreen(hwndContainer, posContainer)
With R
.Left = UserControl.Extender.Left / Screen.TwipsPerPixelX + posContainer.x
.Top = UserControl.Extender.Top / Screen.TwipsPerPixelY + posContainer.y
.Right = .Left + UserControl.Width / Screen.TwipsPerPixelX
.Bottom = .Top + UserControl.Height / Screen.TwipsPerPixelY
End With

If PtInRect(R, posCurrent.x, posCurrent.y) = 0 Then
debug.print "NotIn"
Else
debug.print "IsIn"
End If

End Sub
一元7毛6 2003-04-06
  • 打赏
  • 举报
回复
lingll(凌零羚):
问题基本上解决了,谢谢!
smalle 2003-04-06
  • 打赏
  • 举报
回复
用PictureBox嘛,形状你可以自己用API:SetWindowRect()设置
suntt 2003-04-06
  • 打赏
  • 举报
回复
up
Intelement 2003-04-06
  • 打赏
  • 举报
回复
用Text代替Label试试
一元7毛6 2003-04-06
  • 打赏
  • 举报
回复
Intelement(智能元素):用Text代替Label试试
--------------------------------------------
我要做的控件是透明的呀,Text能行吗?

smalle(锋) :用PictureBox嘛,形状你可以自己用API:SetWindowRect()设置
--------------------------------------------
将Picture本身也是不透明的,到是可以用API将PictureBox搞成透明,那么和一个透明的"空"UserControl有什么区别?我个人感觉是不是绕了一个圈又回到了问题本身.

lingll(凌零羚):
--------------------------------------------
想想看,可能应该没有问题,我先试试看.
一元7毛6 2003-04-06
  • 打赏
  • 举报
回复
在UserControl中,我如何对UserContro.ParentContol的MouseMove事件进行处理呀?
我将我的最终目的说出来算了!简单的讲:
我打算做一个透明控件,跟Label差不多。当MoveMove时ForColor=rgb(255,0,0),当MoveOut时ForColor=rgb(0,255,0)。

代码:
UserContol中:
UserContol.BackStyle=0
UserContol.WindowLess=True

Private Sub UserControl_Paint()
UserControl.Cls
Print "Caption"
End Sub

问题来了:
一、无窗口控件的MouseMove事件本身就不会触发(即Usercontrol_Mousemove),只有
UserControl_HitTest事件。
二、在UserControl_HitTest中SetCapture无效。
Private Sub UserControl_HitTest(X As Single, Y As Single, HitResult As Integer)
Dim MouseOver As Boolean

MouseOver = (0 <= X) And (X <= UserControl.Width) And (0 <= Y) And (Y <= UserControl.Height)
If MouseOver = False Then Stop
If MouseOver Then
SetCapture UserControl.hwnd
UserControl.ForeColor = RGB(255, 0, 255)
Else
UserControl.ForeColor = RGB(0, 255, 0)
ReleaseCapture
End If
End Sub
在上面的代码中,MouseOver 始终=True。

有什么办法可以解决如上问题,或达到目的?
lingll 2003-04-06
  • 打赏
  • 举报
回复
没有hwnd的,一定有parent
如果parent1是label1的parent
那么parent1的mousemove事件,就是label1的mouseout了

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧