在picturebox中画线时如何添加延时?我使用的是C #,visual studio 2010。
Graphics g = picturebox.CreateGraphics();
Pen p = new Pen(Color.Red);
for (int j = 1; j < 10; j++)
{
//Draw Line 1
g.DrawLine(p,j*3,j*3,100,100);
//----->How to put a Delay for 2 seconds So I
// see the first line then see the second after 2 sec
//Draw Line 2
g.DrawLine(p,j*10,j*10,100,100);
}发布于 2013-06-14 13:29:57
在绘图表单上使用计时器。当您准备好绘制时,启用计时器并开始跟踪您需要绘制的各种线(例如,在列表/数组中)。每次定时器触发时,在定时器的回调函数中绘制一行,并递增您的“行索引”(接下来绘制哪条行)。绘制完所有线条后,禁用计时器。
例如:
public partial class DrawingForm : Form
{
Timer m_oTimer = new Timer ();
public DrawingForm ()
{
InitializeComponent ();
m_oTimer.Tick += new EventHandler ( m_oTimer_Tick );
m_oTimer.Interval = 2000;
m_oTimer.Enabled = false;
}
// Enable the timer and call m_oTimer.Start () when
// you're ready to draw your lines.
void m_oTimer_Tick ( object sender, EventArgs e )
{
// Draw the next line here; disable
// the timer when done with drawing.
}
}发布于 2013-06-14 13:33:09
您可以使用一个简单的计时器(System.Windows.Forms.Timer)来跟踪当前行索引。
public partial class Form1 : Form {
private int index;
private void frmBrowser_Load(object sender, EventArgs e) {
index = 0;
timer.Interval = 2000;
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e) {
index++;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e) {
Pen p = new Pen(Color.Red);
for (int j = 1; j < index; j++) {
g.DrawLine(p,j*3,j*3,100,100);
g.DrawLine(p,j*10,j*10,100,100);
}
}
}这是从head写的,没有经过测试。
发布于 2013-06-14 14:07:11
其他答案中使用计时器添加暂停的建议是正确的,但如果您希望单线的绘制也显示得很慢,您需要做更多的工作。
您可以编写自己的线条绘制方法,并将线条的绘制拆分为线段,然后在线段之间暂停。
一种快速的替代方法是使用WPF而不是WinForms:
通过这种方式,您不必编写线条绘制代码或计时器。
https://stackoverflow.com/questions/17101570
复制相似问题