
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
for (int i = 0; i <= 360; i += 10)
{
// 1. 將繪圖原點平移至窗體中心
g.TranslateTransform(this.Width / 2, this.Height / 2);
// 2. 旋轉坐標系
g.RotateTransform(i);
// 3. 繪製文字(相對新原點 0,0)
using (Brush brush = Brushes.DarkViolet)
{
g.DrawString(".bo ke yuan ", new Font("Lucida Console", 11f), brush, 0, 0);
}
// 4. 重置變換,供下一次迭代
g.ResetTransform();
}
}
| 類 | 主要作用 |
|---|---|
Graphics | 繪圖畫布,提供 DrawString、FillRectangle、Transform 等方法 |
Font | 封裝字體家族、大小、樣式 |
Brush | 定義填充樣式(純色、漸變、紋理) |
本例使用 DrawString(string, Font, Brush, float x, float y),坐標相對當前坐標系原點。其他常用重載支持 PointF、RectangleF、StringFormat 等。
ScaleTransform 實現螺旋放大/縮小動畫。OnPaint 外部使用 Timer 驅動角度增量,構建持續旋轉動畫。StringFormat 設置對齊方式,讓文字繞中心對稱分布。