DrawMode 列挙体
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Public Enumeration DrawMode

メンバ名 | 説明 | |
---|---|---|
Normal | コントロールのすべての要素は、オペレーティング システムによって同じサイズに描画されます。 | |
OwnerDrawFixed | コントロールのすべての要素は、手動で同じサイズに描画されます。 | |
OwnerDrawVariable | コントロール内のすべての要素は手動で描画し、要素ごとに異なるサイズを指定できます。 |

この列挙体は、ListBox、CheckedListBox、ComboBox の各クラスの DrawMode などのメンバで使用されます。
コントロールの一部または特定の要素の描画をオーバーライドできます。この列挙体を使用して、オペレーティング システムによってコントロールを描画するか、ユーザーが独自に作成したコードでコントロールの描画を処理するかを指定します。
![]() |
---|
CheckedListBox クラスは DrawMode.Normal だけをサポートします。オーナー描画モードは無視されます。 |
DrawMode 列挙体の使用法の詳細については、MeasureItem イベントおよび DrawItem イベントのほか、ItemHeight プロパティを参照してください。

オーナー描画の ListBox 項目を作成する方法を次の例に示します。このコードでは、DrawMode プロパティを使用して、描画する項目が固定サイズであることを示し、DrawItem イベントを使用して、ListBox への各項目の描画を実行しています。この例のコードでは、イベント ハンドラへのパラメータとして渡された DrawItemEventArgs クラスのプロパティとメソッドを使用して項目を描画しています。この例は、listBox1 という名前の ListBox コントロールがフォームに既に追加されていて、DrawItem イベントがこの例のコードに定義されているイベント ハンドラで処理されることを前提にしています。この例では、"Apple"、"Orange"、"Plum" の 3 つのテキストの項目がこの順番で ListBox に追加されていることも想定しています。
Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem ' Set the DrawMode property to draw fixed sized items. ListBox1.DrawMode = DrawMode.OwnerDrawFixed ' Draw the background of the ListBox control for each item. e.DrawBackground() ' Define the default color of the brush as black. Dim myBrush As Brush ' Determine the color of the brush to draw each item based on the index of the item to draw. Select Case (e.Index) Case 0 myBrush = Brushes.Red Case 1 myBrush = Brushes.Orange Case 2 myBrush = Brushes.Purple End Select ' Draw the current item text based on the current Font and the custom brush settings. e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) ' If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle() End Sub
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // Set the DrawMode property to draw fixed sized items. listBox1.DrawMode = DrawMode.OwnerDrawFixed; // Draw the background of the ListBox control for each item. e.DrawBackground(); // Define the default color of the brush as black. Brush myBrush = Brushes.Black; // Determine the color of the brush to draw each item based on the index of the item to draw. switch (e.Index) { case 0: myBrush = Brushes.Red; break; case 1: myBrush = Brushes.Orange; break; case 2: myBrush = Brushes.Purple; break; } // Draw the current item text based on the current Font and the custom brush settings. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds ,StringFormat.GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); }
private: void listBox1_DrawItem( Object^ /*sender*/, System::Windows::Forms::DrawItemEventArgs^ e ) { // Set the DrawMode property to draw fixed sized items. listBox1->DrawMode = DrawMode::OwnerDrawFixed; // Draw the background of the ListBox control for each item. e->DrawBackground(); // Define the default color of the brush as black. Brush^ myBrush = Brushes::Black; // Determine the color of the brush to draw each item based on the index of the item to draw. switch ( e->Index ) { case 0: myBrush = Brushes::Red; break; case 1: myBrush = Brushes::Orange; break; case 2: myBrush = Brushes::Purple; break; } // Draw the current item text based on the current Font and the custom brush settings. e->Graphics->DrawString( listBox1->Items[ e->Index ]->ToString(), e->Font, myBrush, e->Bounds, StringFormat::GenericDefault ); // If the ListBox has focus, draw a focus rectangle around the selected item. e->DrawFocusRectangle(); }
private void listBox1_DrawItem(Object sender , System.Windows.Forms.DrawItemEventArgs e) { // Set the DrawMode property to draw fixed sized items. listBox1.set_DrawMode(DrawMode.OwnerDrawFixed); // Draw the background of the ListBox control for each item. e.DrawBackground(); // Create a new Brush and initialize to a Black colored brush // by default. Brush myBrush = Brushes.get_Black(); // Determine the color of the brush to draw each item based on the // index of the item to draw. switch (e.get_Index()) { case 0 : myBrush = Brushes.get_Red(); break; case 1 : myBrush = Brushes.get_Orange(); break; case 2 : myBrush = Brushes.get_Purple(); break; } // Draw the current item text based on the current Font and the custom // brush settings. e.get_Graphics().DrawString(System.Convert.ToString(listBox1. get_Items().get_Item(e.get_Index())), e.get_Font(), myBrush, RectangleF.op_Implicit(e.get_Bounds()), StringFormat. get_GenericDefault()); // If the ListBox has focus, draw a focus rectangle around the selected // item. e.DrawFocusRectangle(); } //listBox1_DrawItem

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DrawMode 列挙体のページへのリンク