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

RichEdit

开发平台:

Delphi

  1. /*******************************************************}
  2. {                                                       }
  3. {       RichView                                        }
  4. {       Demo: drawing RVF fields in TDBGrid             }
  5. {                                                       }
  6. {       Copyright (c) Sergey Tkachenko                  }
  7. {       svt@trichview.com                               }
  8. {       http://www.trichview.com                        }
  9. {                                                       }
  10. {*******************************************************/
  11. /*
  12. Warning 1: RichView does not support loading text and paragraph styles
  13.   from RVF under C++Builder 1, so this demo will not work properly in C++Builder 1
  14. Warning 2: This demo uses a hack to change row heighs. It will cause glitches
  15.   in drawing edit buttons in older versions of C++Builder
  16. */
  17. //---------------------------------------------------------------------------
  18. #include <vclvcl.h>
  19. #pragma hdrstop
  20. #include "Unit1.h"
  21. #include "Unit2.h"
  22. //---------------------------------------------------------------------------
  23. #pragma link "Grids"
  24. #pragma link "RVReport"
  25. #pragma link "PtblRV"
  26. #pragma link "RVStyle"
  27. #pragma resource "*.dfm"
  28. TfrmMain *frmMain;
  29. //---------------------------------------------------------------------------
  30. __fastcall TfrmMain::TfrmMain(TComponent* Owner)
  31. : TForm(Owner)
  32. {
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TfrmMain::FormCreate(TObject *Sender)
  36. {
  37.   // Initializing RVReportHelper's properties
  38.   RVReportHelper1->RichView->Style = RVStyle1;
  39.   RVReportHelper1->RichView->Options << rvoTagsArePChars;
  40.   // Allowing editing RVF fields
  41.   for (int i = 0; i<DBGrid1->Columns->Count; i++)
  42.     if (IsRVFField(DBGrid1->Columns->Items[i]->Field))
  43.       DBGrid1->Columns->Items[i]->ButtonStyle = cbsEllipsis;
  44.   DefRowHeight =((TDrawGrid*)DBGrid1)->DefaultRowHeight;
  45.   SetRowHeight();
  46. }
  47. //---------------------------------------------------------------------------
  48. void TfrmMain::SetRowHeight()
  49. {
  50.   // A hack to change DBGrid row heights. Is it possible without hacks?
  51.   ((TDrawGrid*)DBGrid1)->DefaultRowHeight = 100;
  52.   ((TDrawGrid*)DBGrid1)->RowHeights[0] = DefRowHeight;
  53. }
  54. //---------------------------------------------------------------------------
  55. void MakeSelected(TRVReportHelper* rvh)
  56. {
  57.   for (int i=0; i<rvh->RichView->Style->TextStyles->Count; i++)
  58.     rvh->RichView->Style->TextStyles->Items[i]->Color = clHighlightText;
  59.   rvh->RichView->Color = clHighlight;
  60. }
  61. //---------------------------------------------------------------------------
  62. // Drawing RVF field on Canvas at Rect using rvh.
  63. void DrawRVFField(TBlobField* field, TCanvas* Canvas, TRect Rect,
  64.   TRVReportHelper* rvh, bool Selected)
  65. {
  66.   try
  67.   {
  68.    rvh->RichView->Clear();
  69.    rvh->RichView->Color = clWindow;
  70.    TMemoryStream*Stream = new TMemoryStream;
  71.    field->SaveToStream(Stream);
  72.    Stream->Position = 0;
  73.    rvh->RichView->LoadRVFFromStream(Stream);
  74.    delete Stream;
  75.    Graphics::TBitmap*bmp = new Graphics::TBitmap;
  76.    bmp->Width = Rect.Right-Rect.Left;
  77.    bmp->Height = Rect.Bottom-Rect.Top;
  78.    rvh->Init(bmp->Canvas, bmp->Width);
  79.    rvh->FormatNextPage(1000);
  80.    if (Selected)
  81.      MakeSelected(rvh);
  82.    if (rvh->PagesCount>0)
  83.      rvh->DrawPage(1, bmp->Canvas, true, bmp->Height);
  84.    Canvas->Draw(Rect.Left, Rect.Top, bmp);
  85.    delete bmp;
  86.   }
  87.   catch(...)
  88.   {
  89.   }
  90. }
  91. //---------------------------------------------------------------------------
  92. // Drawing DBGrid RVF cell
  93. void __fastcall TfrmMain::DBGrid1DrawColumnCell(TObject *Sender,
  94. const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State)
  95. {
  96.   if (IsRVFField(Column->Field))
  97.     DrawRVFField((TBlobField*)(Column->Field), DBGrid1->Canvas, Rect,
  98.       RVReportHelper1, State.Contains(gdSelected));
  99. }
  100. //---------------------------------------------------------------------------
  101. // Editing
  102. void __fastcall TfrmMain::DBGrid1EditButtonClick(TObject *Sender)
  103. {
  104.   if (!IsRVFField(DBGrid1->SelectedField))
  105.     return;
  106.   TMemoryStream* Stream = new TMemoryStream;
  107.    ((TBlobField*)(DBGrid1->SelectedField))->SaveToStream(Stream);
  108.    Stream->Position = 0;
  109.    frmEdit->RichViewEdit1->LoadRVFFromStream(Stream);
  110.    frmEdit->RichViewEdit1->Format();
  111.    delete Stream;
  112.   frmEdit->ActiveControl = frmEdit->RichViewEdit1;
  113.   if (frmEdit->ShowModal()==mrOk)
  114.   {
  115.     Table1->Edit();
  116.     TMemoryStream*Stream = new TMemoryStream;
  117.     frmEdit->RichViewEdit1->SaveRVFToStream(Stream, false);
  118.     Stream->Position = 0;
  119.     ((TBlobField*)(DBGrid1->SelectedField))->LoadFromStream(Stream);
  120.     delete Stream;
  121.   }
  122. }
  123. //---------------------------------------------------------------------------
  124. // Is this field a RVF field?
  125. bool TfrmMain::IsRVFField(TField* Field)
  126. {
  127.   return Field->FieldName=="Data";
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TfrmMain::CheckBox1Click(TObject *Sender)
  131. {
  132.   TDBGridOptions Options = DBGrid1->Options;
  133.   if (CheckBox1->Checked)
  134.     Options >> dgEditing;
  135.   else
  136.     Options << dgEditing;
  137.   DBGrid1->Options = Options;
  138.   SetRowHeight();
  139. }
  140. //---------------------------------------------------------------------------