Skip to main content

dxBarRegisterItem, dxBarUnregisterItem Example

  • 2 minutes to read

The following code demonstrates custom item creation. This custom item is similar to TdxBarStatic, but allows you to specify text color.

unit CustomItem;
interface
uses
  Classes, Graphics, Windows, dxBar, cxGraphics;
type
  // defines the item class
  TdxBarColorStatic = class(TdxBarItem)
  private
    FColor : TColor;
    procedure SetColor(AColor : TColor);
  public
    constructor Create(AOwner : TComponent); override;
  published
    property Color: TColor read FColor write SetColor;
  end;
  // defines the item control class
  TdxBarColorStaticControl = class(TdxBarItemControl)
  private
    function GetItem: TdxBarColorStatic;
  protected
    function GetDefaultHeight: Integer; override;
    function GetDefaultWidth: Integer; override;
    function CanDestroyOnClick: Boolean; override;
    procedure DoPaint(ARect: TRect; PaintType: TdxBarPaintType); override;
  public
    property Item : TdxBarColorStatic read GetItem;
  end;
procedure Register;
implementation
constructor TdxBarColorStatic.Create(AOwner : TComponent);
begin
  inherited;
  FColor := 0;
end;
procedure TdxBarColorStatic.SetColor(AColor : TColor);
begin
// sets a new color and updates all linked item controls
  FColor := AColor;
  Update;
end;
function TdxBarColorStaticControl.GetItem : TdxBarColorStatic;
begin
  Result := TdxBarColorStatic(ItemLink.Item);
end;
// paints the item control on screen
procedure TdxBarColorStaticControl.DoPaint(ARect : TRect; PaintType : TdxBarPaintType);
var
  ASize: TSize;
  AText: string;
begin
  with DrawParams do
  begin
    if ARect.Left = ARect.Right then Exit;
    SetBkMode(Canvas.Handle, TRANSPARENT);
    SetTextColor(Canvas.Handle, Item.Color);
    Windows.FillRect(Canvas.Handle, ARect, GetSysColorBrush(COLOR_BTNFACE));
    AText := Item.Caption;
    cxGetTextExtentPoint32(Canvas.Handle, AText, ASize);
    if PaintType = ptVert then
      with ARect do
        OffsetRect(ARect,(Right-Left+ASize.cy)div 2,(Bottom-Top-ASize.cx)div 2)
    else
      with ARect do
        OffsetRect(ARect,(Right-Left-ASize.cx)div 2,(Bottom-Top-ASize.cy)div 2);
    cxDrawText(Canvas.Handle, AText, ARect, DT_SINGLELINE or DT_NOCLIP);
  end;
end;
function TdxBarColorStaticControl.GetDefaultWidth: Integer;
var
  ASize: TSize;
  AText string;
begin
  AText := Item.Caption;
  cxGetTextExtentPoint32(Canvas.Handle, AText, ASize);
  if Parent.IsVertical then
    Result := Parent.TextSize
  else
    Result := ASize.cx + (Parent.TextSize div 2);
end;
// returns the item control height based on its layout
function TdxBarColorStaticControl.GetDefaultHeight: Integer;
var
  ASize: TSize;
  AText: string;
begin
  AText := Item.Caption;
  cxGetTextExtentPoint32(Canvas.Handle, AText, ASize);
  if Parent.IsVertical then
    Result := ASize.cx + (Parent.TextSize div 2)
 else
    Result := Parent.TextSize;
end;
function TdxBarColorStaticControl.CanDestroyOnClick: Boolean;
begin
  Result := False;
end;
function TdxBarColorStaticControl.GetDefaultWidth: integer;
var
  Size : TSize;
  AText: string;
begin
  AText := Item.Caption;
  GetTextExtentPoint32(Parent.Canvas.Handle, PChar(AText), Length(AText), Size);
  if Parent.IsVertical then
    Result := Parent.TextSize
  else
    Result := Size.cx + (Parent.TextSize div 2);
end;
// returns the item control height according to its layout
function TdxBarColorStaticControl.GetDefaultHeight: integer;
var
  Size : TSize;
  AText: string;
begin
  AText := Item.Caption;
  GetTextExtentPoint32(Parent.Canvas.Handle, PChar(AText), Length(AText), Size);
  if Parent.IsVertical then
    Result := Size.cx + (Parent.TextSize div 2)
 else
    Result := Parent.TextSize;
end;
procedure Register;
begin
  RegisterNoIcon([TdxBarColorStatic]);
end;
initialization
dxBarRegisterItem(TdxBarColorStatic, TdxBarColorStaticControl, True);
finalization
dxBarUnregisterItem(TdxBarColorStatic);
end.