我想在C#Windows窗體中使用System.Drawing.Graphics
繪製一個矩形,但我似乎無法讓它在沒有使用按鈕單擊事件的情況下工作。繪製與System.Drawing.Graphics窗體上加載
在線搜索發現我必須使用表單內的Paint或Shown事件,但是我的嘗試失敗了。
我想在加載表單及其組件時運行我的Draw()
方法。
public Form1()
{
InitializeComponent();
Draw(); //doesn't work
}
private void Draw()
{
Graphics g = pictureBox.CreateGraphics();
g.Clear(Color.White);
Pen p = new Pen(Color.Black, 1);
g.DrawRectangle(p, 0, 0, 50, 50);
}
private void ApproximateButton_Click(object sender, EventArgs e)
{
Draw(); //works
}
實現此目的的正確方法是什麼?
不要忘了'.dispose()'你的''''''''''否則你會最終崩潰你的應用程序。 – Enigmativity
@Ivan Spajic,考慮我的答案編輯。 – 4D1C70