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

RichEdit

开发平台:

Delphi

  1. //---------------------------------------------------------------------------
  2. #include <vclvcl.h>
  3. #pragma hdrstop
  4. #include "Unit2.h"
  5. //---------------------------------------------------------------------------
  6. #pragma link "RVEdit"
  7. #pragma link "RichView"
  8. #pragma link "RVScroll"
  9. #pragma resource "*.dfm"
  10. TForm2 *Form2;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm2::TForm2(TComponent* Owner)
  13.     : TForm(Owner)
  14. {
  15. }
  16. #if __BORLANDC__>0x520
  17. // Simple, but useful functions.
  18. // Earlier version of C++Builder do not support TTable.CreateBlobStream...
  19. bool SaveRVFToField(TCustomRichView*rv, TTable* tbl,
  20.                         const AnsiString FieldName)
  21. {
  22.   TStream* Stream = tbl->CreateBlobStream(tbl->FieldByName(FieldName), bmWrite);
  23.   bool r = rv->SaveRVFToStream(Stream, false);
  24.   delete Stream;
  25.   return r;
  26. }
  27. bool LoadRVFFromField(TCustomRichView* rv, TTable* tbl,
  28.                           const AnsiString FieldName)
  29. {
  30.   TStream * Stream = tbl->CreateBlobStream(tbl->FieldByName(FieldName), bmRead);
  31.   bool r = rv->LoadRVFFromStream(Stream);
  32.   delete Stream;
  33.   rv->Format();
  34.   return r;
  35. }
  36. #else
  37. bool SaveRVFToField(TCustomRichView*rv, TTable* tbl,
  38.                         const AnsiString FieldName)
  39. {
  40.   TStream* Stream = new TMemoryStream;
  41.   bool r = rv->SaveRVFToStream(Stream, false);
  42.   Stream->Position = 0;
  43.   ((TBlobField*)(tbl->FieldByName(FieldName)))->LoadFromStream(Stream);
  44.   delete Stream;
  45.   return r;
  46. }
  47. bool LoadRVFFromField(TCustomRichView* rv, TTable* tbl,
  48.                           const AnsiString FieldName)
  49. {
  50.   TStream* Stream = new TMemoryStream;
  51.   ((TBlobField*)(tbl->FieldByName(FieldName)))->SaveToStream(Stream);
  52.   Stream->Position = 0;
  53.   bool r = rv->LoadRVFFromStream(Stream);
  54.   delete Stream;
  55.   rv->Format();
  56.   return r;
  57. }
  58. #endif
  59. //---------------------------------------------------------------------------
  60. void TForm2::SetField(const AnsiString AFieldName, TTable * ATable)
  61. {
  62.   FTable     = ATable;
  63.   FFieldName = AFieldName;
  64.   Load();
  65.   Caption = FTable->FieldByName("Caption")->AsString;
  66. }
  67. //---------------------------------------------------------------------------
  68. void TForm2::Load()
  69. {
  70.   LoadRVFFromField(RichViewEdit1, FTable, FFieldName);
  71.   Modified = false;
  72. }
  73. //---------------------------------------------------------------------------
  74. void TForm2::Save()
  75. {
  76.   FTable->Edit();
  77.   SaveRVFToField(RichViewEdit1, FTable, FFieldName);
  78.   FTable->Post();
  79.   Modified = false;
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TForm2::RichViewEdit1Change(TObject *Sender)
  83. {
  84.   Modified = true;
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TForm2::SetModified(const bool Value)
  88. {
  89.   if (FModified!=Value)
  90.   {
  91.     FModified = Value;
  92.     if (FModified)
  93.       Panel1->Caption = "Modified";
  94.     else
  95.       Panel1->Caption = "";
  96.   }
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm2::btnPostClick(TObject *Sender)
  100. {
  101.   Save();    
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TForm2::btnCancelClick(TObject *Sender)
  105. {
  106.   Load();    
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TForm2::FormCloseQuery(TObject *Sender, bool &CanClose)
  110. {
  111.   if (Modified)
  112.     switch (Application->MessageBox("Save changes?", "Text was modified",
  113.          MB_YESNOCANCEL | MB_ICONQUESTION))
  114.     {
  115.       case IDYES:
  116.         Save();
  117.         CanClose = true;
  118.         break;
  119.       case IDNO:
  120.         CanClose = true;
  121.         break;
  122.       case IDCANCEL:
  123.         CanClose = false;
  124.     }
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm2::btnCloseClick(TObject *Sender)
  128. {
  129.   Close();    
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm2::RichViewEdit1CurTextStyleChanged(TObject *Sender)
  133. {
  134.   btnBold->Down = RichViewEdit1->CurTextStyleNo!=0;    
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TForm2::btnBoldClick(TObject *Sender)
  138. {
  139.   // switching 1-st and 0-th styles
  140.   if (btnBold->Down)
  141.     RichViewEdit1->ApplyTextStyle(1);
  142.   else
  143.     RichViewEdit1->ApplyTextStyle(0);
  144. }
  145. //---------------------------------------------------------------------------