Unit1.pas
上传用户:daoqigc
上传日期:2021-04-20
资源大小:2795k
文件大小:6k
源码类别:

RichEdit

开发平台:

Delphi

  1. {-------------------------------------------------------------------------------
  2.   Working with RVF files containing shared images.
  3.   This demo stores them in the special subdirectory, but you can store them
  4.   in a database, etc.
  5.   The main setting - rvfoSavePicturesBody is EXCLUDED from
  6.   RichViewEdit1.RVFOptions
  7. -------------------------------------------------------------------------------}
  8. unit Unit1;
  9. interface
  10. uses
  11.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  12.   Dialogs, RVStyle, RVScroll, RichView, RVEdit, RVFuncs, StdCtrls,
  13.   CRVData, RVTable, RVItem;
  14. type
  15.   TForm1 = class(TForm)
  16.     RichViewEdit1: TRichViewEdit;
  17.     RVStyle1: TRVStyle;
  18.     Button1: TButton;
  19.     OpenDialog1: TOpenDialog;
  20.     Button2: TButton;
  21.     Button3: TButton;
  22.     OpenDialog2: TOpenDialog;
  23.     SaveDialog1: TSaveDialog;
  24.     procedure RichViewEdit1RVFPictureNeeded(Sender: TCustomRichView;
  25.       Name: String; Tag: Integer; var gr: TGraphic);
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure Button3Click(Sender: TObject);
  29.     procedure Button2Click(Sender: TObject);
  30.     procedure RichViewEdit1Copy(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.     function CopyImageToTheImagesDir(ImageFileName: String; gr: TGraphic): String;
  34.     procedure SaveAllUnknownImages(RVData: TCustomRVData);
  35.   public
  36.     { Public declarations }
  37.   end;
  38. var
  39.   Form1: TForm1;
  40. implementation
  41. {$R *.dfm}
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. begin
  44.   Randomize;
  45.   RichViewEdit1.LoadRVF(ExtractFilePath(Application.ExeName)+'demo.rvf');
  46.   RichViewEdit1.Format;
  47. end;
  48. // This event occurs when reading RVF files.
  49. // Image file name is stored in the Name parameter.
  50. // This event load this image from the Images subdirectory.
  51. procedure TForm1.RichViewEdit1RVFPictureNeeded(Sender: TCustomRichView;
  52.   Name: String; Tag: Integer; var gr: TGraphic);
  53. var pic: TPicture;
  54. begin
  55.   Name := ExtractFilePath(Application.ExeName)+'Images'+Name;
  56.   pic := TPicture.Create;
  57.   try
  58.     try
  59.       pic.LoadFromFile(Name);
  60.     except
  61.       pic.Assign(RVStyle1.InvalidPicture);
  62.     end;
  63.     gr := RV_CreateGraphics(TGraphicClass(pic.Graphic.ClassType));
  64.     gr.Assign(pic.Graphic);
  65.   finally
  66.     pic.Free;
  67.   end;
  68. end;
  69. // Inserting image.
  70. // If this image is not from the Images subdirectory, copying it there
  71. // (under an unique file name)
  72. procedure TForm1.Button1Click(Sender: TObject);
  73. var pic: TPicture;
  74.     gr: TGraphic;
  75.     ImageName: String;
  76. begin
  77.   if OpenDialog1.Execute then begin
  78.     try
  79.       pic := TPicture.Create;
  80.       try
  81.         pic.LoadFromFile(OpenDialog1.FileName);
  82.         gr := RV_CreateGraphics(TGraphicClass(pic.Graphic.ClassType));
  83.         gr.Assign(pic.Graphic);
  84.         ImageName := ExtractFileName(CopyImageToTheImagesDir(OpenDialog1.FileName, nil));
  85.         RichViewEdit1.InsertPicture(ImageName, gr, rvvaBaseline);
  86.       finally
  87.         pic.Free;
  88.       end;
  89.     except
  90.       Application.MessageBox('Image loading error', 'Error', 0);
  91.     end;
  92.   end;
  93. end;
  94. // Copying the file ImageFileName to the images subdirectory (if gr=nil)
  95. // or saving gr in the images subdirectory.
  96. // Assigning an unique file name.
  97. function TForm1.CopyImageToTheImagesDir(ImageFileName: String; gr: TGraphic): String;
  98. var ImagesDir, NewImageFileName, ImageExt: String;
  99.     RandomValue: Integer;
  100. begin
  101.   ImageFileName := AnsiLowerCase(ImageFileName);
  102.   ImagesDir := AnsiLowerCase(ExtractFilePath(Application.ExeName)+'Images');
  103.   if Pos(ImagesDir,ImageFileName)<>1 then begin
  104.     NewImageFileName := ImagesDir+ExtractFileName(ImageFileName);
  105.     if FileExists(NewImageFileName) then begin
  106.       ImageExt := ExtractFileExt(NewImageFileName);
  107.       NewImageFileName := Copy(NewImageFileName, 1, Length(NewImageFileName)-Length(ImageExt));
  108.       RandomValue := Random(MaxInt);
  109.       while FileExists(NewImageFileName+IntToStr(RandomValue)+ImageExt) do
  110.         inc(RandomValue);
  111.       NewImageFileName := NewImageFileName+IntToStr(RandomValue)+ImageExt;
  112.     end;
  113.     if gr=nil then
  114.       CopyFile(PChar(ImageFileName), PChar(NewImageFileName), False)
  115.     else
  116.       gr.SaveToFile(NewImageFileName);
  117.     Result := NewImageFileName;
  118.     end
  119.   else
  120.     Result := ImageFileName;
  121. end;
  122. // Saving all images that not in the images directory
  123. // Such images can appear when loading or pasting files with images 
  124. procedure TForm1.SaveAllUnknownImages(RVData: TCustomRVData);
  125. var i,r,c, Tag: Integer;
  126.     VAlign: TRVVAlign;
  127.     table: TRVTableItemInfo;
  128.     gr: TGraphic;
  129.     s, ImageFileName, Ext: String;
  130. begin
  131.   for i := 0 to RVData.ItemCount-1 do
  132.     case RVData.GetItemStyle(i) of
  133.       rvsPicture, rvsHotPicture:
  134.         begin
  135.           ImageFileName := ExtractFilePath(Application.ExeName)+'Images'+RVData.GetItemText(i);
  136.           if not (FileExists(ImageFileName)) then begin
  137.             RVData.GetPictureInfo(i, s, gr, VAlign, Tag);
  138.             Ext := GraphicExtension(TGraphicClass(gr.ClassType));
  139.             RVData.SetItemText(i, ExtractFileName(CopyImageToTheImagesDir('Image.'+Ext, gr)));
  140.           end;
  141.         end;
  142.       rvsTable:
  143.         begin
  144.           table := TRVTableItemInfo(RVData.GetItem(i));
  145.           for r := 0 to table.Rows.Count-1 do
  146.             for c := 0 to table.Rows[r].Count-1 do
  147.               if table.Cells[r,c]<>nil then
  148.                 SaveAllUnknownImages(table.Cells[r,c].GetRVData);
  149.         end;
  150.     end;
  151. end;
  152. // Before copying to the clipboard
  153. procedure TForm1.RichViewEdit1Copy(Sender: TObject);
  154. begin
  155.   SaveAllUnknownImages(RichViewEdit1.RVData);
  156. end;
  157. // Loading doc
  158. procedure TForm1.Button3Click(Sender: TObject);
  159. begin
  160.   if OpenDialog2.Execute then begin
  161.     if not RichViewEdit1.LoadRVF(OpenDialog2.FileName) then
  162.       Application.MessageBox('Document loading error', 'Error', 0);
  163.     RichViewEdit1.Format;
  164.   end;
  165. end;
  166. // Saving doc
  167. procedure TForm1.Button2Click(Sender: TObject);
  168. begin
  169.   if SaveDialog1.Execute then begin
  170.     SaveAllUnknownImages(RichViewEdit1.RVData);
  171.     if not RichViewEdit1.SaveRVF(SaveDialog1.FileName, False) then
  172.       Application.MessageBox('Document saving error', 'Error', 0);
  173.   end;
  174. end;
  175. end.