Créer des boutons avec un rendu web

Des boutons au look web dans une appli VCL, avouons-le, ça a tout de suite plus d’allure. Problème : la VCL s’appuie sur les API Windows (contrairement à FireMonkey, beaucoup plus libre côté design), et un simple Caption texte ne va pas se souligner tout seul façon lien web au clic.

Heureusement, la HTML Component Library change la donne : le Caption d’un THtLabel accepte du vrai HTML, interprété comme une mini-page web.

Objectif : une barre de navigation de 4 THtLabel posés sur un TPanel. Au clic, l’onglet actif change de couleur et se souligne — comme sur le web.

Le code

Le secret : on injecte un petit gabarit HTML formaté (couleur + soulignement) dans le Caption de chaque THtLabel au démarrage (FormCreate) puis dans chaque OnClick :

unit FrmBoutonLabelWeb;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, htmlcomp, Vcl.ExtCtrls;

type
  TFormBoutonLabelWeb = class(TForm)
    PanelAffichageColonnes: TPanel;
    L_Accueil: THtLabel;
    L_Param: THtLabel;
    L_Donnees: THtLabel;
    L_APropos: THtLabel;
    procedure FormCreate(Sender: TObject);
    procedure L_ParamClick(Sender: TObject);
    procedure L_DonneesClick(Sender: TObject);
    procedure L_AProposClick(Sender: TObject);
    procedure L_AccueilClick(Sender: TObject);
  private
  public
  end;

var
  FormBoutonLabelWeb: TFormBoutonLabelWeb;

implementation

{$R *.dfm}

const
  Html = '<!DOCTYPE html>'
        +'<html lang="fr">'
        +'<head>'
        +'<meta charset="UTF-8">'
        +'<meta name="viewport" content="width=device-width, initial-scale=1.0">'
        +'<style>'
        +'  body {'
        +'    font-family: Arial, sans-serif;'
        +'    text-align: center;'
        +'  }'
        +'  .Custom-Color {'
        +'    color: %s;'
        +'    %s;'
        +'  }'
        +'</style>'
        +'</head>'
        +'<body>'
        +'	<span class="Custom-Color">%s</span>'
        +'  </body>'
        +'</html>';

  AttributUnderline = 'text-decoration: underline';
  ColorActiveText   = '#0178D6';
  ColorInactiveText = '#333333';
  CaptionAccueil    = 'Accueil';
  CaptionParam      = 'Paramétrage';
  CaptionDonnees    = 'Gestion des données';
  CaptionAPropos    = 'A propos';

procedure TFormBoutonLabelWeb.FormCreate(Sender: TObject);
begin
  L_Accueil.Caption := Format(Html, [ColorActiveText, AttributUnderline, CaptionAccueil]);
  L_Param.Caption   := Format(Html, [ColorInactiveText, EmptyStr, CaptionParam]);
  L_Donnees.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionDonnees]);
  L_APropos.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAPropos]);
end;

procedure TFormBoutonLabelWeb.L_AccueilClick(Sender: TObject);
begin
  L_Accueil.Caption := Format(Html, [ColorActiveText, AttributUnderline, CaptionAccueil]);
  L_Param.Caption   := Format(Html, [ColorInactiveText, EmptyStr, CaptionParam]);
  L_Donnees.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionDonnees]);
  L_APropos.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAPropos]);
end;

procedure TFormBoutonLabelWeb.L_ParamClick(Sender: TObject);
begin
  L_Accueil.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAccueil]);
  L_Param.Caption   := Format(Html, [ColorActiveText, AttributUnderline, CaptionParam]);
  L_Donnees.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionDonnees]);
  L_APropos.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAPropos]);
end;

procedure TFormBoutonLabelWeb.L_DonneesClick(Sender: TObject);
begin
  L_Accueil.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAccueil]);
  L_Param.Caption   := Format(Html, [ColorInactiveText, EmptyStr, CaptionParam]);
  L_Donnees.Caption := Format(Html, [ColorActiveText, AttributUnderline, CaptionDonnees]);
  L_APropos.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAPropos]);
end;

procedure TFormBoutonLabelWeb.L_AProposClick(Sender: TObject);
begin
  L_Accueil.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionAccueil]);
  L_Param.Caption   := Format(Html, [ColorInactiveText, EmptyStr, CaptionParam]);
  L_Donnees.Caption := Format(Html, [ColorInactiveText, EmptyStr, CaptionDonnees]);
  L_APropos.Caption := Format(Html, [ColorActiveText, AttributUnderline, CaptionAPropos]);
end;

end.

À l’exécution, le label cliqué se colore et se souligne pendant que ses voisins rentrent sagement dans le rang. De quoi bricoler des menus VCL bien plus modernes — voire avec effets et animations — grâce à la HTML Component Library.

En vidéo

Le DFM pour tester

À coller dans une fiche vide pour reproduire l’exemple :

object FormBoutonLabelWeb: TFormBoutonLabelWeb
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu]
  BorderStyle = bsSingle
  Caption = 'Bouton Label Web'
  ClientHeight = 315
  ClientWidth = 483
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  Position = poScreenCenter
  OnCreate = FormCreate
  TextHeight = 15
  object PanelAffichageColonnes: TPanel
    Left = 0
    Top = 0
    Width = 483
    Height = 40
    Align = alTop
    ShowCaption = False
    TabOrder = 0
    object L_Accueil: THtLabel
      AlignWithMargins = True
      Left = 11
      Top = 9
      Width = 51
      Height = 23
      Margins.Left = 10
      Margins.Top = 8
      Margins.Bottom = 7
      Align = alLeft
      AutoSize = False
      Caption = 'Accueil'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -16
      Font.Name = 'Segoe UI'
      Font.Style = []
      ParentColor = False
      ParentFont = False
      Transparent = True
      OnClick = L_AccueilClick
    end
    object L_Param: THtLabel
      AlignWithMargins = True
      Left = 75
      Top = 9
      Width = 92
      Height = 23
      Margins.Left = 10
      Margins.Top = 8
      Margins.Bottom = 7
      Align = alLeft
      AutoSize = False
      Caption = 'Param'#233'trage'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -16
      Font.Name = 'Segoe UI'
      Font.Style = []
      ParentColor = False
      ParentFont = False
      Transparent = True
      OnClick = L_ParamClick
    end
    object L_Donnees: THtLabel
      AlignWithMargins = True
      Left = 180
      Top = 9
      Width = 149
      Height = 23
      Margins.Left = 10
      Margins.Top = 8
      Margins.Bottom = 7
      Align = alLeft
      AutoSize = False
      Caption = 'Gestion des donn'#233'es'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -16
      Font.Name = 'Segoe UI'
      Font.Style = []
      ParentColor = False
      ParentFont = False
      Transparent = True
      OnClick = L_DonneesClick
    end
    object L_APropos: THtLabel
      AlignWithMargins = True
      Left = 342
      Top = 9
      Width = 65
      Height = 23
      Margins.Left = 10
      Margins.Top = 8
      Margins.Bottom = 7
      Align = alLeft
      AutoSize = False
      Caption = 'A propos'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -16
      Font.Name = 'Segoe UI'
      Font.Style = []
      ParentColor = False
      ParentFont = False
      Transparent = True
      OnClick = L_AProposClick
    end
  end
end

Les sources complètes de cet exemple sont disponibles sur GitHub.

Rechercher

Rubriques
Archives
Par mois
Par année