如何将鼠标光标设置到标签上的指定位置?

How to set the mouse cursor to a specified location on a label?

所以我在 visual basic 6.0 中开发了这个游戏,这是一个迷宫游戏,我希望我的鼠标光标设置在带有迷宫的表单的开始标签上,一旦表单被激活并获得焦点!

Dim label1 As New label=Start

我过去使用 Windows API 完成过类似的任务。在下面的示例中,一个表单包含一个名为 'Label1' 的标签,该标签位于该表单的某处。当表格被激活时,光标将以 'Label1':

为中心
Option Explicit

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Sub Form_Activate()
    Dim wr As RECT
    Dim tb As Long
    Dim le As Long
    Dim x As Long
    Dim y As Long
    
    'calculate coordinates
    Call GetWindowRect(Me.hwnd, wr)                                    'window coordinates
    tb = (Me.Height - Me.ScaleHeight) - (Me.Width - Me.ScaleWidth) / 2 'title bar height
    le = (Me.Width - Me.ScaleWidth) * 0.5                              'left edge of client area
    
    'calculate center of label
    x = wr.Left + ScaleX(le + Label1.Left + Label1.Width * 0.5, Me.ScaleMode, vbPixels)
    y = wr.Top + ScaleY(tb + Label1.Top + Label1.Height * 0.5, Me.ScaleMode, vbPixels)
    SetCursorPos x, y
End Sub