vb 根据圆的轨迹运动,vb高手进
- 提问者网友:伴风望海
- 2021-04-15 11:43
- 五星知识达人网友:爱难随人意
- 2021-04-15 12:42
Dim pi As Double
Dim runTime As Double
Dim a As Single, b As Single
Dim cntX As Single, cntY As Single
Dim r As Single
Private WithEvents Timer1 As Timer
Dim tX As Single, tY As Single
Dim tStep As Single
Private Sub Form_Load()
Me.ScaleMode = 3
Me.AutoRedraw = True
pi = Atn(1)
Set Timer1 = Controls.Add("vb.timer", "Timer1")
a = 50 '椭圆长轴
b = 50 '椭圆短轴
cntX = 100 '中心坐标X
cntY = 60 '中心坐标Y
r = 10 '圆半径
Timer1.Interval = 50 '运动间隔(毫秒)
tStep = pi / 10 '角度步长
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
runTime = runTime + 1
Dim Arg As Double
Arg = runTime * tStep
tX = cntX + a * Cos(Arg)
tY = cntY + b * Sin(Arg)
Me.Cls
Me.DrawStyle = 2
Me.Circle (cntX, cntY), a, , , , b / a
Me.DrawStyle = 0
Me.Circle (tX, tY), r
End Sub
- 1楼网友:酒者煙囻
- 2021-04-15 14:18
在窗体上添加一个PictureBox控件,名为Picture1,然后在Picture1上添加一个label控件(是添加到Picture1上,这一点很重要),名为lable1,然后添加两个按钮一个Command1,一个Command2,窗体如图所示
然后添加如下代码
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Const Pi = 3.14 Private Sub Command1_Click() Call CircleRun(100) End Sub Private Function CircleRun(ByVal sudu As Long) Dim x As Single, y As Single, α, r As Single r = 3 Picture1.Scale (-4, 4)-(4, -4) Picture1.ForeColor = RGB(255, 123, 254) Picture1.DrawWidth = 2 y = r * Sin(α) x = r * Cos(α) Label1.Top = y - Label1.Height / 2 Label1.Left = x - Label1.Width / 2
Label1.Visible = True Do While 1 Sleep (sudu) DoEvents y = r * Sin(α) x = r * Cos(α) Label1.Top = y - Label1.Height / 2 Label1.Left = x - Label1.Width / 2 α = α + 0.05 Loop
End Function
Private Sub Command2_Click() End End Sub
Private Sub Form_Load() Label1.Caption = "" Label1.BackColor = vbRed Label1.Width = 200 Label1.Height = 200 Label1.Visible = False End Sub
Private Sub Form_Unload(Cancel As Integer) End End Sub
- 2楼网友:爱难随人意
- 2021-04-15 12:59