[英]Show a picture on my form, a certain time after the Load event.
I want to make a picture appear on my form, a certain number of seconds after it loads, and then have that picture move in a controlled manner within the form boundaries. 我希望在我的表单上显示一张图片,加载后一定秒数,然后让图片在表单边界内以受控方式移动。 I'd appreciate a code example that will get me started with timed events.
我很感激一个代码示例,它将让我开始定时事件。
public partial class Form1 : Form
{
int horiz, vert, step;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
//image is moved at each interval of the timer
goblin.Left = goblin.Left + (horiz * step);
goblin.Top = goblin.Top + (vert * step);
// if goblin has hit the RHS edge, if so change direction left
if ((goblin.Left + goblin.Width) >= (Form1.ActiveForm.Width - step))
horiz = -1;
// if goblin has hit the LHS edge, if so change direction right
if (goblin.Left <= step)
horiz = 1;
// if goblin has hit the bottom edge, if so change direction upwards
if ((goblin.Top + goblin.Height) >= (Form1.ActiveForm.Height - step))
vert = -1;
// if goblin has hit the top edge, if so change direction downwards
if (goblin.Top < step)
vert = 1;
}
private void Form1_Load_1(object sender, EventArgs e)
{
//Soon as the forms loads activate the goblin to start moving
//set the intial direction
horiz = 1; //start going right
vert = 1; //start going down
step = 5; //moves goblin 5 pixels
timer1.Enabled = true;
}
}
}
The easiest solution based on what you've shown us so far is to use the same timer you are using already and essentially skip a few ticks. 基于您到目前为止所展示的内容,最简单的解决方案是使用您已经使用的相同计时器,并且基本上跳过几个滴答。 Let's assume that your current timer is happening at
100ms
which is 10 timers per second (10hz) 假设您当前的计时器发生在
100ms
,即每秒10个计时器(10赫兹)
If you want to delay this activity by 5 seconds, you need to skip 5 * 10
(50) of the first ticks. 如果您想将此活动延迟5秒,则需要跳过第一个滴答的
5 * 10
(50)。 Create a new integer member variable to store how many ticks you've processed: 创建一个新的整数成员变量来存储您处理的滴答数:
private int ticks = 0;
Each time the timer expires/ticks do this first: 每次计时器到期/滴答时首先执行此操作:
ticks++;
if (ticks < 50) {
// Don't do anything just skip
return;
}
You can provide second 'temporary' timer (timer2) 你可以提供第二个'临时'计时器(timer2)
private void Form1_Load_1(object sender, EventArgs e)
{
//Soon as the forms loads activate the goblin to start moving
//set the intial direction
horiz = 1; //start going right
vert = 1; //start going down
step = 5; //moves goblin 5 pixels
timer1.Tick += timer1_Tick_1;
// temporary timer
Timer timer2 = new Timer();
timer2.Interval = 5000;
timer2.Tick += delegate
{
// activate goblin timer
timer1.Enabled = true;
// deactivate 5s temp timer
timer2.Enabled = false;
timer2.Dispose();
};
timer2.Enabled = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.