Back to Devexpress

BarCustomContainerItem.PaintMenuBar Event

windowsforms-devexpress-dot-xtrabars-dot-barcustomcontaineritem-c6835152.md

latest6.7 KB
Original Source

BarCustomContainerItem.PaintMenuBar Event

Enables you to paint the content of the bar displayed to the left of the sub-menu.

Namespace : DevExpress.XtraBars

Assembly : DevExpress.XtraBars.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public event BarCustomDrawEventHandler PaintMenuBar
vb
Public Event PaintMenuBar As BarCustomDrawEventHandler

Event Data

The PaintMenuBar event's data class is BarCustomDrawEventArgs. The following properties provide information specific to this event:

PropertyDescription
BoundsGets the bounding rectangle of the painted element.
GraphicsGets the drawing surface of the element being painted.
HandledGets or sets a value specifying whether default painting must be performed.

Remarks

Sub-menus can display bars to the left of their content. You can specify the width of such bar via the BarCustomContainerItem.MenuBarWidth property. If this property value is not 0 , the bar is displayed. Note that nothing is painted in such bars by default.

Write a PaintMenuBar event handler to paint the contents of the menu bar. The parameters of the event allow you to obtain the painting surface and the bar’s bounds. You should set the BarCustomDrawEventArgs.Handled parameter to true to prohibit default painting.

Example

This example shows you how to paint the menu bar using the BarCustomContainerItem.PaintMenuBar event. The sample handler fills the background of the bar with a texture and linear gradient brush. Then it paints the name of the container item, whose menu is displayed.

The image below demonstrates code execution. (Note that the BarCustomContainerItem.MenuBarWidth property of corresponding container items must be set to 20 to obtain similar output).

csharp
using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.XtraBars;

readonly Image textureImage = Image.FromFile(@"C:\Images\lblue.gif");
readonly Font textFont = new Font("Verdana", 11, FontStyle.Bold);
private void BarSubItem1_PaintMenuBar(object sender, BarCustomDrawEventArgs e) {
   // creating a texture brush and filling the background with an image
    using(var textureBrush = new TextureBrush(textureImage))
        e.Graphics.FillRectangle(textureBrush, e.Bounds);
    // filling the background with a linear gradient brush
    using(var lBrush = new LinearGradientBrush(e.Bounds,
        Color.FromArgb(0, 0, 0, 0), Color.Blue, LinearGradientMode.Vertical))
        e.Graphics.FillRectangle(lBrush, e.Bounds);

    // formatting the output string
    using(var outStringFormat = new StringFormat()) {
        outStringFormat.Alignment = StringAlignment.Near;
        outStringFormat.LineAlignment = StringAlignment.Center;
        outStringFormat.FormatFlags |= StringFormatFlags.DirectionVertical;

        // transforming the painting surface and bounding rectangle
        // this allows you to provide the proper string orientation
        e.Graphics.RotateTransform(180);
        Rectangle rect = e.Bounds;
        rect.Offset(-rect.Width, -rect.Height);

        // determining the caption of the owning container item and painting it
        BarItem item = sender as BarItem;
        string outString = item.Caption.Replace("&", "") + " menu";
        e.Graphics.DrawString(outString, textFont,
            Brushes.White, rect, outStringFormat);
        e.Graphics.ResetTransform();
    }

    // prohibiting default painting
    e.Handled = true;
}
vb
Imports System.Drawing
Imports System.Drawing.Drawing2D

Private ReadOnly textureImage As Image = Image.FromFile("C:\Images\lblue.gif")
Private ReadOnly textFont As New Font("Verdana", 11, FontStyle.Bold)
Private Sub BarSubItem1_PaintMenuBar(ByVal sender As Object, _
 ByVal e As DevExpress.XtraBars.BarCustomDrawEventArgs) Handles BarSubItem1.PaintMenuBar
    ' creating a texture brush and filling the background with an image
    Using textureBrush = New TextureBrush(textureImage)
        e.Graphics.FillRectangle(textureBrush, e.Bounds)
    End Using
    ' filling the background with a linear gradient brush
    Using lBrush = New LinearGradientBrush(e.Bounds, Color.FromArgb(0, 0, 0, 0), Color.Blue, LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(lBrush, e.Bounds)
    End Using

    ' formatting the output string
    Using outStringFormat = New StringFormat()
        outStringFormat.Alignment = StringAlignment.Near
        outStringFormat.LineAlignment = StringAlignment.Center
        outStringFormat.FormatFlags = outStringFormat.FormatFlags Or StringFormatFlags.DirectionVertical

        ' transforming the painting surface and bounding rectangle
        ' this allows you to provide the proper string orientation
        e.Graphics.RotateTransform(180)
        Dim rect As Rectangle = e.Bounds
        rect.Offset(-rect.Width, -rect.Height)

        ' determining the caption of the owning container item and painting it
        Dim item As BarItem = TryCast(sender, BarItem)
        Dim outString As String = item.Caption.Replace("&", "") & " menu"
        e.Graphics.DrawString(outString, textFont, Brushes.White, rect, outStringFormat)
        e.Graphics.ResetTransform()
    End Using

    ' prohibiting default painting
    e.Handled = True
End Sub

See Also

BarCustomContainerItem.MenuBarWidth

PopupMenu.MenuBarWidth

PaintMenuBar

BarCustomContainerItem Class

BarCustomContainerItem Members

DevExpress.XtraBars Namespace