如何:繪製外框形狀
本範例會在表單上繪製外框橢圓形和矩形。
範例
private:
void DrawEllipse()
{
System::Drawing::Pen^ myPen =
gcnew System::Drawing::Pen(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->DrawEllipse(myPen, Rectangle(0, 0, 200, 300));
delete myPen;
delete formGraphics;
}
private:
void DrawRectangle()
{
System::Drawing::Pen^ myPen =
gcnew System::Drawing::Pen(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->DrawRectangle(myPen, Rectangle(0, 0, 200, 300));
delete myPen;
delete formGraphics;
}
private void DrawEllipse()
{
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawEllipse(myPen, new Rectangle(0, 0, 200, 300));
myPen.Dispose();
formGraphics.Dispose();
}
private void DrawRectangle()
{
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawRectangle(myPen, new Rectangle(0, 0, 200, 300));
myPen.Dispose();
formGraphics.Dispose();
}
Private Sub DrawEllipse()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawEllipse(myPen, New Rectangle(0, 0, 200, 300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
Private Sub DrawRectangle()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawRectangle(myPen, New Rectangle(0, 0, 200, 300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
編譯程式碼
您無法在 Load 事件處理常式中呼叫這個方法。 如果表單調整大小或被另一個表單遮蔽,則繪製的內容將不會重新繪製。 若要讓內容自動重繪,您應該覆寫 OnPaint 方法。
穩固程式設計
您應該一律呼叫 Dispose 您所建立的任何物件,以取用系統資源。 在上一個範例中, Pen 會建立 和 Graphics 物件,然後處置。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應