HOW TO:使用 .NET Framework 繪製圖案

下列程式碼範例會使用 Graphics 類別 (Class),修改 OnPaint 事件處理常式,為主要表單擷取指向 Graphics 物件的指標。接著會使用這個指標設定表單的背景色彩,並使用 Graphics.DrawLineDrawArc 方法繪製線條和弧形。

注意事項注意事項

GDI+ 內含在 Windows XP 中,而在 Windows NT 4.0 SP 6、Windows 2000、Windows 98 和 Windows Me 上則是以可轉散發檔案的方式提供。若要下載最新的可轉散發套件,請參閱 https://go.microsoft.com/fwlink/?linkid=11232。如需詳細資訊,請參閱 GDI+

範例

#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
// ...
protected: 
virtual Void Form1::OnPaint(PaintEventArgs^ pe ) override
{
   Graphics^ g = pe->Graphics;
   g->Clear(Color::AntiqueWhite);

   Rectangle rect = Form::ClientRectangle;
   Rectangle smallRect;
   smallRect.X = rect.X + rect.Width / 4;
   smallRect.Y = rect.Y + rect.Height / 4;
   smallRect.Width = rect.Width / 2;
   smallRect.Height = rect.Height / 2;

   Pen^ redPen = gcnew Pen(Color::Red);
   redPen->Width = 4;
   g->DrawLine(redPen, 0, 0, rect.Width, rect.Height);

   Pen^ bluePen = gcnew Pen(Color::Blue);
   bluePen->Width = 10;
   g->DrawArc( bluePen, smallRect, 90, 270 );
}

請參閱

參考

System::Drawing 命名空間

其他資源

.NET 程式設計的 Visual C++