範例:視窗 308×234,標題列 30、邊框 4 → 工作區 300×200。
若文件 (文本、圖形) 總高度 250px > 工作區 200px,部分內容將被遮蔽。需啟用捲動條。
this.AutoScrollMinSize = new Size(300, 250); // 文件邏輯大小
當 ClientSize 小於 AutoScrollMinSize 時,WinForms 自動顯示捲動條。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.White;
this.AutoScrollMinSize = new Size(300, 250); // 文件大小 300x250
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
// 繪製矩形 (0,0) 200x150
g.FillRectangle(Brushes.LightPink, 0, 0, 200, 150);
// 繪製橢圓 (0,150) 300x100
g.FillEllipse(Brushes.LightGreen, 0, 150, 300, 100);
}
}
AutoScrollMinSize 定義文件邏輯大小。OnPaint 中使用文件座標繪圖,無需手動計算捲動位移。Graphics.TranslateTransform 自動套用捲動位移。