在Timer1_Timer事件中写入 Private Sub Timer1_Timer() text1.left=Text1.Left+10 text1.Top=Text1.Top+10 End Sub如需控制移动的时间,可以在窗口中另外放置Text等输入控件,可输入移动时间,在txtTime_Change事件中写入Timer1.Interval = txtTime.Text即可
public xMove as integer public yMove as integer Private Sub Timer1_Timer() text1.left=Text1.Left+xMove text1.Top=Text1.Top+yMove End Sub 控制xMove和yMove就能控制控件移动速度
首先添加一个timer控件,控件Interval属性自己定(也影响控件的移动速度)。 定义一个全局变量并赋值,这两个是向X.Y移动的速度控制: public imageX,imageY as Long imageX=10 imageY=5 然后timer里的代码:Private Sub Timer1_Timer() Image1.Move Image1.Left + imageX, Image1.Left + imageY End Sub 具体让timer启动和停止的操作,要自己来设定。
Private Sub Form_Initialize() '设定image1开始位置在窗体最右边 image1.left=scalewidth End Sub在窗体上加一按钮控件command1 Private Sub command1_click() Image1.Move Image1.Left - 100 '控制Image循环移动 if image1.left< -image1.width then image1.left=scalewidth end if End Sub主要是两个属性改变: timer里的Index属性为0,Intervar属性为1000,(1000表示1秒做一次变动,这个值越小变动越快,你自己可以设为100,50,30,1500都行)。Image1.Move Image1.Left - 100 “100”可以作变动,值越大移动越快,(可以设为50,30,200都行)
要控制移动的速度,调节Timer1.interval的值就行。如果是一个固定的值,则在设计就可手工给定。如想给定一个不确定的值,在代码中给定:如 dim i as integer for i =1 to 100 Timer1.interval = i*5 next i '注意:Timer1.interval有规定不能大于65000的,超出会出错的。复杂一点的方法是取系统时间,计算两者之差再移动,想要什么速度就做什么样的速度。。
可是我试过了改Interval的值,只能把初始时间变长,并不能把图片在移动过程中的速度变慢啊! 我的代码如下: Private Sub Command1_Click() Image1.Left = 120 Image1.Top = 720 Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 1000 Timer1.Enabled = False End SubPrivate Sub Timer1_Timer() While Image1.Left < 3800 Image1.Left = Image1.Left + 1 Wend While Image1.Top < 2200 Image1.Top = Image1.Top + 1 Wend End Sub
{
Image1->Canvas->Ellipse(0,0,20,20);
Image1->Left = Image1->Left +5;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Timer1->Interval = StrToInt(Edit1->Text);
}
//---------------------------------------------------------------------------
Private Sub Timer1_Timer()
text1.left=Text1.Left+10
text1.Top=Text1.Top+10
End Sub如需控制移动的时间,可以在窗口中另外放置Text等输入控件,可输入移动时间,在txtTime_Change事件中写入Timer1.Interval = txtTime.Text即可
但思路是一样的,
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Image1->Canvas->Ellipse(0,0,20,20); //image上画圆
Image1->Left = Image1->Left +5; //移动image
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Timer1->Interval = StrToInt(Edit1->Text); //改变timer的值
}
public yMove as integer
Private Sub Timer1_Timer()
text1.left=Text1.Left+xMove
text1.Top=Text1.Top+yMove
End Sub
控制xMove和yMove就能控制控件移动速度
定义一个全局变量并赋值,这两个是向X.Y移动的速度控制:
public imageX,imageY as Long
imageX=10
imageY=5
然后timer里的代码:Private Sub Timer1_Timer() Image1.Move Image1.Left + imageX, Image1.Left + imageY
End Sub
具体让timer启动和停止的操作,要自己来设定。
'设定image1开始位置在窗体最右边
image1.left=scalewidth
End Sub在窗体上加一按钮控件command1
Private Sub command1_click()
Image1.Move Image1.Left - 100
'控制Image循环移动
if image1.left< -image1.width then
image1.left=scalewidth
end if
End Sub主要是两个属性改变:
timer里的Index属性为0,Intervar属性为1000,(1000表示1秒做一次变动,这个值越小变动越快,你自己可以设为100,50,30,1500都行)。Image1.Move Image1.Left - 100
“100”可以作变动,值越大移动越快,(可以设为50,30,200都行)
如果这样你觉得不好,可以用进程条控制,(Solider控件)。
dim i as integer
for i =1 to 100
Timer1.interval = i*5
next i
'注意:Timer1.interval有规定不能大于65000的,超出会出错的。复杂一点的方法是取系统时间,计算两者之差再移动,想要什么速度就做什么样的速度。。
我的代码如下:
Private Sub Command1_Click()
Image1.Left = 120
Image1.Top = 720
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = False
End SubPrivate Sub Timer1_Timer()
While Image1.Left < 3800
Image1.Left = Image1.Left + 1
Wend
While Image1.Top < 2200
Image1.Top = Image1.Top + 1
Wend
End Sub