0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TimerでWinFormを揺らしてみた

Posted at

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        private Timer timer;
        private int count = 0;
        private int count_max = 10;
        private int orgX;
        private int orgY;
        public Form1()
        {
            InitializeComponent();
            this.Move += (s, e) => {
                if (!timer.Enabled)
                {
                    orgX = this.Location.X;
                    orgY = this.Location.Y;
                }
            };
            timer = new Timer();
            timer.Tick += (s,e)=> {
                Point p =Location;
                if((count&1)==0)
                {
                    p.Offset(2, 2);
                }
                else
                {
                    p.Offset(-2, -2);
                }
                Location = p;
                count++;
                if(count>=count_max)
                {
                    count = 0;
                    timer.Stop();
                    Location = new Point(orgX, orgY);
                }
            };
            timer.Interval = 25;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(!timer.Enabled)
            {
                timer.Start();
            }
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?