1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ToolWin, Vcl.ComCtrls,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons;
type
TForm1 = class(TForm)
pnlContainer: TPanel; // conteneur supérieur (inclut les flèches et pnlView)
btnLeft: TButton; // flèche gauche
btnRight: TButton; // flèche droite
pnlView: TPanel; // "fenêtre" qui masque la toolbar débordante
ToolBar1: TToolBar; // la toolbar contenant les TToolButton
procedure FormCreate(Sender: TObject);
procedure btnLeftClick(Sender: TObject);
procedure btnRightClick(Sender: TObject);
private const
H_PADDING = 8;
// padding vertical ajouté autour de la toolbar dans pnlContainer
SCROLL_STEP = 120; // pas de défilement en pixels
procedure DoScroll(DeltaX: Integer);
procedure UpdateArrowState;
function TotalButtonsWidth: Integer;
procedure AdjustContainerHeightFixed;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ Calcule la largeur totale occupée par les contrôles à l'intérieur de ToolBar1 }
function TForm1.TotalButtonsWidth: Integer;
var
i: Integer;
c: TControl;
maxR: Integer;
begin
maxR := 0;
for i := 0 to ToolBar1.ControlCount - 1 do
begin
c := ToolBar1.Controls[i];
if (c.Left + c.Width) > maxR then
maxR := c.Left + c.Width;
end;
Result := maxR + 4;
end;
{ Ajuste une fois la hauteur de pnlView et pnlContainer selon ToolBar1.Height et padding }
procedure TForm1.AdjustContainerHeightFixed;
begin
pnlView.Height := ToolBar1.Height;
pnlContainer.Height := ToolBar1.Height + H_PADDING;
end;
{ Initialisation : création dynamique de boutons et configuration de la ToolBar }
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
tb: TToolButton;
begin
ToolBar1.ShowCaptions := True; // afficher le texte des boutons si souhaité
// Création de boutons d'exemple (30) pour forcer l'overflow
for i := 1 to 30 do
begin
tb := TToolButton.Create(ToolBar1);
tb.Parent := ToolBar1;
tb.Caption := 'Btn' + IntToStr(i);
tb.Style := tbsButton;
tb.AutoSize := True;
end;
// Empêcher la ToolBar de se réorganiser automatiquement
ToolBar1.Wrapable := False; // ne pas passer à la ligne
ToolBar1.AutoSize := False; // on gère Width/Height nous-mêmes
ToolBar1.Align := alNone;
ToolBar1.Top := 0;
ToolBar1.Left := 0;
// Fixer la taille initiale de la toolbar et du conteneur
ToolBar1.Height := 32;
ToolBar1.Width := TotalButtonsWidth();
AdjustContainerHeightFixed;
UpdateArrowState;
end;
{ Déplace la ToolBar horizontalement en appliquant DeltaX et en limitant le mouvement
pour ne pas afficher d'espace vide à gauche ou à droite. }
procedure TForm1.DoScroll(DeltaX: Integer);
var
newLeft: Integer;
minLeft: Integer;
begin
newLeft := ToolBar1.Left + DeltaX;
// si la toolbar est plus petite ou égale à la zone visible, on la positionne à 0
if ToolBar1.Width <= pnlView.ClientWidth then
minLeft := 0
else
minLeft := pnlView.ClientWidth - ToolBar1.Width;
if newLeft > 0 then
newLeft := 0;
if newLeft < minLeft then
newLeft := minLeft;
ToolBar1.Left := newLeft;
UpdateArrowState;
end;
{ Click flèche gauche : on déplace la ToolBar vers la droite (montrer boutons à gauche). }
procedure TForm1.btnLeftClick(Sender: TObject);
begin
DoScroll(SCROLL_STEP);
end;
{ Click flèche droite : on déplace la ToolBar vers la gauche (montrer boutons à droite). }
procedure TForm1.btnRightClick(Sender: TObject);
begin
DoScroll(-SCROLL_STEP);
end;
{ Active ou désactive les flèches selon la possibilité de défiler. }
procedure TForm1.UpdateArrowState;
begin
if ToolBar1.Width <= pnlView.ClientWidth then
begin
// pas d'overflow : pas besoin de flèches
btnLeft.Enabled := False;
btnRight.Enabled := False;
ToolBar1.Left := 0;
end
else
begin
// left activé si on peut reculer (ToolBar1.Left < 0)
btnLeft.Enabled := ToolBar1.Left < 0;
// right activé si il reste des éléments à droite
btnRight.Enabled := (ToolBar1.Left + ToolBar1.Width) > pnlView.ClientWidth;
end;
end;
end. |
Partager