Compartir a través de


Controls with Built-In Owner-Drawing Support

Owner drawing in Windows Forms, which is also known as custom drawing, is a technique for changing the visual appearance of certain controls.

NoteNote

The word "control" in this topic is used to mean classes that derive from either Control or Component.

Typically, Windows handles painting automatically by using property settings such as BackColor to determine the appearance of a control. With owner drawing, you take over the painting process, changing elements of appearance that are not available by using properties. For example, many controls let you set the color of the text that is displayed, but you are limited to a single color. Owner drawing enables you to do things like display part of the text in black and part in red.

In practice, owner drawing is similar to drawing graphics on a form. For example, you could use graphics methods in a handler for the form's Paint event to emulate a ListBox control, but you would have to write your own code to handle all user interaction. With owner drawing, the control uses your code to draw its contents but otherwise retains all its intrinsic capabilities. You can use graphics methods to draw each item in the control or to customize some aspects of each item while you use the default appearance for other aspects of each item.

Owner Drawing in Windows Forms Controls

To perform owner drawing in controls that support it, you will typically set one property and handle one or more events.

Most controls that support owner drawing have an OwnerDraw or DrawMode property that indicates whether the control will raise its drawing-related event or events when it paints itself.

Controls that do not have an OwnerDraw or DrawMode property include the DataGridView control, which provides drawing events that occur automatically, and the ToolStrip control, which is drawn using an external rendering class that has its own drawing-related events.

There are many different kinds of drawing events, but a typical drawing event occurs in order to draw a single item within a control. The event handler receives an EventArgs object that contains information about the item being drawn and tools you can use to draw it. For example, this object typically contains the item's index number within its parent collection, a Rectangle that indicates the item's display boundaries, and a Graphics object for calling paint methods. For some events, the EventArgs object provides additional information about the item and methods that you can call to paint some aspects of the item by default, such as the background or a focus rectangle.

To create a reusable control that contains your owner-drawn customizations, create a new class that derives from a control class that supports owner drawing. Rather than handling drawing events, include your owner-drawing code in overrides for the appropriate OnEventName method or methods in the new class. Make sure that you call the base class OnEventName method or methods in this case so that users of your control can handle owner-drawing events and provide additional customization.

The following Windows Forms controls support owner drawing in all versions of the .NET Framework:

The following controls support owner drawing only in Microsoft .NET Framework version 2.0:

The following controls support owner drawing and are new in Microsoft .NET Framework version 2.0:

The following sections provide additional details for each of these controls.

ListBox and ComboBox Controls

The ListBox and ComboBox controls enable you to draw individual items in the control either all in one size, or in varying sizes.

NoteNote

Although the CheckedListBox control is derived from the ListBox control, it does not support owner drawing.

To draw each item the same size, set the DrawMode property to OwnerDrawFixed and handle the DrawItem event.

To draw each item using a different size, set the DrawMode property to OwnerDrawVariable and handle both the MeasureItem and DrawItem events. The MeasureItem event lets you indicate the size of an item before the DrawItem event occurs for that item.

For more information, including code examples, see the following topics:

The MenuItem component represents a single menu item in a MainMenu or ContextMenu component.

To draw a MenuItem, set its OwnerDraw property to true and handle its DrawItem event. To customize the size of the menu item before the DrawItem event occurs, handle the item's MeasureItem event.

For more information, including code examples, see the following reference topics:

TabControl Control

The TabControl control enables you to draw individual tabs in the control. Owner drawing affects only the tabs; the TabPage contents are not affected.

To draw each tab in a TabControl, set the DrawMode property to OwnerDrawFixed and handle the DrawItem event. This event occurs once for each tab only when the tab is visible in the control.

For more information, including code examples, see the following reference topics:

ToolTip Component

The ToolTip component enables you to draw the entire ToolTip when it is displayed.

To draw a ToolTip, set its OwnerDraw property to true and handle its Draw event. To customize the size of the ToolTip before the Draw event occurs, handle the Popup event and set the ToolTipSize property in the event handler.

For more information, including code examples, see the following reference topics:

ListView Control

The ListView control enables you to draw individual items, subitems, and column headers in the control.

To enable owner drawing in the control, set the OwnerDraw property to true.

To draw each item in the control, handle the DrawItem event.

To draw each subitem or column header in the control when the View property is set to Details, handle the DrawSubItem and DrawColumnHeader events.

For more information, including code examples, see the following reference topics:

TreeView Control

The TreeView control enables you to draw individual nodes in the control.

To draw only the text displayed in each node, set the DrawMode property to OwnerDrawText and handle the DrawNode event to draw the text.

To draw all elements of each node, set the DrawMode property to OwnerDrawAll and handle the DrawNode event to draw whichever elements you need, such as text, icons, check boxes, plus and minus signs, and lines connecting the nodes.

For more information, including code examples, see the following reference topics:

DataGridView Control

The DataGridView control enables you to draw individual cells and rows in the control.

To draw individual cells, handle the CellPainting event.

To draw individual rows or elements of rows, handle one or both of the RowPrePaint and RowPostPaint events. The RowPrePaint event occurs before the cells in a row are painted, and the RowPostPaint event occurs after the cells are painted. You can handle both events and the CellPainting event to paint row background, individual cells, and row foreground separately, or you can provide specific customizations where you need them and use the default display for other elements of the row.

For more information, including code examples, see the following topics:

ToolStrip Control

ToolStrip and derived controls enable you to customize any aspect of their appearance.

To provide custom rendering for ToolStrip controls, set the Renderer property of a ToolStrip, ToolStripManager, ToolStripPanel, or ToolStripContentPanel to a ToolStripRenderer object and handle one or more of the many drawing events provided by the ToolStripRenderer class. Alternatively, set a Renderer property to an instance of your own class derived from ToolStripRenderer, ToolStripProfessionalRenderer, or ToolStripSystemRenderer that implements or overrides specific OnEventName methods.

For more information, including code examples, see the following topics:

See Also

Other Resources

Controls to Use on Windows Forms