同样的代码,在不同的窗体中就是呈现不同的结果
如下代码中:
form2中的label1显示为:鼠标位置:171,121 (当然数字是随鼠标的移动而变化的)
form1中的label1显示为:label1 (也就是说没有变化)问题是:form2中的label1随鼠标的移动而变化caption,为什么form1中的label1的caption却没有反应?
form2 和 form1 中的代码是一样的,两个窗体的控件也是一样的,控件的属性也是一样的,也都没有默认的引用或调用部件,
● 为什么运行的结果不同?
● 到底问题在哪里?
● 什么样的解决思路才有效?要求达到目的是: 同样的代码复制到哪一个窗体中运行都应该是一样的结果(label1随鼠标的移动而变化caption)代码如下:(form2 和 form1 代码完全相同)Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End TypePrivate Sub Timer2_Timer()
Dim P1 As POINTAPI
GetCursorPos P1
Label1.Caption = "鼠标位置: " & P1.x & "," & P1.y
End SubPrivate Sub Form_Load()
Me.WindowState = vbMaximized
Me.Top = (Screen.Height - Me.Height) / 2
Me.Left = (Screen.Width - Me.Width) / 2Timer1.Interval = 1000
Timer1.Enabled = False
End Sub
如下代码中:
form2中的label1显示为:鼠标位置:171,121 (当然数字是随鼠标的移动而变化的)
form1中的label1显示为:label1 (也就是说没有变化)问题是:form2中的label1随鼠标的移动而变化caption,为什么form1中的label1的caption却没有反应?
form2 和 form1 中的代码是一样的,两个窗体的控件也是一样的,控件的属性也是一样的,也都没有默认的引用或调用部件,
● 为什么运行的结果不同?
● 到底问题在哪里?
● 什么样的解决思路才有效?要求达到目的是: 同样的代码复制到哪一个窗体中运行都应该是一样的结果(label1随鼠标的移动而变化caption)代码如下:(form2 和 form1 代码完全相同)Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End TypePrivate Sub Timer2_Timer()
Dim P1 As POINTAPI
GetCursorPos P1
Label1.Caption = "鼠标位置: " & P1.x & "," & P1.y
End SubPrivate Sub Form_Load()
Me.WindowState = vbMaximized
Me.Top = (Screen.Height - Me.Height) / 2
Me.Left = (Screen.Width - Me.Width) / 2Timer1.Interval = 1000
Timer1.Enabled = False
End Sub
绝对没错的,timer放多几个都行的
我都测试成功了的,问题是为什么同样的代码放在不同的窗体就会有不同的情况
要不怎么可能出现这种情况呢
设置form1中的timer2.Interval=500再试
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End TypePrivate Sub Timer2_Timer()
Dim P1 As POINTAPI
GetCursorPos P1
Label1.Caption = "鼠标位置: " & P1.x & "," & P1.y
End Sub
Private Sub Form_Paint() ''''★★★窗体自动最大化
Me.WindowState = vbMaximized
End Sub
Private Sub Form_Load()
Form1.Top = (Screen.Height - Form1.Height) / 2 '模拟鼠标点击必须使窗体居中显示 或最大化显示
Form1.Left = (Screen.Width - Form1.Width) / 2 '模拟鼠标点击必须使窗体居中显示 或最大化显示
End Sub
但将代码复制到form2就不行了,其他控件属性全部一样