定时器启动事件
Timer start event
我最近开始 "learning" 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;
}
设计器中生成的 InitializeComponent 方法是什么Form1.Designer.cs?
定时器的事件处理程序在那里吗?
//
// 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);
你应该在 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();
}
}
这是我使用的计时器的属性,反正我没有 timer.Start(),这只是您的代码,将 Enabled 属性 设置为 true,间隔为 1000。[我注意到当我复制时没有任何反应,但是当我将 Enabled 设置为 true 时它开始出现。
我最近开始 "learning" 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;
}
设计器中生成的 InitializeComponent 方法是什么Form1.Designer.cs?
定时器的事件处理程序在那里吗?
//
// 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);
你应该在 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();
}
}
这是我使用的计时器的属性,反正我没有 timer.Start(),这只是您的代码,将 Enabled 属性 设置为 true,间隔为 1000。[我注意到当我复制时没有任何反应,但是当我将 Enabled 设置为 true 时它开始出现。