Back to Devexpress

CardView.CustomDrawCardCaption Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-card-dot-cardview-2245c762.md

latest9.4 KB
Original Source

CardView.CustomDrawCardCaption Event

Enables card captions to be custom painted.

Namespace : DevExpress.XtraGrid.Views.Card

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("CustomDraw")]
public event CardCaptionCustomDrawEventHandler CustomDrawCardCaption
vb
<DXCategory("CustomDraw")>
Public Event CustomDrawCardCaption As CardCaptionCustomDrawEventHandler

Event Data

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

PropertyDescription
AppearanceGets the painted element’s appearance settings. Inherited from CustomDrawEventArgs.
BoundsReturns a value specifying limits for the drawing area. Inherited from CustomDrawEventArgs.
CacheProvides methods to paint on drawing surfaces in GDI+ and DirectX modes. See DirectX hardware acceleration to learn more. Inherited from CustomDrawEventArgs.
CardCaptionGets or sets the current card’s caption.
CardInfoGets an object that stores the information needed to paint a card.
GraphicsA GDI+ drawing surface. Use the CustomDrawEventArgs.Cache property instead if you enable the DirectX hardware acceleration. Inherited from CustomDrawEventArgs.
HandledGets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs.
RowHandleGets the row handle identifying the card whose caption is painted.

The event data class exposes the following methods:

MethodDescription
DefaultDraw()Performs default painting of an element. Inherited from CustomDrawEventArgs.
DrawHtml(HtmlTemplate, DxHtmlPainterContext, Action<DxHtmlPainterArgs>)Paints the required HTML template inside an element that raised this event. The context parameter allows you to assign an object that transfers mouse events to template elements. Inherited from CustomDrawEventArgs.
DrawHtml(HtmlTemplate, Action<DxHtmlPainterArgs>)Paints the required HTML template inside an element that raised this event. Inherited from CustomDrawEventArgs.

Remarks

This event is raised each time a card caption needs to be repainted, and enables you to paint it manually. See the Custom Painting Basics and Custom Painting Scenarios topics for information on using custom draw events.

If you need to paint caption text as specified by the CardView.CardCaptionFormat property, use the CardView.GetCardCaption method to obtain the formatted text.

Important

Do not change cell values, modify the control’s layout, or change the control’s object model in the events used for custom control painting. Actions that update the layout can cause the control to malfunction.

Example

The following example lists a CardView.CustomDrawCardCaption event handler which we use to perform custom painting of card captions. Card captions are drawn differently for the focused and non focused cards.

csharp
private void cardView1_CustomDrawCardCaption(object sender, CardCaptionCustomDrawEventArgs e) {
    CardView view = sender as CardView;
    bool isFocusedCard = e.RowHandle == view.FocusedRowHandle;
    //The brush to draw the background of card captions.
    Brush backBrush, foreBrush;
    Color color1 = Color.FromArgb(142, 57, 80);
    Color color2 = Color.FromArgb(240, 140, 40);
    Color color3 = Color.FromArgb(70, 55, 94);
    Color color4 = Color.FromArgb(144, 84, 84);
    if (isFocusedCard) {
        backBrush = e.Cache.GetGradientBrush(e.Bounds, color1, color2, LinearGradientMode.ForwardDiagonal);
        foreBrush = Brushes.White;
    }
    else {
        backBrush = e.Cache.GetGradientBrush(e.Bounds, color3, color4, LinearGradientMode.ForwardDiagonal);
        foreBrush = Brushes.Coral;
    }
    Rectangle r = e.Bounds;
    r.Inflate(1, 0);
    e.Cache.FillRectangle(backBrush, r);
    // Draw the text.
    e.Appearance.DrawString(e.Cache, view.GetCardCaption(e.RowHandle),r, foreBrush);
    // Disable default painting.
    e.Handled = true;
}
vb
Private Sub CardView1_CustomDrawCardCaption(sender As Object, e As CardCaptionCustomDrawEventArgs) Handles CardView1.CustomDrawCardCaption
    Dim view As CardView = TryCast(sender, CardView)
    Dim isFocusedCard As Boolean = e.RowHandle = view.FocusedRowHandle
    Dim backBrush, foreBrush As Brush
    Dim color1 As Color = Color.FromArgb(142, 57, 80)
    Dim color2 As Color = Color.FromArgb(240, 140, 40)
    Dim color3 As Color = Color.FromArgb(70, 55, 94)
    Dim color4 As Color = Color.FromArgb(144, 84, 84)
    If isFocusedCard Then
        backBrush = e.Cache.GetGradientBrush(e.Bounds, color1, color2, LinearGradientMode.ForwardDiagonal)
        foreBrush = Brushes.White
    Else
        backBrush = e.Cache.GetGradientBrush(e.Bounds, color3, color4, LinearGradientMode.ForwardDiagonal)
        foreBrush = Brushes.Coral
    End If

    Dim r As Rectangle = e.Bounds
    r.Inflate(1, 0)
    e.Cache.FillRectangle(backBrush, r)
    e.Appearance.DrawString(e.Cache, view.GetCardCaption(e.RowHandle), r, foreBrush)
    e.Handled = True
End Sub

See Also

GetCardCaption(Int32)

Custom Painting Basics

Custom Painting Scenarios

Elements that Can Be Custom Painted

Manually Invalidating Controls

CardView Class

CardView Members

DevExpress.XtraGrid.Views.Card Namespace