Nov
06
|
|
DelphiW 2010 ajoute encore de nouveaux composants à la VCLW…
Parmi ceux-ci, on trouve un clavier visuel, permettant d’étendre les capacités de ses applications en ayant la possibilité de le proposer dans son interface, très pratique sur des périphériques à écrans tactilesW
Depuis la palette d’outils de l’environnement de conception de Delphi, dans la catégorie Tactile, on trouve le composant TTouchKeyboard
. Il suffit simplement de l’ajouter dans la fiche de son choix.
Voici son aspect par défaut, avec un OSW en français :
Le clavier tactile s’adapte automatiquement à celui du système d’exploitation : On utilise un Windows en français, on a un clavier AZERTYW. On est sous un Windows anglais, on a un QWERTYW.
On peut personnaliser le clavier en modifiant ses propriétés directement depuis l’inspecteur d’objets. Mais trouvant le design par défaut trop joli, je vais dans le code ci-dessous, montrer comment surcharger une méthode de ce clavier pour redéfinir l’apparence des différentes touches.
Pour cela, j’ai besoin de Delphi 2010, actuellement téléchargeable depuis le site d’Embarcadero.

Application Fiches VCL – Delphi.
Puis on place un composant
TTouchKeyboard
sur la fiche.
On peut ensuite aller dans le code afin d’ajouter les types et fonctions nécessaires.
J’ai trouvé un code sur un blog japonais qui m’a servi de base pour l’implémentation et comprendre le fonctionnement.
Le principe est simple, on va hériter de la classe représentant une touche du clavier, afin de pouvoir surcharger sa procédure Paint. On peut ainsi la redessiner à sa guise.
Voici la première version :
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 | unit main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Keyboard, KeyboardTypes, Math; type TTempTouchKeyboard = class(TCustomTouchKeyboard); TMyKeyboardButton = class(TCustomKeyboardButton) public procedure Paint(Canvas: TCustomCanvas = nil); override; end; type TfrmMain = class(TForm) TouchKeyboard1: TTouchKeyboard; procedure FormCreate(Sender: TObject); end; var frmMain: TfrmMain; implementation {$R *.dfm} procedure TMyKeyboardButton.Paint(Canvas: TCustomCanvas); var LRect: TRect; LCanvas: TCanvas; LCaption: String; const //TDrawState = (dsNormal, dsPressed, dsDisabled); acKeyColors: array[TDrawState] of TColor = (clWhite, clYellow, clGray); procedure DrawOneChar(rSize: TRect; cWhere: TCanvas; sWhat: string); begin rSize.Left := rSize.Left + (((rSize.Right-rSize.Left) - cWhere.TextWidth(sWhat)) div 2); rSize.Top := rSize.Top + (((rSize.Bottom-rSize.Top) - cWhere.TextHeight(sWhat)) div 2); cWhere.TextOut(rSize.Left, rSize.Top, sWhat); end; function GetOverrideCaption(Keyboard: TCustomTouchKeyboard; const Key: TVirtualKey; var Caption: string): Boolean; begin if Keyboard.CaptionOverrides.HasCaption(Key.PublishedName) then begin Caption := Keyboard.CaptionOverrides.GetCaption(Key.PublishedName); Exit(True); end else if Keyboard.CaptionOverrides.HasCaption(Key.PublishedName) then begin Caption := Keyboard.CaptionOverrides.GetCaption(Key.PublishedName); Exit(True); end; Result := False; end; begin if Canvas <> nil then LCanvas := Canvas as TCanvas else LCanvas := TTempTouchKeyboard(Parent).Canvas; LRect := ClientRect; LCanvas.Font.Name := 'Arial'; LCanvas.Pen.Color := clBlack; LCanvas.Font.Color := clBlack; LCanvas.Font.Style := [fsBold]; LCanvas.Brush.Color := acKeyColors[State]; LCanvas.Rectangle(LRect); case KeyImage of kiOverride: begin if not GetOverrideCaption(Parent, Key, LCaption) then LCaption := Caption; DrawOneChar(LRect, LCanvas, LCaption); end; kiText: begin if ((Length(Caption) > 0) and ((Caption[1] = '^') or (Caption[1] = '¨'))) then Caption := Caption[1]; DrawOneChar(LRect, LCanvas, Caption); end; kiTab: DrawOneChar(LRect, LCanvas, 'Tabulation'); kiShift: DrawOneChar(LRect, LCanvas, 'Majuscule'); kiEnter: DrawOneChar(LRect, LCanvas, 'Entrée'); kiBackspace: DrawOneChar(LRect, LCanvas, '←'); kiUp: DrawOneChar(LRect, LCanvas, '↑'); kiDown: DrawOneChar(LRect, LCanvas, '↓'); kiLeft: DrawOneChar(LRect, LCanvas, '←'); kiRight: DrawOneChar(LRect, LCanvas, '→'); kiTallEnter: DrawOneChar(LRect, LCanvas, 'Entrée'); end; end; procedure TfrmMain.FormCreate(Sender: TObject); begin TouchKeyboard1.DefaultButtonClass := TMyKeyboardButton; TTempTouchKeyboard(TouchKeyboard1).CreateKeyboard(); TouchKeyboard1.Redraw; end; end. |
(idea) On remarquera l’utilisation de chaînes UnicodeW, ← ↑ ↓ ← →, afin de dessiner les touches pour déplacer le curseur et le retour-arrière. Mon fichier source a par conséquent été sauvegardé au format Unicode.
On obtient ainsi ce splendide clavier
Sur la page suivante, nous allons reprendre ce code pour ajouter un dégradé sur chaque touche…
Pings: Openway : Représentant unique de la marque Codegear en France » Personnalisation du TTouchKeyboard de Delphi 2010