擴充現有的控件
如果您想要將更多功能新增至現有的控制項,您可以建立繼承自現有控制項的控制項。 新控件包含基底控件的所有功能和視覺層面,但可讓您擴充它。 例如,如果您建立繼承 的 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
首先,新增名為
_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
現在已建立控件,請編譯專案,以使用新的控件填入 [工具箱 ] 視窗。 開啟表單設計工具,並將控件拖曳至表單。 當您執行專案並按下按鈕時,您會看到它會計算點擊次數,並繪製按鈕頂端的文字。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應