Back to Devexpress

How to: Custom Draw Gallery Items

windowsforms-2878-controls-and-libraries-ribbon-bars-and-menu-examples-ribbon-how-to-custom-draw-gallery-items.md

latest3.4 KB
Original Source

How to: Custom Draw Gallery Items

  • Nov 13, 2018
  • 2 minutes to read

Example 1

In this example the BaseGallery.CustomDrawItemText event is handled to manually paint the text within Gallery items.

The full source code can be found in the ‘Ribbon Simple Pad’ demo supplied with the XtraBars.

The image below shows the result:

csharp
using DevExpress.XtraBars.Ribbon.ViewInfo;

private void gddFont_Gallery_CustomDrawItemText(object sender, GalleryItemCustomDrawEventArgs e) {
    DevExpress.XtraBars.Ribbon.ViewInfo.GalleryItemViewInfo itemInfo = e.ItemInfo as DevExpress.XtraBars.Ribbon.ViewInfo.GalleryItemViewInfo;
    itemInfo.PaintAppearance.ItemDescriptionAppearance.Normal.DrawString(e.Cache, e.Item.Description, itemInfo.DescriptionBounds);
    AppearanceObject app = itemInfo.PaintAppearance.ItemCaptionAppearance.Normal.Clone() as AppearanceObject;
    app.Font = (Font)e.Item.Tag;
    try {
        e.Cache.DrawString(e.Item.Caption, app.Font, app.GetForeBrush(e.Cache), itemInfo.CaptionBounds);
    }
    catch { }
    e.Handled = true;
}
vb
Imports DevExpress.XtraBars.Ribbon.ViewInfo

Private Sub gddFont_Gallery_CustomDrawItemText(ByVal sender As Object, ByVal e As GalleryItemCustomDrawEventArgs) Handles gddFont.GalleryCustomDrawItemText
    Dim itemInfo As DevExpress.XtraBars.Ribbon.ViewInfo.GalleryItemViewInfo = TryCast(e.ItemInfo, DevExpress.XtraBars.Ribbon.ViewInfo.GalleryItemViewInfo)
    itemInfo.PaintAppearance.ItemDescriptionAppearance.Normal.DrawString(e.Cache, e.Item.Description, itemInfo.DescriptionBounds)
    Dim app As AppearanceObject = TryCast(itemInfo.PaintAppearance.ItemCaptionAppearance.Normal.Clone(), AppearanceObject)
    app.Font = CType(e.Item.Tag, Font)
    Try
        e.Cache.DrawString(e.Item.Caption, app.Font, app.GetForeBrush(e.Cache), itemInfo.CaptionBounds)
    Catch
    End Try
    e.Handled = True
End Sub

Example 2

In this example the BaseGallery.CustomDrawItemImage event is handled to manually paint item images within the Font Color Gallery. Individual items correspond to specific colors.

The full source code can be found in the ‘Ribbon Simple Pad’ demo supplied with the XtraBars.

The image below shows the result:

csharp
private void gddFontColor_Gallery_CustomDrawItemImage(object sender, 
  GalleryItemCustomDrawEventArgs e) {
    Color clr = (Color)e.Item.Tag;
    using(Brush brush = new SolidBrush(clr)) {
        e.Cache.FillRectangle(brush, e.Bounds);
        e.Handled = true;
    }
}
vb
Private Sub gddFontColor_Gallery_CustomDrawItemImage(ByVal sender As Object, _
  ByVal e As GalleryItemCustomDrawEventArgs) _
  Handles gddFontColor.GalleryCustomDrawItemImage, _
  rgbiFontColor.GalleryCustomDrawItemImage
    Dim clr As Color = CType(e.Item.Tag, Color)
    Dim brush As brush = New SolidBrush(clr)
    Try
        e.Cache.FillRectangle(brush, e.Bounds)
        e.Handled = True
    Finally
        CType(brush, IDisposable).Dispose()
    End Try
End Sub