--- title: "Controls with Built-In Owner-Drawing Support" ms.date: "03/30/2017" helpviewer_keywords: - "drawing [Windows Forms], owner" - "drawing [Windows Forms], custom" - "controls [Windows Forms], changing appearance" - "custom drawing" - "owner drawing" ms.assetid: 3823d01e-9610-43e6-864d-99f9b7c2b351 --- # 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. > [!NOTE] > The word "control" in this topic is used to mean classes that derive from either or . Typically, Windows handles painting automatically by using property settings such as 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 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 that indicates the item's display boundaries, and a 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 `On`*EventName* method or methods in the new class. Make sure that you call the base class `On`*EventName* 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: - - - (used by and ) - The following controls support owner drawing only in .NET Framework 2.0: - - - The following controls support owner drawing and are new in .NET Framework 2.0: - - The following sections provide additional details for each of these controls. ### ListBox and ComboBox Controls The and controls enable you to draw individual items in the control either all in one size, or in varying sizes. > [!NOTE] > Although the control is derived from the control, it does not support owner drawing. To draw each item the same size, set the `DrawMode` property to and handle the `DrawItem` event. To draw each item using a different size, set the `DrawMode` property to 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: - - - - - - - [How to: Create Variable Sized Text in a ComboBox Control](how-to-create-variable-sized-text-in-a-combobox-control.md) ### MenuItem Component The component represents a single menu item in a or component. To draw a , 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 control enables you to draw individual tabs in the control. Owner drawing affects only the tabs; the contents are not affected. To draw each tab in a , set the `DrawMode` property to 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 component enables you to draw the entire ToolTip when it is displayed. To draw a , set its `OwnerDraw` property to `true` and handle its `Draw` event. To customize the size of the before the `Draw` event occurs, handle the `Popup` event and set the property in the event handler. For more information, including code examples, see the following reference topics: - - - ### ListView Control The 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 property is set to , handle the `DrawSubItem` and `DrawColumnHeader` events. For more information, including code examples, see the following reference topics: - - - - ### TreeView Control The control enables you to draw individual nodes in the control. To draw only the text displayed in each node, set the `DrawMode` property to and handle the `DrawNode` event to draw the text. To draw all elements of each node, set the `DrawMode` property to 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 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: - - - - [How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control](customize-the-appearance-of-cells-in-the-datagrid.md) - [How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control](customize-the-appearance-of-rows-in-the-datagrid.md) ### ToolStrip Control and derived controls enable you to customize any aspect of their appearance. To provide custom rendering for controls, set the `Renderer` property of a , , , or 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`, , or that implements or overrides specific `On`*EventName* methods. For more information, including code examples, see the following topics: - - [How to: Create and Set a Custom Renderer for the ToolStrip Control in Windows Forms](create-and-set-a-custom-renderer-for-the-toolstrip-control-in-wf.md) - [How to: Custom Draw a ToolStrip Control](how-to-custom-draw-a-toolstrip-control.md) ## See also - [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)