Skip to main content
A newer version of this page is available. .

How to: Custom Paint the Control's Background

  • 2 minutes to read

The following sample code handles the NavBarControl.CustomDrawBackground event to fill the XtraNavBarControl’s background with a linear gradient brush and draw a string at the bottom.

The image below shows the result.

CustomDraw - Background

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

private void navBarControl1_CustomDrawBackground(object sender, CustomDrawObjectEventArgs e) {

   Rectangle rect = e.RealBounds;
   Graphics gr = e.ObjectInfo.Graphics;

   // creating a brush and filling the background with it
   LinearGradientBrush backBrush = new LinearGradientBrush(rect, Color.Pink, 
     Color.LightSkyBlue, LinearGradientMode.Horizontal);
   gr.FillRectangle(backBrush, rect);

   // creating a new font for the string
   Font stringFont = new Font("Verdana", 8);
   // creating a brush used to draw the string
   LinearGradientBrush stringBrush = new LinearGradientBrush(rect, Color.Blue, 
     Color.Red, LinearGradientMode.Horizontal);
   // calculating string rectangle 
   int stringHeight = Convert.ToInt16(gr.MeasureString("www.devexpress.com", 
     stringFont).Height);
   Rectangle stringRect = new Rectangle(rect.Left, rect.Bottom - stringHeight, 
     rect.Width, stringHeight);
   // formatting string output
   StringFormat outStringFormat = new StringFormat();
   outStringFormat.Alignment = StringAlignment.Center;

   // drawing the string
   gr.DrawString("www.devexpress.com", stringFont, stringBrush, stringRect, outStringFormat);

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