Skip to main content

TcxCustomButton.OnCustomDraw Event

Enables you to custom paint the button.

Declaration

property OnCustomDraw: TcxButtonCustomDrawEvent read; write;

Remarks

Sender specifies the button being painted.

ACanvas specifies the drawing surface.

AViewInfo provides ViewInfo information (a TcxButtonViewInfo class instance) used to paint the button. The table below lists the main TcxButtonViewInfo members useful for custom painting.

Member Description
Painter The painter class that is used to paint the button.
State The button’s state.
Bounds or ButtonRect The bounding rectangle of the button.
CommandLinkHintRect
GetContentRect The bounding rectangle of the button content for a specific button state.
DropDownButtonRect The bounding rectangle of the button’s dropdown button.
TextRect The bounding rectangle of the button caption.
GlyphPos The position of an image displayed within the button.
DrawBackground Paints the button’s background without the button content.

Note

Since methods of ViewInfo classes support the internal infrastructure and are not intended for use in your code, we recommend that you use only the ViewInfo class properties and methods mentioned above.

AHandled specifies whether default painting is required. Set AHandled to True, to prevent default code execution.

The following code snippet demonstrates how to handle the OnCustomDraw event to paint a button that displays an icon and two text blocks.

uses
  ..., cxGeometry;
// ...
implementation
procedure SetFont(AFont: TFont; ASize: Integer; const AColor: TColor; AStyle: TFontStyles);
begin
  AFont.Size := ASize;
  AFont.Color := AColor;
  AFont.Style := AStyle;
end;
procedure <Form>.cxButton1CustomDraw(Sender: TObject; ACanvas: TcxCanvas; AViewInfo: TcxButtonViewInfo; var AHandled: Boolean);
var
  ARect: TRect;
  AContentRect: TRect;
begin
  AContentRect := cxRectContent(AViewInfo.GetContentRect(AViewInfo.State), Rect(6, 6, 6, 6));
  if AViewInfo.State = cxbsPressed then
    AContentRect := cxRectOffset(AContentRect, 1, 1);
  AViewInfo.DrawBackground(ACanvas);
  cxImageList1.Draw(ACanvas.Canvas, AContentRect.Left, AContentRect.Top, 0);
  SetFont(ACanvas.Font, 14, clBlue, []);
  ARect := cxRectSetLeft(AContentRect, AContentRect.Left + cxImageList1.Width + 7);
  ARect.Top := ARect.Top + (cxImageList1.Height + ACanvas.Font.Height) div 2;
  ACanvas.DrawTexT('Live Chat', ARect, 0);
  ARect := cxRectSetTop(AContentRect, AContentRect.Top + cxImageList1.Height + 10);
  SetFont(ACanvas.Font, 9, clWindowText, []);
  ACanvas.DrawTexT('Have a pre-sales question?' + #13 + 'Need assistance with your evaluation?' + #13 + 'We are here to help.', ARect, cxWordBreak);
  AHandled := True;
end;

The following code snippet demonstrates how to handle the OnCustomDraw event to paint a button with a rotated caption.

procedure SetFont(AFont: TFont; ASize: Integer; const AColor: TColor; AStyle: TFontStyles);
begin
  AFont.Size := ASize;
  AFont.Color := AColor;
  AFont.Style := AStyle;
end;
procedure <Form>.cxButton2CustomDraw(Sender: TObject; ACanvas: TcxCanvas; AViewInfo: TcxButtonViewInfo; var AHandled: Boolean);
  procedure SetCanvas(ACanvas: TcxCanvas);
  begin
    ACanvas.Pen.Width := 1;
    ACanvas.Pen.Color := RGB(131, 170, 191);
    ACanvas.Brush.Style := bsClear;
    SetFont(ACanvas.Font, 14, RGB(11, 68, 106), [fsBold, fsItalic]);
  end;
const
  StateToColors: array[TcxButtonState, 0..1] of TColor = ((9420793, 1864666), (9420793, 1864666), (10865658, 4820961), (8432608, 1664964), (clBtnFace, clBtnFace));
var
  ATextRect: TRect;
  AContentRect: TRect;
begin
  AContentRect := AViewInfo.GetContentRect(AViewInfo.State);
  if AViewInfo.State = cxbsPressed then
    AContentRect := cxRectOffset(AContentRect, 1, 1);
  FillGradientRect(ACanvas.Handle, AViewInfo.ButtonRect, StateToColors[AViewInfo.State, 0], StateToColors[AViewInfo.State, 1], True);
  SetCanvas(ACanvas);
  ACanvas.Rectangle(AViewInfo.ButtonRect);
  ATextRect := AContentRect;
  ATextRect := cxRectSetRight(AContentRect, AContentRect.Right - (cxRectWidth(ATextRect) + ACanvas.Font.Height) div 2 + 2);
  ACanvas.DrawTexT('Feedback', ATextRect, cxAlignHCenter, True, raMinus90);
  AHandled := True;
end;
See Also