Skip to main content

NavBarControl.CustomDrawLink Event

Provides the ability to perform custom painting of links.

Namespace: DevExpress.XtraNavBar

Assembly: DevExpress.XtraNavBar.v23.2.dll

NuGet Packages: DevExpress.Win, DevExpress.Win.Navigation

Declaration

Event Data

The CustomDrawLink event's data class is CustomDrawNavBarElementEventArgs. The following properties provide information specific to this event:

Property Description
Appearance Gets the painted element’s appearance settings. Inherited from CustomDrawObjectEventArgs.
Cache Gets an object which specifies the storage for the most used pens, fonts and brushes. Inherited from CustomDrawObjectEventArgs.
Caption Gets or sets the caption of the painted element.
Graphics Gets an object used to paint the object. Inherited from CustomDrawObjectEventArgs.
Handled Gets or sets a value specifying whether the control must perform default painting after an event handler has been executed. Inherited from CustomDrawObjectEventArgs.
Image Gets or sets the image displayed within the painted element.
ObjectInfo Gets an object providing information on the element being painted. Inherited from CustomDrawObjectEventArgs.
RealBounds Gets the bounding rectangle of the painted object. Inherited from CustomDrawObjectEventArgs.

Remarks

Write a CustomDrawLink event handler to perform custom painting of links. Set the CustomDrawObjectEventArgs.Handled property of the event parameter to true to disable default painting. Other properties of the event parameter allow you to identify the link being painted and provide all necessary information to paint.

Important

Do not change cell values, modify the control’s layout, or change the control’s object model in the events used for custom control painting. Actions that update the layout can cause the control to malfunction.

Example

The following sample code handles the NavBarControl.CustomDrawLink event to custom paint links. Links are painted differently in the hot tracked and pressed states.

The image below shows a custom painted hot tracked link:

CustomDraw - Link

private void navBarControl1_CustomDrawLink(object sender, DevExpress.XtraNavBar.ViewInfo.CustomDrawNavBarElementEventArgs e) {
    if (e.ObjectInfo.State == ObjectState.Hot || e.ObjectInfo.State == ObjectState.Pressed) {
        LinearGradientBrush brush;
        NavLinkInfoArgs linkInfo = e.ObjectInfo as NavLinkInfoArgs;
        if (e.ObjectInfo.State == ObjectState.Hot) {
            brush = new LinearGradientBrush(e.RealBounds, Color.Orange, Color.PeachPuff,
                LinearGradientMode.Horizontal);
        }
        else
            brush = new LinearGradientBrush(e.RealBounds, Color.PeachPuff, Color.Orange,
                LinearGradientMode.Horizontal);
        using (brush) {
            e.Cache.FillRectangle(Brushes.OrangeRed, e.RealBounds);
            Rectangle rect = e.RealBounds;
            rect.Inflate(-1, -1);
            e.Cache.FillRectangle(brush, rect);
            if (e.Image != null) {
                Rectangle imageRect = linkInfo.ImageRectangle;
                imageRect.X += (imageRect.Width - e.Image.Width) / 2;
                imageRect.Y += (imageRect.Height - e.Image.Height) / 2;
                imageRect.Size = e.Image.Size;
                e.Cache.DrawImageUnscaled(e.Image, imageRect);
            }
            e.Appearance.DrawString(e.Cache, e.Caption, linkInfo.RealCaptionRectangle, Brushes.White);
            e.Handled = true;
        }
    }
}
See Also