Back to Devexpress

How to: Customize Display Information via Custom Draw Events

windowsforms-4877-controls-and-libraries-navigation-controls-navigation-bar-examples-appearance-how-to-customize-display-information-via-custom-draw-events.md

latest3.5 KB
Original Source

How to: Customize Display Information via Custom Draw Events

  • Nov 13, 2018
  • 2 minutes to read

Custom draw events allow you to perform the following tasks:

  • Manually paint a visual element

  • Customize paint parameters and let the control draw visual elements

csharp
using System.Drawing.Drawing2D;
using DevExpress.XtraNavBar.ViewInfo;
using DevExpress.Utils.Drawing;

Font textFont = new Font("Verdana", 8);

private void navBarControl1_CustomDrawBackground(object sender, CustomDrawObjectEventArgs e) {
    Rectangle rect = e.RealBounds;
    Graphics gr = e.ObjectInfo.Graphics;

    // A brush to draw text.
    using (LinearGradientBrush stringBrush = new LinearGradientBrush(rect, Color.Blue, Color.Red, LinearGradientMode.Horizontal)) {
        // Calculate the text rectangle.
        int stringHeight = Convert.ToInt16(gr.MeasureString("www.devexpress.com", textFont).Height);
        Rectangle stringRect = new Rectangle(rect.Left, rect.Bottom - stringHeight,
          rect.Width, stringHeight);
        // Format string output.
        StringFormat outStringFormat = new StringFormat();
        outStringFormat.Alignment = StringAlignment.Center;

        // Draw the text.
        gr.DrawString("www.devexpress.com", textFont, stringBrush, stringRect, outStringFormat);

        // Prohibit default background painting
        e.Handled = true;
    }
}

private void navBarControl1_CustomDrawLink(object sender, CustomDrawNavBarElementEventArgs e) {
    // If a link is not hot tracked or pressed it is drawn in the normal way.
    if (e.ObjectInfo.State == ObjectState.Hot || e.ObjectInfo.State == ObjectState.Pressed) {
        NavLinkInfoArgs linkInfo = e.ObjectInfo as NavLinkInfoArgs;
        // Customize the font style.
        e.Appearance.FontStyleDelta = FontStyle.Bold;
    }
}
vb
Dim textFont As New Font("Verdana", 8)
Private Sub navBarControl1_CustomDrawBackground(ByVal sender As Object, ByVal e As CustomDrawObjectEventArgs) Handles NavBarControl1.CustomDrawBackground
    Dim rect As Rectangle = e.RealBounds
    Dim gr As Graphics = e.ObjectInfo.Graphics
    ' A brush to draw text.
    Using stringBrush As New LinearGradientBrush(rect, Color.Blue, Color.Red, LinearGradientMode.Horizontal)
        ' Calculate the text rectangle.
        Dim stringHeight As Integer = Convert.ToInt16(gr.MeasureString("www.devexpress.com", textFont).Height)
        Dim stringRect As New Rectangle(rect.Left, rect.Bottom - stringHeight, rect.Width,
stringHeight)
        ' Format string output.
        Dim outStringFormat As New StringFormat()
        outStringFormat.Alignment = StringAlignment.Center
        ' Draw the string.
        gr.DrawString("www.devexpress.com", textFont, stringBrush, stringRect, outStringFormat)
        ' Prohibit default background painting
        e.Handled = True
    End Using
End Sub

Private Sub navBarControl1_CustomDrawLink(ByVal sender As Object, ByVal e As CustomDrawNavBarElementEventArgs) Handles navBarControl1.CustomDrawLink
    ' If a link is not hot tracked or pressed it is drawn in the normal way.
    If e.ObjectInfo.State = ObjectState.Hot OrElse e.ObjectInfo.State = ObjectState.Pressed Then
        Dim linkInfo As NavLinkInfoArgs = TryCast(e.ObjectInfo, NavLinkInfoArgs)
        ' Customize the font style.
        e.Appearance.FontStyleDelta = FontStyle.Bold;
    End If

End Sub