简体   繁体   English

在Load事件发生后的某个时间,在我的表单上显示图片。

[英]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.

相关问题 在MahApps控件中,在加载表单时会触发验证事件,我需要在单击按钮后显示验证消息 - In MahApps controls Validation Event raises at the time of form Load I need to show Validation Messages after Button click 加载表格然后在定义的时间段后显示一条消息 - Load form then show a message after defined period of time 在我的 dll 中,我通过事件公开日志消息。 如何使用日志消息正确更新表单上的列表框? - In my dll, I expose logging msgs by an event. How can I properly update a listbox on my form with log msgs? 每次触发Form.Load事件 - Form.Load event triggered every time 如何使表单在打开表单时加载图片框中的随机图像,使按钮在图片框中显示不同的图像 - how to make a form load random image in the picture box upon opening the form,make a button show a different image in the picture box 在一定时间后未到达文件时发出事件 - Make an event when a file as not arrived after a certain time 一定时间后如何触发TreeView控件的事件? - How to trigger an event of TreeView control after a certain time? 如何在表单加载事件中显示等待的Gif图像 - How to show waiting Gif image during form load event NavigationCompleted 事件后如何停止 Webview2 重新加载网站。 VB.NET C# - How to stop Webview2 reloading website after NavigationCompleted event. VB.NET C# C# 到 VB 转换后表单加载事件未触发 - Form load event not firing after C# to VB conversion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM