vb 根据圆的轨迹运动,vb高手进
vb 先画一个圆,再让一个控件或图片根据圆的轨迹圆周重复运动,或者只要给我个根据轨迹圆运动的代码
追问:只要vb,flash我也会嘿嘿
vb 先画一个圆,再让一个控件或图片根据圆的轨迹圆周重复运动,或者只要给我个根据轨迹圆运动的代码
追问:只要vb,flash我也会嘿嘿
答案: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 SinglePrivate 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 SubPrivate 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如果是动画需要可以用flash实现,需要的话,加我在窗体上添加一个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 / 2Label1.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
LoopEnd Function
Private Sub Command2_Click()
End
End SubPrivate Sub Form_Load()
Label1.Caption = ""
Label1.BackColor = vbRed
Label1.Width = 200
Label1.Height = 200
Label1.Visible = False
End SubPrivate Sub Form_Unload(Cancel As Integer)
End
End Sub