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
|
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtDlgs, StdCtrls, ComCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
bInsererImg: TSpeedButton;
bSauverViderRichEd: TSpeedButton;
bReChargerRichEd: TSpeedButton;
RichEdit1: TRichEdit;
OpenPictureDialog1: TOpenPictureDialog;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
procedure bInsererImgClick(Sender: TObject);
procedure RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure bSauverViderRichEdClick(Sender: TObject);
procedure bReChargerRichEdClick(Sender: TObject);
private
{ Déclarations privées }
procedure InsererImg(nomFichierImg : string; yImg : integer);
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const cls = #13#10; // pour ligne ou paragraphe suivant dans un sms
frdImg = '{i'; // Fanion de repérage des données Img parmi les données texte
var largImg, // Largeur des images fixée à 25% de celle du RichEdit
hautImg, // Hauteur de d'image : calculée pour préserver les proportions
yImg : integer; // Hauteur de positionnement de l'image dans marge de droite
procedure SMS(unMessage : string); begin Showmessage(unMessage); end;
function RepAppli : string; { renvoie Rep avec \ terminal }
begin RepAppli:=ExtractFilePath(Application.ExeName); end;
// < Pour simplifier, le contenu du RichEdit sera sauvé dans le répertoire
// où l'exe de l'Appli est logé
procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin // Récupération du y de positionnement de l'image
yImg:=Y;
end;
procedure TForm1.InsererImg(nomFichierImg : string; yImg : integer);
var plImg : tPanel; img : tImage;
begin largImg:=RichEdit1.width div 4; // Largeur de toutes les images
with RichEdit1 do // Dégagemment de la marge de droite
begin SelectAll;
Paragraph.RightIndent:=largImg;
SelLength:=0;
end;
// Création dynamique d'un Panel qui supportera l'image
plImg:=tPanel.create(form1);
with plImg do // Pré-paramétrage du Panel
begin parent:=form1;
caption:=nomFichierImg; // Pour se souvenir d'où vient le fichier
left:=RichEdit1.left+RichEdit1.width-largImg-5;
top:=yImg;
width:=largImg;
height:=25; // Sera corrigée plus bas
tag:=8888; // Pour distinguer par rapport à d'autres Panels
end;
// Image : Créa dyna + Chargement et ajustement de l'image
Img:=tImage.create(plImg);
with Img do
begin parent:=plImg;
picture.LoadFromFile(nomFichierImg);
left:=0; top:=0;
width:=LargImg;
height:=trunc(largImg*picture.height/picture.width);
Stretch:=true;
end;
plImg.height:=Img.Height;
end;
procedure TForm1.bInsererImgClick(Sender: TObject);
const filtre = 'Fichiers (*.bmp;*.ico;*.wmf;*.jpg)|*.bmp;*.ico;*.wmf;*.jpg';
begin OpenPictureDialog1.Filter := filtre;
If Not (OpenPictureDialog1.Execute) Then
begin sms('Format de fichier image non supporté'); EXIT; end;
// Sinon insertion de l'image à hauteur préalablement pointée par la souris
InsererImg(OpenPictureDialog1.FileName,yImg);
end;
procedure TForm1.bSauverViderRichEdClick(Sender: TObject);
var i : integer; nomFichierImg,syImg : string;
begin i:=0;
while i<=ComponentCount - 1 do
begin if (Components[i] is TPanel)
and ((Components[i] as TPanel).tag=8888) then
begin // Récupération des noms des fichiersImg + yImg
nomFichierImg:=frdImg+(Components[i] as TPanel).caption;
yImg:=(Components[i] as TPanel).top;
syImg:=' y:='+intToStr(yImg); // Chaîne souvenir de yImg
(Components[i] as TPanel).free; // : Libération Panel + Img
// On sauve les syImg et nomFichierImg au début du RichEdit :
RichEdit1.Lines.Insert(0,nomFichierImg+syImg);
end else inc(i);
end;
// On Sauve le contenu du RichEdit :
RichEdit1.Lines.SaveToFile(RepAppli+'TexteImages.rtf');
// On efface le texte du RichEdit
RichEdit1.Lines.clear;
end;
procedure TForm1.bReChargerRichEdClick(Sender: TObject);
var i,po,code : integer; slDImg : tStringList; st,nomFichierImg,syImg : string;
begin OpenDialog1.Filter := 'Fichiers (*.rtf)|*.rtf';
OpenDialog1.Filename:='TexteImages.rtf';
if OpenDialog1.execute then
begin RichEdit1.Lines.LoadFromFile(OpenDialog1.Filename); //'{i'
// On recherche si le RichEdit1 contient des données Img
// repérables avec la constante frdImg au début du RichEdit1 :
slDImg := tStringList.create;
with RichEdit1 do
begin while pos(frdImg,lines[0])=1 do
begin slDImg.Add(lines[0]); // on les récupère dans une liste
lines.Delete(0); // on vire la ligne
end
end;
for i:=0 to slDImg.count-1 do // Traitement des données Img
begin st:=slDImg[i];
Delete(st,1,length(frdImg)); // nettoyage du début de chaine
po:=pos('y:=',st);
syImg:=copy(st,po+3,length(st));
val(syImg,yImg,code); // on récupère yImg
Delete(st,po,length(st)); // nettoyage fin de chaine
nomFichierImg:=trim(st); // et voici le nomFichierImg
InsererImg(nomFichierImg,yImg);// on remet l'image à sa place
end;
slDImg.free;
end;
end;
end. |