public static PenMarkInfoDocument Load(string fileName)
{
using (XmlTextReader reader = new XmlTextReader(fileName))
{
XmlSerializer ser = new XmlSerializer(typeof(PenMarkInfoDocument));
return (PenMarkInfoDocument)ser.Deserialize(reader);
}
}
反序列化會建立新物件,無法注入既有實例。此模式也是 Web Service 傳輸基礎:伺服端序列化 → HTTP 傳輸 → 客戶端反序列化。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 捲動座標轉換
e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
Rectangle clip = e.ClipRectangle;
clip.Offset(-AutoScrollPosition.X, -AutoScrollPosition.Y);
var list = new ArrayList(myDocument);
if (Marking) list.Add(myCurrentInfo);
foreach (PenMarkInfo info in list)
info.Draw(e.Graphics, clip);
// 繪製焦點框
if (myCurrentInfo != null)
ControlPaint.DrawFocusRectangle(e.Graphics, myCurrentInfo.Bounds);
}
TranslateTransform 處理捲動座標系。ClipRectangle 限制重繪區域,提升效能。myCurrentInfo 統一迴圈繪製。ControlPaint.DrawFocusRectangle 繪製。