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



所以我在visualbasic6.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

相关内容

  • 没有找到相关文章

最新更新