TabControl.DrawItem 事件

定義

如果 TabControl 屬性設定為 DrawMode,在 OwnerDrawFixed 需要繪製它的每一個索引標籤時發生。

public:
 event System::Windows::Forms::DrawItemEventHandler ^ DrawItem;
public event System.Windows.Forms.DrawItemEventHandler DrawItem;
public event System.Windows.Forms.DrawItemEventHandler? DrawItem;
member this.DrawItem : System.Windows.Forms.DrawItemEventHandler 
Public Custom Event DrawItem As DrawItemEventHandler 

事件類型

範例

下列程式碼範例會使用一個 TabControlTabPage 來建立 。 這個範例會宣告事件處理常式,用來繪製字串,並在 RectangletabPage1 索引標籤上。 事件處理常式會系結至 DrawItem 事件。

using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public Form
{
private:
   Rectangle tabArea;
   RectangleF tabTextArea;

public:
   Form1()
   {
      TabControl^ tabControl1 = gcnew TabControl;
      TabPage^ tabPage1 = gcnew TabPage;
      
      // Allows access to the DrawItem event.
      tabControl1->DrawMode = TabDrawMode::OwnerDrawFixed;
      tabControl1->SizeMode = TabSizeMode::Fixed;
      tabControl1->Controls->Add( tabPage1 );
      tabControl1->ItemSize = System::Drawing::Size( 80, 30 );
      tabControl1->Location = Point(25,25);
      tabControl1->Size = System::Drawing::Size( 250, 250 );
      tabPage1->TabIndex = 0;
      ClientSize = System::Drawing::Size( 300, 300 );
      Controls->Add( tabControl1 );
      tabArea = tabControl1->GetTabRect( 0 );
      tabTextArea = tabControl1->GetTabRect( 0 );
      
      // Binds the event handler DrawOnTab to the DrawItem event
      // through the DrawItemEventHandler delegate.
      tabControl1->DrawItem += gcnew DrawItemEventHandler( this, &Form1::DrawOnTab );
   }


private:

   // Declares the event handler DrawOnTab which is a method that
   // draws a String* and Rectangle on the tabPage1 tab.
   void DrawOnTab( Object^ /*sender*/, DrawItemEventArgs^ e )
   {
      Graphics^ g = e->Graphics;
      Pen^ p = gcnew Pen( Color::Blue );
      System::Drawing::Font^ font = gcnew System::Drawing::Font( "Arial",10.0f );
      SolidBrush^ brush = gcnew SolidBrush( Color::Red );
      g->DrawRectangle( p, tabArea );
      g->DrawString( "tabPage1", font, brush, tabTextArea );
   }

};

int main()
{
   Application::Run( gcnew Form1 );
}
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    private Rectangle tabArea;
    private RectangleF tabTextArea;

    public Form1()
    {
        TabControl tabControl1 = new TabControl();
        TabPage tabPage1 = new TabPage();

        // Allows access to the DrawItem event. 
        tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;

        tabControl1.SizeMode = TabSizeMode.Fixed;
        tabControl1.Controls.Add(tabPage1);
        tabControl1.ItemSize = new Size(80, 30);
        tabControl1.Location = new Point(25, 25);
        tabControl1.Size = new Size(250, 250);
        tabPage1.TabIndex = 0;
        ClientSize = new Size(300, 300);
        Controls.Add(tabControl1);

        tabArea = tabControl1.GetTabRect(0);
        tabTextArea = (RectangleF)tabControl1.GetTabRect(0);

        // Binds the event handler DrawOnTab to the DrawItem event 
        // through the DrawItemEventHandler delegate.
        tabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab);
    }

    // Declares the event handler DrawOnTab which is a method that
    // draws a string and Rectangle on the tabPage1 tab.
    private void DrawOnTab(object sender, DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        Pen p = new Pen(Color.Blue);
        Font font = new Font("Arial", 10.0f);
        SolidBrush brush = new SolidBrush(Color.Red);

        g.DrawRectangle(p, tabArea);
        g.DrawString("tabPage1", font, brush, tabTextArea);
    }

    static void Main() 
    {
        Application.Run(new Form1());
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Private tabArea As Rectangle
    Private tabTextArea As RectangleF

    Public Sub New()
        Dim tabControl1 As New TabControl()
        Dim tabPage1 As New TabPage()

        ' Allows access to the DrawItem event. 
        tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed

        tabControl1.SizeMode = TabSizeMode.Fixed
        tabControl1.Controls.Add(tabPage1)
        tabControl1.ItemSize = New Size(80, 30)
        tabControl1.Location = New Point(25, 25)
        tabControl1.Size = New Size(250, 250)
        tabPage1.TabIndex = 0
        ClientSize = New Size(300, 300)
        Controls.Add(tabControl1)

        tabArea = tabControl1.GetTabRect(0)
        tabTextArea = RectangleF.op_Implicit(tabControl1.GetTabRect(0))

        ' Binds the event handler DrawOnTab to the DrawItem event 
        ' through the DrawItemEventHandler delegate.
        AddHandler tabControl1.DrawItem, AddressOf DrawOnTab
    End Sub

    ' Declares the event handler DrawOnTab which is a method that
    ' draws a string and Rectangle on the tabPage1 tab.
    Private Sub DrawOnTab(ByVal sender As Object, ByVal e As DrawItemEventArgs)
        Dim g As Graphics = e.Graphics
        Dim p As New Pen(Color.Blue)
        Dim font As New Font("Arial", 10.0F)
        Dim brush As New SolidBrush(Color.Red)

        g.DrawRectangle(p, tabArea)
        g.DrawString("tabPage1", font, brush, tabTextArea)
    End Sub

    Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class

備註

當您將 DrawMode 屬性設定為 OwnerDrawFixed 時, TabControl 每當需要繪製其中一個索引標籤時,就會引發 DrawItem 事件。 若要自訂索引標籤的外觀,請在 事件的處理常式 DrawItem 中提供您自己的繪製程式碼。

TabControl不支援具有擁有者繪圖的可變索引標籤大小。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱