擴充現有的控制項
如果您想要將更多功能新增至現有的控制項,您可建立繼承自現有控制項的控制項。 新控制項包含基底控制項的所有功能和視覺層面,但您有機會擴充它。 例如,如果您建立了繼承 Button 的控制項,則新控制項的外觀和行為與按鈕完全相同。 您可以建立新的方法和屬性來自訂控制項的行為。 某些控制項可讓您覆寫 OnPaint 方法,以變更控制項的外觀。
將自訂控制項新增至專案
建立新專案之後,請使用 Visual Studio 範本來建立使用者控制項。 下列步驟示範如何將使用者控制項新增至您的專案:
在 Visual Studio 中,尋找 [專案總管] 窗格。 以滑鼠右鍵按一下專案,然後選擇 [新增]>[類別]。
在 [名稱] 方塊中,輸入使用者控制項的名稱。 Visual Studio 提供您可使用的預設和唯一名稱。 接下來,按 [新增]。
建立使用者控制項之後,Visual Studio 會開啟控制項的程式碼編輯器。 下一個步驟是將此自訂控制項轉換成按鈕並加以擴充。
將自訂控制項變更為按鈕
在本節中,您可了解如何將自訂控制項變更為計數並顯示按下次數的按鈕。
將自訂控制項新增至專案 (名為 CustomControl1
) 之後,應該會開啟控制項設計工具。 如果沒有,請按兩下 [方案總管] 中的控制項。 請遵循下列步驟,將自訂控制項轉換成繼承自 Button
的控制項並加以擴充:
開啟控制項設計工具後,按 F7 或以滑鼠右鍵按一下設計工具視窗,然後選取 [檢視程式碼]。
在程式碼編輯器中,您應該會看到類別定義:
namespace CustomControlProject { public partial class CustomControl2 : Control { public CustomControl2() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); } } }
Public Class CustomControl2 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e) 'Add your custom paint code here End Sub End Class
將基底類別從
Control
變更為Button
。重要
如果您使用 Visual Basic,則基底類別會定義於控制項的 *.designer.vb 檔案中。 要在 Visual Basic 中使用的基底類別為
System.Windows.Forms.Button
。新增名為
_counter
的類別範圍變數。private int _counter = 0;
Private _counter As Integer = 0
覆寫
OnPaint
方法。 這個方法可繪製控制項。 控制項應該在按鈕上面繪製字串,因此您必須先呼叫基底類別的OnPaint
方法,然後繪製字串。protected override void OnPaint(PaintEventArgs pe) { // Draw the control base.OnPaint(pe); // Paint our string on top of it pe.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, new PointF(3, 3)); }
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) ' Draw the control MyBase.OnPaint(e) ' Paint our string on top of it e.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, New PointF(3, 3)) End Sub
最後,覆寫
OnClick
方法。 每次按下控制項時,都會呼叫這個方法。 程式碼即將使計數器增加,然後呼叫Invalidate
方法,以強迫控制項重新繪製本身。protected override void OnClick(EventArgs e) { // Increase the counter and redraw the control _counter++; Invalidate(); // Call the base method to invoke the Click event base.OnClick(e); }
Protected Overrides Sub OnClick(e As EventArgs) ' Increase the counter and redraw the control _counter += 1 Invalidate() ' Call the base method to invoke the Click event MyBase.OnClick(e) End Sub
最終程式碼看起來應類似以下程式碼片段:
public partial class CustomControl1 : Button { private int _counter = 0; public CustomControl1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { // Draw the control base.OnPaint(pe); // Paint our string on top of it pe.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, new PointF(3, 3)); } protected override void OnClick(EventArgs e) { // Increase the counter and redraw the control _counter++; Invalidate(); // Call the base method to invoke the Click event base.OnClick(e); } }
Public Class CustomControl1 Private _counter As Integer = 0 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) ' Draw the control MyBase.OnPaint(e) ' Paint our string on top of it e.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, New PointF(3, 3)) End Sub Protected Overrides Sub OnClick(e As EventArgs) ' Increase the counter and redraw the control _counter += 1 Invalidate() ' Call the base method to invoke the Click event MyBase.OnClick(e) End Sub End Class
現在已建立控制項,請編譯專案以使用新控制項填入 [工具箱] 視窗。 開啟表單設計工具,並將控制項拖曳至表單。 執行專案,然後按下按鈕。 每次按下都會使點擊次數增加一次。 總點擊數會列印為按鈕上面的文字。