计时器不会打勾

时间:2017-05-30 18:09:30

标签: c# winforms visual-studio timer

今天我尝试过Visual Studio + WinForms(我一直在使用Xamarin) 我不使用设计器,只需创建空项目并链接所需的库。 因此,我的计时器无法正常工作,它只能以最小化的形式打勾。

App.cs

using System;
using System.Windows.Forms;

namespace Line
{
    class App
    {
        [STAThread]
        public static void Main()
        {
            Application.Run(new Window());
        }
    }
}

Window.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Line
{
    class Window : Form
    {
        Timer timer;
        PictureBox pictureBox;
        Bitmap bmp;

        public struct Circle
        {
            public PointF position;
            public SizeF size;
        };

        List<Circle> circles;

        public Window()
        {
            this.Text = "Line";
            this.Size = SizeFromClientSize(new Size(640, 480));
            pictureBox = new PictureBox();
            pictureBox.Dock = DockStyle.Fill;
            pictureBox.BackColor = Color.White;
            pictureBox.Paint += new PaintEventHandler(pictureBox_Paint);
            this.Controls.Add(pictureBox);
            this.Load += new EventHandler(Window_Load);
            this.Resize += new EventHandler(Window_Resize);
            circles = new List<Circle>();
            timer = new Timer();
            timer.Interval = 15;
            timer.Enabled = true;
            timer.Tick += new EventHandler(timer_Tick);
        }

        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.Black);
            foreach (Circle c in circles)
            {
                g.DrawEllipse(Pens.White, new RectangleF(c.position, c.size));
            }
            pictureBox.Image = bmp;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Circle c;
            Console.WriteLine("Works");
            for (int i = 0; i < circles.Count; i++)
            {
                c = circles[i];
                c.size.Width = (c.size.Width > 200) ? 100 : c.size.Width + 1;
                circles[i] = c;
            }
            pictureBox.Invalidate();
        }

        private void Window_Load(object sender, EventArgs e)
        {
            bmp = new Bitmap(this.Width, this.Height);
            circles.Add(new Circle());
            Circle c = circles[0];
            c.position = new PointF(100, 100);
            c.size = new SizeF(100, 100);
            circles[0] = c;
        }

        private void Window_Resize(object sender, EventArgs e)
        {
            bmp = new Bitmap(this.Width, this.Height);
            pictureBox.Invalidate();
        }
    }
}

相同的代码在Xamarin中运行良好。

1 个答案:

答案 0 :(得分:1)

1)在enableMarkersDrag() { for (var i=0; i<this.get('markers').length; i++) { this.get('markers')[i].draggable = true; } }, 删除行:timer_Tick
2)从pictureBox.Invalidate();剪切代码并粘贴到pictureBox_Paint的末尾 3)修复变量名称冲突timer_Tick