在窗体中建立一个图像框,然后把鼠标隐藏起来。代码如果是这样:
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 ExplicitPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X
Image1.Top = Y
End SubPrivate 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
可以实现一定效果,但是还是不理想。
(鼠标隐藏代码没写)
所以想请问有没有什么方法可以获得鼠标相对于窗体的时时位置,不论鼠标是否在控件上。明白我的意思吗?
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 ExplicitPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X
Image1.Top = Y
End SubPrivate 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
可以实现一定效果,但是还是不理想。
(鼠标隐藏代码没写)
所以想请问有没有什么方法可以获得鼠标相对于窗体的时时位置,不论鼠标是否在控件上。明白我的意思吗?
解决方案 »
- 直线绕一点旋转问题;大家进来看看
- 写richTextBox,如何让输入的内容只有在遇到回车符的时候才进行换行?敬待
- 那位高人知道VB中的CDate与DateValue这二个方法有什么区别。多谢了 ^_^
- 请教:datareport,在打印报表时,如何让一个控件在打印最后一页时不可见
- 关于如何决定当前日期所在的星期。。。急!
- 求手机号码段数据库,不是最新的也可以
- 困惑~~~~~
- 求助:Command控件对treeview控件节点的访问操作出现问题
- Visual Basic 6.0中怎么定义和使用类?只能新建一个Class Module才能定义吗?
- 高分求解:如何直接在接收时保存邮件附件到硬盘上?
- 求关闭EXCEL时 自动保存文档的VBA 谢谢
- vb程序在一台机器上能运行,在另外一台机器就不能运行了
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 LongPrivate Sub Command2_Click()
Print "有没有办法去响应控件(commandbutton)事件?"
End SubPrivate Sub Form_Load()
Me.WindowState = 2
Command2.Enabled = False
End SubPrivate 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
你的代码很有意思:没有command1,command2。而且把command2.enabled=false,当然无法响应控件事件。另外我也不要在使用鼠标时多一个无效控件。(我知道你想论证一些东西才加入的,开个玩笑。)之前我也用过这个API,代码是:Option ExplicitPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPIPrivate 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 ExplicitPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPIPrivate 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 SubPrivate 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就可以实现我想要的效果了。总之,谢谢楼上的回复。
你能解决问题就好!