首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何画延时的线条?

如何画延时的线条?
EN

Stack Overflow用户
提问于 2013-06-14 13:20:39
回答 4查看 3.2K关注 0票数 1

在picturebox中画线时如何添加延时?我使用的是C #,visual studio 2010。

代码语言:javascript
复制
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);
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-06-14 13:29:57

在绘图表单上使用计时器。当您准备好绘制时,启用计时器并开始跟踪您需要绘制的各种线(例如,在列表/数组中)。每次定时器触发时,在定时器的回调函数中绘制一行,并递增您的“行索引”(接下来绘制哪条行)。绘制完所有线条后,禁用计时器。

例如:

代码语言:javascript
复制
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.
    }
}
票数 3
EN

Stack Overflow用户

发布于 2013-06-14 13:33:09

您可以使用一个简单的计时器(System.Windows.Forms.Timer)来跟踪当前行索引。

代码语言:javascript
复制
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写的,没有经过测试。

票数 2
EN

Stack Overflow用户

发布于 2013-06-14 14:07:11

其他答案中使用计时器添加暂停的建议是正确的,但如果您希望单线的绘制也显示得很慢,您需要做更多的工作。

您可以编写自己的线条绘制方法,并将线条的绘制拆分为线段,然后在线段之间暂停。

一种快速的替代方法是使用WPF而不是WinForms:

  • 将线条添加到画布,使其起点和终点在同一位置。
  • 将动画添加到端点,该动画会将其移动到所需的位置
  • 完成后,对下一条线执行相同的操作。

通过这种方式,您不必编写线条绘制代码或计时器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17101570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档