windowsforms-devexpress-dot-xtranavbar-dot-navbarcontrol-86b19868.md
Provides the ability to perform custom painting of group captions.
Namespace : DevExpress.XtraNavBar
Assembly : DevExpress.XtraNavBar.v25.2.dll
NuGet Package : DevExpress.Win
public event CustomDrawNavBarElementEventHandler CustomDrawGroupCaption
Public Event CustomDrawGroupCaption As CustomDrawNavBarElementEventHandler
The CustomDrawGroupCaption 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. |
Write a CustomDrawGroupCaption event handler to perform custom painting of group captions. 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 group whose caption is painted and provide all necessary information to paint it.
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.
The following sample code handles the NavBarControl.CustomDrawGroupCaption event to custom paint the borders around group captions, fill in the background and paint its text. The background is filled in, in various different ways depending upon the state of the group’s caption.
The image below shows the look & feel of group captions in the normal, hot tracked and pressed states, respectively.
using System.Drawing.Drawing2D;
using DevExpress.Utils.Drawing;
using DevExpress.XtraNavBar.ViewInfo;
private void navBarControl1_CustomDrawGroupCaption(object sender,
CustomDrawNavBarElementEventArgs e) {
Rectangle outerRect = e.RealBounds;
e.Cache.FillRectangle(Brushes.Orange, outerRect);
// painting the background
Rectangle innerRect = outerRect;
innerRect.Inflate(-1, -1);
LinearGradientBrush innerBrush;
if(e.ObjectInfo.State == ObjectState.Hot)
innerBrush = new LinearGradientBrush(innerRect, Color.PeachPuff, Color.Orange,
LinearGradientMode.Vertical);
else if(e.ObjectInfo.State == ObjectState.Normal)
innerBrush = new LinearGradientBrush(innerRect, Color.Orange, Color.PeachPuff,
LinearGradientMode.Horizontal);
else
innerBrush = new LinearGradientBrush(innerRect, Color.Orange, Color.PeachPuff,
LinearGradientMode.Vertical);
using(innerBrush)
e.Cache.FillRectangle(innerBrush, innerRect);
// painting the caption
using(var outStringFormat = new StringFormat()) {
outStringFormat.Alignment = StringAlignment.Near;
outStringFormat.LineAlignment = StringAlignment.Center;
NavGroupInfoArgs info = e.ObjectInfo as NavGroupInfoArgs;
e.Cache.DrawString(info.Group.Caption, e.Appearance.Font, Brushes.White,
innerRect, outStringFormat);
}
// prohibiting default painting
e.Handled = true;
}
Imports System.Drawing.Drawing2D
Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraNavBar.ViewInfo
Private Sub NavBarControl1_CustomDrawGroupCaption(ByVal sender As Object, _
ByVal e As CustomDrawNavBarElementEventArgs) Handles NavBarControl1.CustomDrawGroupCaption
Dim outerRect As Rectangle = e.RealBounds
e.Cache.FillRectangle(Brushes.Orange, outerRect)
' painting the background
Dim innerRect As Rectangle = outerRect
innerRect.Inflate(-1, -1)
Dim innerBrush As LinearGradientBrush
If e.ObjectInfo.State = ObjectState.Hot Then
innerBrush = New LinearGradientBrush(innerRect, Color.PeachPuff, Color.Orange, LinearGradientMode.Vertical)
ElseIf e.ObjectInfo.State = ObjectState.Normal Then
innerBrush = New LinearGradientBrush(innerRect, Color.Orange, Color.PeachPuff, LinearGradientMode.Horizontal)
Else
innerBrush = New LinearGradientBrush(innerRect, Color.Orange, Color.PeachPuff, LinearGradientMode.Vertical)
End If
Using innerBrush
e.Cache.FillRectangle(innerBrush, innerRect)
End Using
' painting the caption
Using outStringFormat = New StringFormat()
outStringFormat.Alignment = StringAlignment.Near
outStringFormat.LineAlignment = StringAlignment.Center
Dim info As NavGroupInfoArgs = TryCast(e.ObjectInfo, NavGroupInfoArgs)
e.Cache.DrawString(info.Group.Caption, e.Appearance.Font, Brushes.White, innerRect, outStringFormat)
End Using
' prohibiting default painting
e.Handled = True
End Sub
See Also
CustomDrawGroupClientBackground