簽名軌跡點每秒產生數十次,若走 Invalidate→OnPaint 流程會嚴重卡頓。**解法**:呼叫 Control.CreateGraphics() 取得即時 Graphics(相當於 Win32 GetDC),直接在事件處理中繪製線段,**完全繞過訊息佇列與 OnPaint**,極大提升流暢度。
// OnMouseMove 中
if (Marking) {
Point viewPt = new Point(e.X, e.Y);
viewPt.Offset(-AutoScrollPosition.X, -AutoScrollPosition.Y);
currentLine.Add(viewPt); // 加入視圖座標點
using (Graphics g = CreateGraphics()) {
g.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
// 僅繪製最新線段
g.DrawLine(pen, LastPoint, viewPt);
}
LastPoint = viewPt;
}
protected override void OnMouseUp(MouseEventArgs e) {
base.OnMouseUp(e);
if (Marking && currentLine != null) {
Rectangle bounds = currentLine.Bounds;
// 忽略極小軌跡(寬或高 < 5 像素視為誤觸)
if (bounds.Width > 5 || bounds.Height >= 5)
currentInfo.Lines.Add(currentLine);
currentLine = null;
}
}
同邏輯用於 Windows 雙擊判定:**時間間隔 + 位移距離**雙重門檻過濾誤操作。
frmTest 表單,工具箱加入 PenMarkControl。PenMarkLib.dll 交付。Invalidate(Rectangle) 局部失效,ClipRectangle 精準重繪。PointArrayList→PenMarkInfo→PenMarkInfoDocument 三層架構。XmlSerializer 一鍵物件↔XML。CreateGraphics 繞過重繪管線,適用高頻回饋場景。Graphics.FromImage(Bitmap) 離屏渲染,適用任何專案類型。掌握以上技術,已具備開發專業級圖形編輯器的基礎。**下一講將探索 ASP.NET 圖形開發:帶超連結的餅圖**,敬請期待!