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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| unit uPiloterRichEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Buttons,
RichEdit,
Printers,
Contnrs;
type
TfrmPiloterRichEdit = class(TForm)
bVersRichEdit: TSpeedButton;
RichEdit1: TRichEdit;
bIndice: TSpeedButton;
bExposant: TSpeedButton;
bImprimer: TSpeedButton;
procedure bVersRichEditClick(Sender: TObject);
procedure bIndiceClick(Sender: TObject);
procedure bExposantClick(Sender: TObject);
procedure bImprimerClick(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
frmPiloterRichEdit: TfrmPiloterRichEdit;
type TCharPosDY = (Indice, Exposant, Normal);
type TMotSelAttr = class
public
mot : string; Name : tFontName; Size : integer; Style : tFontStyles;
Color : tColor; CharPosDY : TCharPosDY;
constructor Create( iMot : string; iName : tFontName;
iSize : integer; iStyle : tFontStyles;
iColor : tColor; iCharPosDY : TCharPosDY);
end;
TListeMotSelAttr = class(TObjectList)
private
function GetItem(Index: Integer): TMotSelAttr;
procedure SetItem(Index: Integer; const Value: TMotSelAttr);
public
function Add(AObject: TMotSelAttr): Integer;
property Items[Index: Integer]: TMotSelAttr read GetItem write SetItem; default;
end;
type TPiloterRichEd = object
RichEdCible : tRichEdit;
MotsEtFormats : TListeMotSelAttr;
procedure Init( iRichEdCible : tRichEdit);
procedure AddFormat(Ligne : string);
//procedure SurlignerSelection(iColor: TColor);
procedure SelectionEnIndiceExposant(CharPosDY : TCharPosDY);
procedure ImprimerTout;
procedure Fini;
end;
implementation
{$R *.DFM}
// --- TMotSelAttr -------------------------------------------------------------
constructor TMotSelAttr.Create( iMot : string; iName : tFontName;
iSize : integer; iStyle : tFontStyles;
iColor : tColor; iCharPosDY : TCharPosDY);
begin Mot :=iMot; Name:=iName; Size:=iSize;
Style:=iStyle; Color:=iColor; CharPosDY:=iCharPosDY;
end;
// --- TListeMotSelAttr --------------------------------------------------------
function TListeMotSelAttr.Add(AObject: TMotSelAttr): Integer;
begin result := inherited Add(AObject); end;
function TListeMotSelAttr.GetItem(Index: Integer): TMotSelAttr;
begin result := TMotSelAttr(inherited GetItem(index)); end;
procedure TListeMotSelAttr.SetItem(Index: Integer; const Value: TMotSelAttr);
begin inherited setItem(index, value); end;
// --- TPiloterRichEd ----------------------------------------------------------
procedure TPiloterRichEd.Init( iRichEdCible : tRichEdit);
begin RichEdCible:=iRichEdCible;
MotsEtFormats:=TListeMotSelAttr.Create(true);
end;
procedure TPiloterRichEd.AddFormat(Ligne : string);
var i,PosDeb,lgFin,PosTrouv : integer;
begin PosDeb:=Length(richEdCible.Text);
with richEdCible do
begin Lines.add(Ligne);
for i:=0 to MotsEtFormats.count-1 do
begin lgFin := Length(Text) - PosDeb;
if SelLength <> 0 then PosDeb := SelStart + SelLength;
PosTrouv:=FindText(MotsEtFormats.items[i].Mot,PosDeb,lgFin,[]); // stWholeWord : incompatible avec indices/exposants
if PosTrouv=-1
then showMessage( 'Mot à formatter : '+MotsEtFormats.items[i].Mot+' : non trouvé'+#13#10
+'vérifier l''ordre des déclarations de formattage des mots')
else begin
SelStart:=PosTrouv;
SelLength:=length(MotsEtFormats.items[i].Mot);
if MotsEtFormats.items[i].CharPosDY<>Normal
then SelectionEnIndiceExposant(MotsEtFormats.items[i].CharPosDY);
with SelAttributes do
begin Name :=MotsEtFormats.items[i].Name;
Size :=MotsEtFormats.items[i].Size;
Style:=MotsEtFormats.items[i].Style;
Color:=MotsEtFormats.items[i].Color;
end;
update;
end;
end;
end;
end;
procedure TPiloterRichEd.SelectionEnIndiceExposant(CharPosDY : TCharPosDY);
var Format: TCharFormat; // infos sur formattage de caractères RichEdit (voir SDK Win)
begin
FillChar(format, SizeOf(Format), 0);
with Format do
begin cbSize := SizeOf(Format);
dwMask := CFM_OFFSET; // Pour positionnement deltaY du caractère
case CharPosDY of
Exposant : yOffset := 55; //positif en twips 1 twip = 1/1440 pouce ou 1/20 point.
Normal : yOffset := 0;
Indice : yOffset := -55; //négatif en twips
end;
end;
// Transfert du formattage des caractères dans la sélection du RichEdit
SendMessage(RichEdCible.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format))
end;
procedure TPiloterRichEd.ImprimerTout;
begin // Ajouter un bout de code por fixer des marges d'impressio
RichEdCible.Print('imprRichEdit');
end;
procedure TPiloterRichEd.Fini;
begin MotsEtFormats.Free;
end;
// ------ Exemples d'Utilisation -----------------------------------------------
var piloteRED : TPiloterRichEd;
procedure TfrmPiloterRichEdit.bVersRichEditClick(Sender: TObject);
var ligneAAjouter : string; i : integer;
begin
piloteRED.Init(RichEdit1);
//----- 1ère ligne
ligneAAjouter:='avec marges additionnelles motA motB motC motD';
with piloteRED.MotsEtFormats do
begin Add(TMotSelAttr.Create('motA','Arial', 10,[fsBold],clRed,Normal));
Add(TMotSelAttr.Create('motD','Comic Sans MS', 10,[fsItalic],clNavy,Normal));
//motD déclaré ici dans l'ordre incorrect d'apparition pour déclecher message d'erreur
Add(TMotSelAttr.Create('motC','Verdana', 9,[],clYellow,Normal));
end;
piloteRED.AddFormat(ligneAAjouter);
// Pour cette ligneAAjouter : On fixe des marges gauche+droite + Alignement
// En l'absence de marges additionnles la méthode Print imprime dans le rectangle
// interne aux marges techniques
with piloteRED.RichEdCible.Paragraph do
begin Alignment:=taLeftJustify;
FirstIndent:=25;
RightIndent:=15;
end;
//----- 2ème ligne avec Wingdings "flèche" et "téléphone"
ligneAAjouter:=chr(240)+' passer un coup de '+chr(40)+' à Nénette';
with piloteRED.MotsEtFormats do
begin Clear;
Add(TMotSelAttr.Create(chr(240),'WingDings', 9,[fsBold],clTeal,Normal));
Add(TMotSelAttr.Create(chr(40),'WingDings', 9,[],clNavy,Normal));
Add(TMotSelAttr.Create('Nénette','Courrier', 12,[fsUnderline],clMaroon,Normal));
end;
piloteRED.AddFormat(ligneAAjouter);
// Pour cette ligneAAjouter : On fixe se contente de centrer le texte :
piloteRED.RichEdCible.Paragraph.Alignment:=taCenter;
//----- 3ème ligne avec indices et exposants
ligneAAjouter:='an.xn +an-1.xn-1 + ... + ap.xp + ... + a0 = 0';
with piloteRED.MotsEtFormats do
begin Clear;
Add(TMotSelAttr.Create('n','Arial', 8,[],clBlack,Indice));
Add(TMotSelAttr.Create('n','Arial', 8,[],clBlack,Exposant));
Add(TMotSelAttr.Create('n-1','Arial', 8,[],clBlack,Indice));
Add(TMotSelAttr.Create('n-1','Arial', 8,[],clBlack,Exposant));
Add(TMotSelAttr.Create('p','Arial', 8,[],clBlack,Indice));
Add(TMotSelAttr.Create('p','Arial', 8,[],clBlack,Exposant));
Add(TMotSelAttr.Create('0','Arial', 8,[],clBlack,Indice));
end;
piloteRED.AddFormat(ligneAAjouter);
piloteRED.RichEdCible.Paragraph.Alignment:=taRightJustify; //<
//----- Nième ligne
ligneAAjouter:='ligne entièrement en évidence';
with piloteRED.MotsEtFormats do
begin Clear;
Add(TMotSelAttr.Create(ligneAAjouter,'Verdana',9,[fsBold],clTeal,Normal));
end;
piloteRED.AddFormat(ligneAAjouter);
//----- N+1ième ligne
ligneAAjouter:='ligne sans mise en évidence';
piloteRED.MotsEtFormats.Clear;
piloteRED.AddFormat(ligneAAjouter);
{ Pour voir la liste des WingDings puis des WebDings : déverrouiller la mise en commentaire
//----- N+2ième ligne WingDings
ligneAAjouter:='';
for i:=33 to 255 do ligneAAjouter:=ligneAAjouter+chr(i);
with piloteRED.MotsEtFormats do
begin Clear;
Add(TMotSelAttr.Create(ligneAAjouter,'WingDings',14,[],clNavy,Normal));
end;
piloteRED.AddFormat(ligneAAjouter+#13#10);
//----- N+2ième ligne WebDings
with piloteRED.MotsEtFormats do
begin Clear;
Add(TMotSelAttr.Create(ligneAAjouter,'WebDings',14,[],clNavy,Normal));
end;
piloteRED.AddFormat(ligneAAjouter); }
//piloteRED.Fini;
end;
// ------- Indice - Exposant sur sélection à la souris :
procedure TfrmPiloterRichEdit.bIndiceClick(Sender: TObject);
begin piloteRED.SelectionEnIndiceExposant(Indice);end;
procedure TfrmPiloterRichEdit.bExposantClick(Sender: TObject);
begin piloteRED.SelectionEnIndiceExposant(exposant); end;
// ------- Imprimer :
procedure TfrmPiloterRichEdit.bImprimerClick(Sender: TObject);
begin if length(piloteRED.richEdCible.Text) > 0
then piloteRED.ImprimerTout;
end;
END. |
Partager