共用方式為


擴充現有的控制項

如果您想要將更多功能新增至現有的控制項,您可建立繼承自現有控制項的控制項。 新控制項包含基底控制項的所有功能和視覺層面,但您有機會擴充它。 例如,如果您建立了繼承 Button 的控制項,則新控制項的外觀和行為與按鈕完全相同。 您可以建立新的方法和屬性來自訂控制項的行為。 某些控制項可讓您覆寫 OnPaint 方法,以變更控制項的外觀。

將自訂控制項新增至專案

建立新專案之後,請使用 Visual Studio 範本來建立使用者控制項。 下列步驟示範如何將使用者控制項新增至您的專案:

  1. 在 Visual Studio 中,尋找 [專案總管] 窗格。 以滑鼠右鍵按一下專案,然後選擇 [新增]>[類別]

    以滑鼠右鍵按一下 Visual Studio 方案總管,以將使用者控制項新增至 Windows Forms 專案

  2. 在 [名稱] 方塊中,輸入使用者控制項的名稱。 Visual Studio 提供您可使用的預設和唯一名稱。 接下來,按 [新增]

    Visual Studio 中的 [新增項目] 對話方塊 (適用於 Windows Forms)

建立使用者控制項之後,Visual Studio 會開啟控制項的程式碼編輯器。 下一個步驟是將此自訂控制項轉換成按鈕並加以擴充。

將自訂控制項變更為按鈕

在本節中,您可了解如何將自訂控制項變更為計數並顯示按下次數的按鈕。

適用於 .NET 的 Windows Forms 自訂控制項

將自訂控制項新增至專案 (名為 CustomControl1) 之後,應該會開啟控制項設計工具。 如果沒有,請按兩下 [方案總管] 中的控制項。 請遵循下列步驟,將自訂控制項轉換成繼承自 Button 的控制項並加以擴充:

  1. 開啟控制項設計工具後,按 F7 或以滑鼠右鍵按一下設計工具視窗,然後選取 [檢視程式碼]

  2. 在程式碼編輯器中,您應該會看到類別定義:

    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
    
  3. 將基底類別從 Control 變更為 Button

    重要

    如果您使用 Visual Basic,則基底類別會定義於控制項的 *.designer.vb 檔案中。 要在 Visual Basic 中使用的基底類別為 System.Windows.Forms.Button

  4. 新增名為 _counter 的類別範圍變數。

    private int _counter = 0;
    
    Private _counter As Integer = 0
    
  5. 覆寫 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
    
  6. 最後,覆寫 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
    

現在已建立控制項,請編譯專案以使用新控制項填入 [工具箱] 視窗。 開啟表單設計工具,並將控制項拖曳至表單。 執行專案,然後按下按鈕。 每次按下都會使點擊次數增加一次。 總點擊數會列印為按鈕上面的文字。

Windows Forms 的 Visual Studio [工具箱] 視窗會顯示自訂控制項目。