我有一个程序随机设置光标x,y坐标,然后单击。我有这个函数/ sub,它根据某些参数创建一个矩形。
Private Sub drawTitleBarRectangle()
Dim titleBarRectangle As Rectangle = RectangleToScreen(Me.ClientRectangle)
Dim titleBarRectangleHeight As Integer = titleBarRectangle.Top - Me.Top
Dim titleBarRectangleWidth As Integer = Screen.PrimaryScreen.Bounds.Width
Dim titleBarRectangleTop As Integer = Screen.PrimaryScreen.Bounds.Top
Dim titleBarBounds As New Drawing.Rectangle(0, 0, titleBarRectangleWidth, titleBarRectangleHeight)
End Sub
我想检查光标是否位于x,y位置,如果它位于从该函数创建的矩形的边界内。现在我有这个:
drawTitleBarRectangle()
SetCursorPos(x, y)
If titleBarRectangle.Contains(x, y) Then
leftClick(800, 800)
End If
Private titleBarRectangle
来自我声明为Private titleBarRectangle As New Drawing.Rectangle
的全局变量我不太清楚为什么说实话......
任何帮助都将不胜感激。
答案 0 :(得分:1)
您列出的初始方法中的所有变量都是本地变量。这意味着当该方法退出时它们被简单地丢弃。您需要通过进行赋值而不是声明来更新已声明的类级变量。考虑到这一点,它看起来应该更像:
Public Class Form1
Private titleBarRectangle As Rectangle
Private Sub drawTitleBarRectangle()
Dim rc As Rectangle = Me.RectangleToScreen(Me.ClientRectangle)
Dim titleBarRectangleHeight As Integer = rc.Top - Me.Top
titleBarRectangle = New Rectangle(Me.Location.X, Me.Location.Y, Me.Width, titleBarRectangleHeight)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
drawTitleBarRectangle()
Debug.Print(titleBarRectangle.ToString)
ControlPaint.DrawReversibleFrame(titleBarRectangle, Color.Black, FrameStyle.Dashed)
Dim x As Integer = titleBarRectangle.Location.X + titleBarRectangle.Width / 2
Dim y As Integer = titleBarRectangle.Location.Y + titleBarRectangle.Height / 2
Cursor.Position = New Point(x, y)
If titleBarRectangle.Contains(Cursor.Position) Then
Debug.Print("It's in there!")
End If
End Sub
End Class
注意方法中的最后一行如何使用类级别变量而不是本地级别,因为我们前面没有Dim
。