How to: Customize Display Information via Custom Draw Events

  • 2 minutes to read

Custom draw events allow you to perform the following tasks:

custom_draw_events

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;
    }
}