Back to Devexpress

NavBarControl.CustomDrawLink Event

windowsforms-devexpress-dot-xtranavbar-dot-navbarcontrol-b9dd1956.md

latest7.5 KB
Original Source

NavBarControl.CustomDrawLink Event

Provides the ability to perform custom painting of links.

Namespace : DevExpress.XtraNavBar

Assembly : DevExpress.XtraNavBar.v25.2.dll

NuGet Package : DevExpress.Win

Declaration

csharp
public event CustomDrawNavBarElementEventHandler CustomDrawLink
vb
Public Event CustomDrawLink As CustomDrawNavBarElementEventHandler

Event Data

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

PropertyDescription
AppearanceGets the painted element’s appearance settings. Inherited from CustomDrawObjectEventArgs.
CacheGets an object which specifies the storage for the most used pens, fonts and brushes. Inherited from CustomDrawObjectEventArgs.
CaptionGets or sets the caption of the painted element.
GraphicsGets an object used to paint the object. Inherited from CustomDrawObjectEventArgs.
HandledGets or sets a value specifying whether the control must perform default painting after an event handler has been executed. Inherited from CustomDrawObjectEventArgs.
ImageGets or sets the image displayed within the painted element.
ObjectInfoGets an object providing information on the element being painted. Inherited from CustomDrawObjectEventArgs.
RealBoundsGets 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:

csharp
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;
        }
    }
}
vb
Private Sub NavBarControl1_CustomDrawLink(sender As Object, e As CustomDrawNavBarElementEventArgs) Handles NavBarControl1.CustomDrawLink
    If e.ObjectInfo.State = ObjectState.Hot OrElse e.ObjectInfo.State = ObjectState.Pressed Then
        Dim brush As LinearGradientBrush
        Dim linkInfo As NavLinkInfoArgs = TryCast(e.ObjectInfo, NavLinkInfoArgs)
        If e.ObjectInfo.State = ObjectState.Hot Then
            brush = New LinearGradientBrush(e.RealBounds, Color.Orange, Color.PeachPuff, LinearGradientMode.Horizontal)
        Else
            brush = New LinearGradientBrush(e.RealBounds, Color.PeachPuff, Color.Orange, LinearGradientMode.Horizontal)
        End If
        Using brush
            e.Cache.FillRectangle(Brushes.OrangeRed, e.RealBounds)
            Dim rect As Rectangle = e.RealBounds
            rect.Inflate(-1, -1)
            e.Cache.FillRectangle(brush, rect)
            If e.Image IsNot Nothing Then
                Dim imageRect As Rectangle = 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)
            End If
            e.Appearance.DrawString(e.Cache, e.Caption, linkInfo.RealCaptionRectangle, Brushes.White)
            e.Handled = True
        End Using
    End If
End Sub

See Also

CustomDrawGroupCaption

CustomDrawHint

CustomDrawBackground

CustomDrawGroupClientBackground

CustomDrawGroupClientForeground

NavBarControl Class

NavBarControl Members

DevExpress.XtraNavBar Namespace