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

RichEdit

开发平台:

Delphi

  1. //---------------------------------------------------------------------------
  2. #include <vclvcl.h>
  3. #include <stdlib.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. #include "CreateGraphics.hpp"
  7. //---------------------------------------------------------------------------
  8. #pragma link "RVEdit"
  9. #pragma link "RichView"
  10. #pragma link "RVScroll"
  11. #pragma link "RVStyle"
  12. #pragma resource "*.dfm"
  13. TForm1 *Form1;
  14. //---------------------------------------------------------------------------
  15. __fastcall TForm1::TForm1(TComponent* Owner)
  16. : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TForm1::FormCreate(TObject *Sender)
  21. {
  22.   Randomize();
  23.   RichViewEdit1->LoadRVF(ExtractFilePath(Application->ExeName)+"demo.rvf");
  24.   RichViewEdit1->Format();
  25. }
  26. //---------------------------------------------------------------------------
  27. // This event occurs when reading RVF files.
  28. // Image file name is stored in the Name parameter.
  29. // This event load this image from the Images subdirectory.
  30. void __fastcall TForm1::RichViewEdit1RVFPictureNeeded(TCustomRichView *Sender,
  31. AnsiString Name, int Tag, TGraphic *&gr)
  32. {
  33.   Name = ExtractFilePath(Application->ExeName)+"Images\"+Name;
  34.   TPicture* pic = new TPicture;
  35.   try
  36.   {
  37.     try
  38.     {
  39.       pic->LoadFromFile(Name);
  40.     }
  41.     catch(...)
  42.     {
  43.       pic->Assign(RVStyle1->InvalidPicture);
  44.     }
  45.     gr = GetGraphicCopy(pic->Graphic); // from CreateGraphics unit
  46.     gr->Assign(pic->Graphic);
  47.   }
  48.   catch(...)
  49.   {
  50.   }
  51.   delete pic;
  52. }
  53. //---------------------------------------------------------------------------
  54. // Inserting image.
  55. // If this image is not from the Images subdirectory, copying it there
  56. // (under an unique file name)
  57. void __fastcall TForm1::Button1Click(TObject *Sender)
  58. {
  59.   if (OpenDialog1->Execute())
  60.     try
  61.     {
  62.       TPicture* pic = new TPicture;
  63.       try
  64.       {
  65.         pic->LoadFromFile(OpenDialog1->FileName);
  66.         TGraphic* gr = GetGraphicCopy(pic->Graphic); // from CreateGraphics unit
  67.         gr->Assign(pic->Graphic);
  68.         AnsiString ImageName = ExtractFileName(CopyImageToTheImagesDir(OpenDialog1->FileName, NULL));
  69.         RichViewEdit1->InsertPicture(ImageName, gr, rvvaBaseline);
  70.       }
  71.       catch(...)
  72.       {
  73.       }
  74.       delete pic;
  75.     }
  76.     catch(...)
  77.     {
  78.       Application->MessageBox("Image loading error", "Error", 0);
  79.     }
  80. }
  81. //---------------------------------------------------------------------------
  82. // Copying the file ImageFileName to the images subdirectory (if gr==NULL)
  83. // or saving gr in the images subdirectory.
  84. // Assigning an unique file name.
  85. AnsiString TForm1::CopyImageToTheImagesDir(AnsiString ImageFileName, TGraphic* gr)
  86. {
  87.   AnsiString ImagesDir, NewImageFileName, ImageExt;
  88.   int RandomValue;
  89.   ImageFileName = AnsiLowerCase(ImageFileName);
  90.   ImagesDir = AnsiLowerCase(ExtractFilePath(Application->ExeName)+"Images\");
  91.   if (ImageFileName.Pos(ImagesDir)!=1)
  92.   {
  93.     NewImageFileName = ImagesDir+ExtractFileName(ImageFileName);
  94.     if (FileExists(NewImageFileName))
  95.     {
  96.       ImageExt = ExtractFileExt(NewImageFileName);
  97.       NewImageFileName = NewImageFileName.SubString(1, NewImageFileName.Length()-ImageExt.Length());
  98.       RandomValue = random(MaxInt);
  99.       while (FileExists(NewImageFileName+IntToStr(RandomValue)+ImageExt))
  100.         RandomValue++;
  101.       NewImageFileName = NewImageFileName+IntToStr(RandomValue)+ImageExt;
  102.     }
  103.     if (!gr)
  104.       CopyFile(ImageFileName.c_str(), NewImageFileName.c_str(), false);
  105.     else
  106.       gr->SaveToFile(NewImageFileName);
  107.     return NewImageFileName;
  108.   }
  109.   else
  110.     return ImageFileName;
  111. }
  112. //---------------------------------------------------------------------------
  113. // Saving all images that not in the images directory
  114. // Such images can appear when loading or pasting files with images
  115. void TForm1::SaveAllUnknownImages(TCustomRVData* RVData)
  116. {
  117.   for (int i=0; i<RVData->ItemCount; i++)
  118.     switch (RVData->GetItemStyle(i))
  119.     {
  120.       case rvsPicture:
  121.       case rvsHotPicture:
  122.       {
  123.         AnsiString ImageFileName = ExtractFilePath(Application->ExeName)+"Images\"+RVData->GetItemText(i);
  124.         if (!FileExists(ImageFileName))
  125.         {
  126.           TRVVAlign VAlign;
  127.           TGraphic* gr;
  128.           AnsiString s, Ext;
  129.           RVData->GetPictureInfo(i, s, gr, VAlign, Tag);
  130.           Ext = GetGraphicExtension(gr); // from CreateGraphics unit
  131.           RVData->SetItemText(i, ExtractFileName(CopyImageToTheImagesDir("Image."+Ext, gr)));
  132.         }
  133.         break;
  134.       }
  135.       case rvsTable:
  136.       {
  137.         TRVTableItemInfo* table = (TRVTableItemInfo*)(RVData->GetItem(i));
  138.         for (int r=0; r<table->Rows->Count; r++)
  139.           for (int c=0; c<table->Rows->Items[r]->Count; c++)
  140.             if (table->Cells[r][c])
  141.               SaveAllUnknownImages(table->Cells[r][c]->GetRVData());
  142.         break;
  143.       }
  144.     }
  145. }
  146. //---------------------------------------------------------------------------
  147. // Before copying to the clipboard
  148. void __fastcall TForm1::RichViewEdit1Copy(TObject *Sender)
  149. {
  150.   SaveAllUnknownImages(RichViewEdit1->RVData);
  151. }
  152. //---------------------------------------------------------------------------
  153. // Loading doc
  154. void __fastcall TForm1::Button3Click(TObject *Sender)
  155. {
  156.   if (OpenDialog2->Execute())
  157.   {
  158.     if (!RichViewEdit1->LoadRVF(OpenDialog2->FileName))
  159.       Application->MessageBox("Document loading error", "Error", 0);
  160.     RichViewEdit1->Format();
  161.   }
  162. }
  163. //---------------------------------------------------------------------------
  164. void __fastcall TForm1::Button2Click(TObject *Sender)
  165. {
  166.   if (SaveDialog1->Execute())
  167.   {
  168.     SaveAllUnknownImages(RichViewEdit1->RVData);
  169.     if (!RichViewEdit1->SaveRVF(SaveDialog1->FileName, false))
  170.       Application->MessageBox("Document saving error", "Error", 0);
  171.   }
  172. }
  173. //---------------------------------------------------------------------------