次の方法で共有


TabControl.DrawItem イベント

DrawMode プロパティが OwnerDrawFixed に設定されている場合に、タブが描画されたときに発生します。

Public Event DrawItem As DrawItemEventHandler
[C#]
public event DrawItemEventHandler DrawItem;
[C++]
public: __event DrawItemEventHandler* DrawItem;

[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。

イベント データ

イベント ハンドラが、このイベントに関連するデータを含む、DrawItemEventArgs 型の引数を受け取りました。次の DrawItemEventArgs プロパティには、このイベントの固有の情報が記載されます。

プロパティ 説明
BackColor 描画されている項目の背景色を取得します。
Bounds 描画されている項目の境界を表す四角形を取得します。
Font 描画されている項目に割り当てられているフォントを取得します。
ForeColor 描画されている項目の前景色を取得します。
Graphics 項目を描画するグラフィックス表面を取得します。
Index 描画されている項目のインデックス値を取得します。
State 描画されている項目の状態を取得します。

解説

イベント処理の詳細については、「 イベントの利用 」を参照してください。

使用例

[Visual Basic, C#, C++] 1 つの TabPage が配置された TabControl を作成する例を次に示します。この例では、 tabPage1 のタブ上に文字列および Rectangle を描画するために使用するイベント ハンドラを宣言しています。このイベント ハンドラは、 DrawItem イベントに連結されます。

[Visual Basic, C#, C++] この例では、 System.Drawing 名前空間と System.Windows.Forms 名前空間を使用します。

 
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

[C#] 
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());
    }
}

[C++] 
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class Form1 : public Form {
private:
    Rectangle  tabArea;
    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 = 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 = RectangleF::op_Implicit(tabControl1->GetTabRect(0));

        // Binds the event handler DrawOnTab to the DrawItem event
        // through the DrawItemEventHandler delegate.
        tabControl1->DrawItem += new DrawItemEventHandler(this, &Form1::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);
        System::Drawing::Font* font = new System::Drawing::Font(S"Arial", 10.0f);
        SolidBrush* brush = new SolidBrush(Color::Red);

        g->DrawRectangle(p, tabArea);
        g->DrawString(S"tabPage1", font, brush, tabTextArea);
    }
};

int main() {
    Application::Run(new Form1());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TabControl クラス | TabControl メンバ | System.Windows.Forms 名前空間 | DrawItemEventHandler | OnDrawItem