我的目的是在表单应用程序中每秒更改绘制线渐变。但它不起作用。值得反击的是“标签的变化,但不会改变形式的油漆..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter = 1;
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
counter = 0;
timer1.Tick += new EventHandler(timer1_Tick);
counter = new int();
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Crimson),200,200,counter ,300) ;
}
}
}
我打算随着时间改变我的绘图渐变,但是当它的
时变量没有改变来形成油漆...但它确实在lbl中发生变化......
如果你们能帮助我,请帮助我。不知道该怎么办答案 0 :(得分:1)
在这里,这个有效。答案是在每个计时器滴答时调用this.Invalidate()。
public partial class Form1 : Form
{
int counter = 0;
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
counter = 1;
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Crimson), 200, 200, counter*10, 300);
}
}
还改变了几件事:
答案 1 :(得分:0)
我会建议以下:
Panel
控制绘图及其绘画事件,例如Panel_Paint
Timer_Tick
中使用Panel.Invalidate();
Pen
的图形对象。在表单中添加名为panel1
的Panel控件。
将所有其他控件保留在面板之外。
Panel Paint和Timer事件的示例:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Brushes.Crimson))
{
e.Graphics.DrawLine(pen, 200, 200, counter, 300);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
panel1.Invalidate();
}