vcl-155071-expressbars-how-to-create-several-item-controls-for-the-same-underlying-item.md
This example illustrates how you can create several item controls based on a single item. This procedure creates a toolbar and several item controls. Fifteen item controls determine standard colors for a form; the sixteenth control activates the Color Selection dialog, which allows you to select a non-standard color. The dxBarManagerBarDockingStyleChange procedure is the OnBarDockingStyleChange event handler. It changes the paint style of the item controls when changing the docking style of a new toolbar.
// ...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, dxBar, StdCtrls;
// form declaration
type
TMainForm = class(TForm)
dxBarManager: TdxBarManager;
CreateColorBarButton: TButton;
procedure CreateColorBarButtonClick(Sender: TObject);
procedure dxBarManagerBarDockingStyleChange(Sender: TdxBarManager; ABar: TdxBar);
private
{ Private declarations }
barColor: TdxBar;
procedure btnColorClick(Sender: TObject);
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
// The OnClick event handler of a new item
procedure TMainForm.btnColorClick(Sender: TObject);
begin
with TdxBarButton(Sender).ClickItemLink do
if Data = -1 then
with TColorDialog.Create(nil) do
begin
Color := Self.Color;
if Execute then
Self.Color := Color;
Free;
end
else
Color := Data;
end;
procedure TMainForm.CreateColorBarButtonClick(Sender: TObject);
const
Captions: array[1..16] of string = ('Black', 'Maroon', 'Green', 'Olive', 'Navy', 'Purple', 'Teal', 'Gray', 'Silver', 'Red', 'Lime', 'Yellow', 'Blue', 'Fuchsia', 'Aqua', 'White');
Colors: array[1..16] of TColor = (clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray, clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite);
var
btnColor: TdxBarButton;
B: TBitmap;
I: Integer;
begin
with dxBarManager do
begin
// Creates a new item
btnColor := TdxBarButton.Create(Self);
with btnColor do
begin
PaintStyle := psCaptionGlyph;
OnClick := btnColorClick;
end;
// Creates a new toolbar for new item controls
barColor := Bars.Add;
with barColor do
begin
AllowCustomizing := False;
Caption := 'Color';
end;
// Creates a bitmap drawn on an item control's surface
B := TBitmap.Create;
with B do
begin
Width := 18;
Height := 16;
with Canvas do
begin
// For transparent color
Brush.Color := clBtnFace;
FillRect(Rect(0, 0, Width, Height));
// Draws frame
Brush.Color := clBtnShadow;
FrameRect(Rect(1, 0, Width - 1, Height));
end;
end;
// Creates item controls on a new toolbar
for I := 1 to 16 do
with barColor.ItemLinks.Add do
begin
Data := Colors[I];
Item := btnColor;
UserCaption := Captions[I];
with B, Canvas do
begin
Brush.Color := Colors[I];
FillRect(Rect(2, 1, Width - 2, Height - 1));
end;
UserGlyph := B;
end;
// Creates the 16th item control
// that calls the color selection dialog
with barColor.ItemLinks.Add do
begin
BeginGroup := True;
Data := -1;
Item := btnColor;
UserCaption := 'Custom...';
UserPaintStyle := psCaption;
end;
B.Free;
// Specifies position and displays a new toolbar on screen
barColor.FloatLeft := Left - 80;
barColor.FloatTop := Top;
barColor.Visible := True;
end;
end;
// The OnDockingStyleChange event handler of a bar manager
procedure TMainForm.dxBarManagerBarDockingStyleChange(Sender: TdxBarManager; ABar: TdxBar);
begin
if ABar = barColor then
with ABar do
begin
LockUpdate := True;
with TdxBarButton(ItemLinks[0].Item) do
if DockingStyle = dsNone then
PaintStyle := psCaptionGlyph
else
PaintStyle := psStandard;
LockUpdate := False;
end;
end;
end.
// ...
Unit1.h
#ifndef Unit1H
#define Unit1H
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "dxBar.hpp"
#include "dxBarExtItems.hpp"
class TMainForm : public TForm
{
__published: // IDE-managed Components
TdxBarManager *dxBarManager;
TButton * CreateColorBarButton;
void __fastcall CreateColorBarButtonClick(TObject *Sender);
void __fastcall dxBarManagerBarDockingStyleChange(TdxBarManager *Sender, TdxBar *ABar);
private: // User declarations
TdxBar * barColor;
void __fastcall btnColorClick(TObject * Sender);
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
extern PACKAGE TMainForm MainForm;
#endif
// C++ Builder
// ...
Unit1.cpp
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//-------------------------------------------------------------
#pragma package(smart_init)
#pragma link "dxBar"
#pragma link "dxBarExtItems"
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner)
{
}
const AnsiString Captions[] = {"Black", "Maroon", "Green", "Olive", "Navy", "Purple", "Teal", "Gray", "Silver", "Red", "Lime", "Yellow", "Blue", "Fuchsia", "Aqua", "White"};
const TColor Colors[] = {clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray, clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite};
void __fastcall TMainForm::CreateColorBarButtonClick(TObject *Sender)
{
TdxBarButton * btnColor;
Graphics::TBitmap * B;
int I;
TdxBarItemLink * NewItemLink;
// Creates a new item
btnColor = new TdxBarButton(this);
btnColor->PaintStyle = psCaptionGlyph;
btnColor->OnClick = btnColorClick;
// Creates a new toolbar for new item controls
barColor = dxBarManager->Bars->Add();
barColor->AllowCustomizing = false;
barColor->Caption = "Color";
// Creates a bitmap drawn on an item control surface
B = new Graphics::TBitmap;
B->Width = 18;
B->Height = 16;
// For transparent color
B->Canvas->Brush->Color = clBtnFace;
B->Canvas->FillRect(Rect(0, 0, B->Width, B->Height));
// Draws a frame
B->Canvas->Brush->Color = clBtnShadow;
B->Canvas->FrameRect(Rect(1, 0, B->Width - 1, B->Height));
// Creates item controls on a new toolbar
for(I=0;I<16;I++)
{
NewItemLink = barColor->ItemLinks->Add();
NewItemLink->Data = Colors[I];
NewItemLink->Item = btnColor;
NewItemLink->UserCaption = Captions[I];
B->Canvas->Brush->Color = Colors[I];
B->Canvas->FillRect(Rect(2, 1, B->Width - 2, B->Height - 1));
NewItemLink->UserGlyph = B;
}
// Creats the 16th item control
// that calls the color selection dialog
NewItemLink = barColor->ItemLinks->Add();
NewItemLink->BeginGroup = true;
NewItemLink->Data = -1;
NewItemLink->Item = btnColor;
NewItemLink->UserCaption = "Custom...";
NewItemLink->UserPaintStyle = psCaption;
delete B;
// Specifies the position and displays a new toolbar on screen
barColor->FloatLeft = Left - 80;
barColor->FloatTop = Top;
barColor->Visible = true;
}
void __fastcall TMainForm::btnColorClick(TObject * Sender)
{
TColorDialog * ColorDialog;
if( ((TdxBarButton *)Sender)->ClickItemLink->Data == -1)
{
ColorDialog = new TColorDialog(NULL);
ColorDialog->Color = this->Color;
if (ColorDialog->Execute())
this->Color = Color;
delete ColorDialog;
}
else
Color = (TColor)(((TdxBarButton *)Sender)->ClickItemLink->Data);
}
void __fastcall TMainForm::dxBarManagerBarDockingStyleChange(TdxBarManager *Sender, TdxBar *ABar)
{
TdxBarButton * Button;
if (ABar == barColor)
ABar->LockUpdate = true;
Button = (TdxBarButton *)(ABar->ItemLinks->Items[0]->Item);
if (ABar->DockingStyle == dsNone)
Button->PaintStyle = psCaptionGlyph;
else
Button->PaintStyle = psStandard;
ABar->LockUpdate = false;
}