做一个VB中让label标签在form中上下左右手动移动。

2024-12-17 04:24:08
推荐回答(2个)
回答1:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 38 'Up
If Label1.Top > 100 Then Label1.Top = Label1.Top - 100
Case 37 'Left
If Label1.Left > 100 Then Label1.Left = Label1.Left - 100
Case 39 'Right
If Label1.Left + Label1.Width < Me.ScaleWidth Then Label1.Left = Label1.Left + 100
Case 40 'Down
If Label1.Top + Label1.Height < Me.ScaleHeight Then Label1.Top = Label1.Top + 100
End Select
End Sub

回答2:

Option Explicit
Const x = 1000, y = 1000
Dim a As Integer, b As Integer
Private Sub Form_Activate()
With Label1
.AutoSize = True
.Caption = "累啊"
End With
Me.WindowState = vbMaximized
Timer1.Interval = 50
a = 1: b = 1
End Sub
Private Sub Timer1_Timer()
With Label1
If .Top >= Me.ScaleHeight Then a = -1 * a
If .Left >= Me.ScaleWidth Then b = -1 * b
If .Top <= 0 Then a = -1 * a
If .Left <= 0 Then b = -1 * b
.Top = .Top + y * a
.Left = .Left + x * b
End With
End Sub