Back to Devexpress

How to: Custom Paint the Control's Background

windowsforms-4910-controls-and-libraries-navigation-controls-navigation-bar-examples-appearance-how-to-custom-paint-the-controls-background.md

latest1.5 KB
Original Source

How to: Custom Paint the Control's Background

  • Sep 11, 2019

The code below handles the NavBarControl.CustomDrawBackground event to fill the background with a linear gradient brush.

csharp
using System.Drawing;

private void navBarControl1_CustomDrawBackground(object sender, CustomDrawObjectEventArgs e) {
    // Get the background bounds.
    Rectangle rect = e.RealBounds;

    // Get a gradient brush and fill the background. 
    Brush backBrush = e.Cache.GetGradientBrush(rect, Color.Pink, Color.LightSkyBlue, LinearGradientMode.Horizontal);
    e.Cache.FillRectangle(backBrush, rect);

    // Prevent the default paint algorithm from being invoked.
    e.Handled = true;
}
vb
Imports System.Drawing

Private Sub NavBarControl1_CustomDrawBackground(ByVal sender As Object, ByVal e As CustomDrawObjectEventArgs) _
  Handles NavBarControl1.CustomDrawBackground
    ' Get the background bounds.
    Dim rect As Rectangle = e.RealBounds

    ' Get a gradient brush and fill the background. 
    Dim backBrush As Brush = e.Cache.GetGradientBrush(rect, Color.Pink, Color.LightSkyBlue, LinearGradientMode.Horizontal)
    e.Cache.FillRectangle(backBrush, rect)

    ' Prevent the default paint algorithm from being invoked.
    e.Handled = True
End Sub