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

RichEdit

开发平台:

Delphi

  1. //---------------------------------------------------------------------------
  2. #include <vclvcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. #include "PreviewFrm.h"
  6. //---------------------------------------------------------------------------
  7. #pragma link "RVEdit"
  8. #pragma link "RichView"
  9. #pragma link "RVScroll"
  10. #pragma link "RVStyle"
  11. #pragma link "PtblRV"
  12. #pragma resource "*.dfm"
  13. TForm1 *Form1;
  14. //---------------------------------------------------------------------------
  15. __fastcall TForm1::TForm1(TComponent* Owner)
  16. : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. bool TForm1::IsEmpty(TCustomRichViewEdit* rve)
  21. {
  22.   return rve->ItemCount==0 ||
  23.     (rve->ItemCount==1 && rve->GetItemStyle(0)>=0 && rve->GetItemText(0)=="");
  24. }
  25. //---------------------------------------------------------------------------
  26. void TForm1::PreparePrinting()
  27. {
  28.   RVPrint1->AssignSource(RichViewEdit1);
  29.   if (!IsEmpty(RichViewEdit2))
  30.   {
  31.     if (StrToIntDef(txtHeader->Text,-1)<0)
  32.       txtHeader->Text = "10";
  33.     RVPrint1->SetHeader(RichViewEdit2->RVData);
  34.   }
  35.   else
  36.     RVPrint1->SetHeader(NULL);
  37.   if (!IsEmpty(RichViewEdit3))
  38.   {
  39.     if (StrToIntDef(txtFooter->Text,-1)<0)
  40.       txtFooter->Text = "10";
  41.     RVPrint1->SetFooter(RichViewEdit3->RVData);
  42.   }
  43.   else
  44.     RVPrint1->SetFooter(NULL);
  45.   RVPrint1->LeftMarginMM   = StrToIntDef(txtLeft->Text,0);
  46.   RVPrint1->RightMarginMM  = StrToIntDef(txtRight->Text,0);
  47.   RVPrint1->TopMarginMM    = StrToIntDef(txtTop->Text,0);
  48.   RVPrint1->BottomMarginMM = StrToIntDef(txtBottom->Text,0);
  49.   RVPrint1->HeaderYMM      = StrToIntDef(txtHeader->Text,0);
  50.   RVPrint1->FooterYMM      = StrToIntDef(txtFooter->Text,0);
  51.   RVPrint1->FormatPages(TRVDisplayOptions());
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TForm1::New1Click(TObject *Sender)
  55. {
  56.   RichViewEdit1->Clear();
  57.   RichViewEdit2->Clear();
  58.   RichViewEdit3->Clear();
  59.   RichViewEdit1->DeleteUnusedStyles(true,true,true);
  60.   RichViewEdit2->DeleteUnusedStyles(true,true,true);
  61.   RichViewEdit3->DeleteUnusedStyles(true,true,true);
  62.   RichViewEdit1->Format();
  63.   RichViewEdit2->Format();
  64.   RichViewEdit3->Format();
  65. }
  66. //---------------------------------------------------------------------------
  67. void TForm1::OpenFile(const AnsiString FileName)
  68. {
  69.   RichViewEdit1->Clear();
  70.   RichViewEdit2->Clear();
  71.   RichViewEdit3->Clear();
  72.   RichViewEdit1->DeleteUnusedStyles(true,true,true);
  73.   RichViewEdit2->DeleteUnusedStyles(true,true,true);
  74.   RichViewEdit3->DeleteUnusedStyles(true,true,true);
  75.   RichViewEdit1->RTFReadProperties->SetHeader(RichViewEdit2->RVData);
  76.   RichViewEdit1->RTFReadProperties->SetFooter(RichViewEdit3->RVData);
  77.   RichViewEdit1->LoadRTF(FileName);
  78.   RichViewEdit1->Format();
  79.   RichViewEdit2->Format();
  80.   RichViewEdit3->Format();
  81.   txtHeader->Text = IntToStr(RichViewEdit1->RTFReadProperties->HeaderYMM);
  82.   txtFooter->Text = IntToStr(RichViewEdit1->RTFReadProperties->FooterYMM);
  83. }
  84. void __fastcall TForm1::FormCreate(TObject *Sender)
  85. {
  86.   OpenFile(ExtractFilePath(Application->ExeName)+"doc.rtf");
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TForm1::Open1Click(TObject *Sender)
  90. {
  91.   if (OpenDialog1->Execute())
  92.     OpenFile(OpenDialog1->FileName);
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall TForm1::PrintPreview1Click(TObject *Sender)
  96. {
  97.   PreparePrinting();
  98.   TfrmPreview* frm = new TfrmPreview(Application);
  99.   frm->rvpp->RVPrint = RVPrint1;
  100.   frm->Button1Click(NULL);
  101.   frm->ShowModal();
  102.   delete frm;
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TForm1::Print1Click(TObject *Sender)
  106. {
  107. #if __BORLANDC__ > 0x520
  108.   bool PrintIt = psd->Execute();
  109. #else
  110.   bool PrintIt = true;
  111. #endif
  112.   if (PrintIt)
  113.   {
  114.     RVPrint1->AssignSource(RichViewEdit1);
  115.     RVPrint1->FormatPages(TRVDisplayOptions());
  116.     if (RVPrint1->PagesCount>0)
  117.       RVPrint1->Print("RichView Edit Demo",1,false);
  118.   }
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TForm1::Exit1Click(TObject *Sender)
  122. {
  123.   Close();
  124. }
  125. //---------------------------------------------------------------------------