簡體   English   中英

如何繪制延遲線?

[英]How to Draw Lines with delay?

如何在畫框中畫線時增加時間延遲? 我正在使用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);
}

在圖形表單上使用計時器。 准備繪制時,啟用計時器並開始跟蹤需要繪制的各種線條(例如,在列表/數組中)。 每次計時器觸發時,在計時器的回調函數中繪制1條線,並增加您的“行索引”(下一條要繪制的線)。 畫完所有線條后,禁用計時器。

例如:

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.
    }
}

您可以使用簡單的Timer( 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);
         }
    }
}

從頭寫下來的,未經測試。

其他答案中使用計時器添加暫停的建議是正確的,但是如果您希望緩慢顯示單條線的圖形,則還需要執行更多操作。

您可以編寫自己的線描方法,然后將線描分為若干段,然后在各段之間暫停。

一個快速的替代方法是使用WPF代替WinForms:

  • 將線添加到Canvas中,使其起點和終點在同一位置。
  • 將動畫添加到端點,將其移動到所需位置
  • 完成后,對下一行執行相同的操作。

這樣,您不必編寫線條圖代碼或計時器。

使用System.Threading;

了Thread.sleep(2000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM