定时器启动事件

时间:2015-11-12 19:37:11

标签: c# forms timer draw

我最近开始了#34;学习" C#。目前我正在为学校项目做一些游戏。我想在表格上画一个圆圈。我添加了一个时间,每隔1000毫秒在一个随机的地方绘制一个新的圆圈。但是,当我开始我的表格时,没有任何事情发生。

命名空间Vezba_4 {

public partial class Form1 : Form
{
  // attempt is when you try to "poke" the circle
    bool attempt = false;
    int xc, yc, Br = 0, Brkr = 0;
    Random R = new Random();
    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }

// Br是玩家成功的圈数" poked"。而Brkr是游戏画面上出现的总圈数。

    private void timer1_Tick(object sender, EventArgs e)
    {
        Refresh();
        SolidBrush cetka = new SolidBrush(Color.Red);
        Graphics g = CreateGraphics();
        xc = R.Next(15, ClientRectangle.Width - 15);
        yc = R.Next(15, ClientRectangle.Height - 15);
        g.FillEllipse(cetka, xc - 15, yc - 15, 30, 30);
        Brkr++;
        Text = Br + "FROM" + Brkr;
        attempt = false;
        g.Dispose();
        cetka.Dispose();

    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (attempt == false)
        {
            if ((e.X - xc) * (e.X - xc) + (e.Y - yc) * (e.Y - yc) <= 225) Br++;
            Text = Br + " FROM " + Brkr++;
        }
        attempt = true;
    }

3 个答案:

答案 0 :(得分:1)

设计器Form1.Designer.cs中生成的InitializeComponent方法是什么?

计时器的事件处理程序是否在那里?

    // 
    // timer1
    // 
    this.timer1.Interval = 1000;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

编辑: 因为mousedown必须确认处理程序在Form.Designer.cs中也是如此:

    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);

答案 1 :(得分:0)

你应该在Form1_Paint中绘制你的圆圈。因为它仅在Paint事件触发时被绘制,并且在触发时,它会查找Form1_Paint。

public partial class Form1 : Form
{
    bool attempt = false;
    int xc, yc, Br = 0, Brkr = 0;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (attempt == false)
        {
            if ((e.X - xc) * (e.X - xc) + (e.Y - yc) * (e.Y - yc) <= 225) Br++;
            Text = Br + " FROM " + Brkr++;
        }
        attempt = true;
    }

    public void Paaint()
    {
        SolidBrush cetka = new SolidBrush(Color.Red);
        Graphics g = CreateGraphics();
        xc = R.Next(15, ClientRectangle.Width - 15);
        yc = R.Next(15, ClientRectangle.Height - 15);
        g.FillEllipse(cetka, xc - 15, yc - 15, 30, 30);
        Brkr++;
        label1.Text = Br + "FROM" + Brkr;
        attempt = false;
        g.Dispose();
        cetka.Dispose();
    }
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Paaint();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Invalidate();
    }

    Random R = new Random();
    public Form1()
    {
        InitializeComponent();
    }
}

答案 2 :(得分:0)

enter image description here

这里是我使用的计时器的属性,我没有计时器.Start()无论如何,它只是你的代码,Enabled属性设置为true,间隔为1000。[我注意到当我没有发生任何事情时,但当我将Enabled设置为true时,它开始出现。