获取鼠标位置

Excellentwxg 2009-07-06 02:08:43
在窗体中建立一个图像框,然后把鼠标隐藏起来。代码如果是这样:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X
Image1.Top = Y
End Sub
结果图像框充当鼠标,但是当鼠标移动到图像框上时窗体就无法获得鼠标的位置,图像框就不会动了。
如果改成:
Option Explicit

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X
Image1.Top = Y
End Sub

Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = Image1.Left + X
Image1.Top = Image1.Top + Y
End Sub
可以实现一定效果,但是还是不理想。
(鼠标隐藏代码没写)
所以想请问有没有什么方法可以获得鼠标相对于窗体的时时位置,不论鼠标是否在控件上。

明白我的意思吗?
...全文
108 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdingyun 2009-07-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 excellentwxg 的回复:]
楼上的意思我大致明白。
你的代码很有意思:没有command1,command2。而且把command2.enabled=false,当然无法响应控件事件。另外我也不要在使用鼠标时多一个无效控件。(我知道你想论证一些东西才加入的,开个玩笑。)

之前我也用过这个API,代码是:

[/Quote]
LZ:应时间紧随手找了一段响应控件(commandbutton)事件的代码贴上.
你能解决问题就好!
嗷嗷叫的老马 2009-07-07
  • 打赏
  • 举报
回复
搞定就好.

路过.

闪人.
Excellentwxg 2009-07-06
  • 打赏
  • 举报
回复
我是回复#1楼的,没想到写的时候中间跑出一个#2楼来~!
Excellentwxg 2009-07-06
  • 打赏
  • 举报
回复
楼上的意思我大致明白。
你的代码很有意思:没有command1,command2。而且把command2.enabled=false,当然无法响应控件事件。另外我也不要在使用鼠标时多一个无效控件。(我知道你想论证一些东西才加入的,开个玩笑。)

之前我也用过这个API,代码是:

Option Explicit

Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
GetCursorPos MousePos
Image1.Left = MousePos.X
Image1.Top = MousePos.Y
End Sub

结果效果与问题中的第一段代码相同,误以为这个API返回的只是鼠标在窗体时的位置,在控件上不能返回值。刚刚才明白在控件上的时候没有触发该事件,现在把代码改为:

Option Explicit

Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
GetCursorPos MousePos
Image1.Left = MousePos.X
Image1.Top = MousePos.Y
End Sub

Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
GetCursorPos MousePos
Image1.Left = MousePos.X
Image1.Top = MousePos.Y
End Sub

就可以实现我想要的效果了。

总之,谢谢楼上的回复。
贝隆 2009-07-06
  • 打赏
  • 举报
回复
zdingyun 2009-07-06
  • 打赏
  • 举报
回复
使用API可以做到:
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI
Dim NewX As Long
Dim NewY As Long

Private Sub Command2_Click()
Print "有没有办法去响应控件(commandbutton)事件?"
End Sub

Private Sub Form_Load()
Me.WindowState = 2
Command2.Enabled = False
End Sub

Private Sub Timer1_Timer()
GetCursorPos MousePos
NewX = MousePos.X * Screen.TwipsPerPixelX
NewY = MousePos.Y * Screen.TwipsPerPixelY - 300
Label1 = NewX
Label2 = NewY
If NewX >= Command2.Left And NewX <= Command2.Left + Command2.Width And NewY >= Command2.Top And NewY <= Command2.Height + Command2.Top Then
Command2_Click
Else
Command2.Enabled = False
Me.Cls
End If
End Sub

7,763

社区成员

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

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