Skip to main content

Custom Painting

  • 3 minutes to read

All DevExpress .NET Windows Forms controls use Appearances (the AppearanceObject objects) to customize their look and feel. The vertical grid controls (VGridControl and PropertyGridControl) provide both design time and runtime facilities for customizing the appearance of their visual elements. This implies modifying attributes such as the background and foreground colors, background images, font settings, etc. The appearance settings used to paint the grid’s elements can be customized in a number of ways which are listed in the Appearance and Conditional Formatting document.

If the appearance customization techniques provided don’t suit your needs, you can handle the custom draw events.

CUSTOM PAINTING BASICS

You can paint elements manually by handling vertical grid events, specifically designed for this purpose. Each event is used to paint the elements of a specific type and fires before an individual element is painted. For instance, the VGridControlBase.CustomDrawTreeButton event can be used to paint tree buttons displayed within the headers of parent rows. It fires for each individual tree button every time the button needs to be painted (e.g. when the row becomes visible or when it is expanded or collapsed). Thus, you can provide a custom appearance for all types of elements in all their states.

The image below shows an example of using the custom painting feature.

Concepts - CustomPainting - Basics

The custom draw events can be handled for the following purposes:

  • To paint the elements manually.

    This allows you to provide any appearance you like for grid elements.

  • To paint the elements using the default mechanism but with modified appearance settings.

    This can be useful when custom text needs to be displayed within data cells or different appearances provided for elements of the same type (row headers, data cells, etc.).

Custom draw events are raised before the grid elements are painted. These events have different sets of parameters since they are designed to paint different elements. However, some parameters are common to all events. These parameters are listed below.

Parameter Description
CustomDrawEventArgs.Graphics Gets the System.Drawing.Graphics object which represents the painting surface. This object provides a number of methods that can be used to paint text, images and geometrical primitives (such as rectangles, circles, polylines, etc). Use these methods to paint the processed element’s content in the manner you want.
CustomDrawEventArgs.Bounds Gets the bounding rectangle of the element being painted. Use this parameter to determine where to paint.
CustomDrawEventArgs.Cache Gets an object which specifies the storage for the most used pens, fonts and brushes.
CustomDrawEventArgs.Handled Specifies whether the event is handled. If custom drawing is implemented and the Handled property’s value is set to false, all the custom drawing performed will be overwritten by the default painting mechanism using the appearance settings. Set this property to true to prevent the default painting mechanism from clearing any custom drawing.
CustomDrawEventArgs.Appearance Gets the appearance settings used to paint an element.

See the following documents for a list of the elements that can be custom painted and examples:

See Also