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

RichEdit

开发平台:

Delphi

  1. unit Unit1;
  2. interface
  3. {$I RV_Defs.inc}
  4. uses
  5.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  6.   Dialogs, Menus, RVScroll, RichView, RVEdit, ComCtrls, RVStyle, PreviewFrm,
  7.   PtblRV, StdCtrls;
  8. type
  9.   TForm1 = class(TForm)
  10.     PageControl1: TPageControl;
  11.     TabSheet1: TTabSheet;
  12.     TabSheet2: TTabSheet;
  13.     TabSheet3: TTabSheet;
  14.     TabSheet4: TTabSheet;
  15.     RichViewEdit1: TRichViewEdit;
  16.     RichViewEdit2: TRichViewEdit;
  17.     RichViewEdit3: TRichViewEdit;
  18.     MainMenu1: TMainMenu;
  19.     File1: TMenuItem;
  20.     Open1: TMenuItem;
  21.     Print1: TMenuItem;
  22.     Exit1: TMenuItem;
  23.     RVStyle1: TRVStyle;
  24.     RVStyle2: TRVStyle;
  25.     RVStyle3: TRVStyle;
  26.     RVPrint1: TRVPrint;
  27.     N1: TMenuItem;
  28.     N2: TMenuItem;
  29.     PrintPreview1: TMenuItem;
  30.     OpenDialog1: TOpenDialog;
  31.     GroupBox1: TGroupBox;
  32.     Label1: TLabel;
  33.     txtLeft: TEdit;
  34.     Label2: TLabel;
  35.     txtRight: TEdit;
  36.     Label3: TLabel;
  37.     txtTop: TEdit;
  38.     Label4: TLabel;
  39.     txtBottom: TEdit;
  40.     GroupBox2: TGroupBox;
  41.     Label5: TLabel;
  42.     Label6: TLabel;
  43.     txtHeader: TEdit;
  44.     txtFooter: TEdit;
  45.     New1: TMenuItem;
  46.     PrinterSetupDialog1: TPrinterSetupDialog;
  47.     procedure PrintPreview1Click(Sender: TObject);
  48.     procedure Open1Click(Sender: TObject);
  49.     procedure FormCreate(Sender: TObject);
  50.     procedure New1Click(Sender: TObject);
  51.     procedure Print1Click(Sender: TObject);
  52.     procedure Exit1Click(Sender: TObject);
  53.   private
  54.     { Private declarations }
  55.     function IsEmpty(rve: TCustomRichViewEdit): Boolean;
  56.     procedure PreparePrinting;
  57.     procedure OpenFile(const FileName: String);    
  58.   public
  59.     { Public declarations }
  60.   end;
  61. var
  62.   Form1: TForm1;
  63. implementation
  64. {$R *.dfm}
  65. function TForm1.IsEmpty(rve: TCustomRichViewEdit): Boolean;
  66. begin
  67.   Result := (rve.ItemCount=0) or ((rve.ItemCount=1) and (rve.GetItemStyle(0)>=0) and
  68.     (rve.GetItemText(0)=''));
  69. end;
  70. procedure TForm1.PreparePrinting;
  71. begin
  72.   RVPrint1.AssignSource(RichViewEdit1);
  73.   if not IsEmpty(RichViewEdit2) then begin
  74.     if StrToIntDef(txtHeader.Text,-1)<0 then
  75.       txtHeader.Text := '10';
  76.     RVPrint1.SetHeader(RichViewEdit2.RVData);
  77.     end
  78.   else
  79.     RVPrint1.SetHeader(nil);
  80.   if not IsEmpty(RichViewEdit3) then begin
  81.     if StrToIntDef(txtFooter.Text,-1)<0 then
  82.       txtFooter.Text := '10';
  83.     RVPrint1.SetFooter(RichViewEdit3.RVData)
  84.     end
  85.   else
  86.     RVPrint1.SetFooter(nil);
  87.   RVPrint1.LeftMarginMM   := StrToIntDef(txtLeft.Text,0);
  88.   RVPrint1.RightMarginMM  := StrToIntDef(txtRight.Text,0);
  89.   RVPrint1.TopMarginMM    := StrToIntDef(txtTop.Text,0);
  90.   RVPrint1.BottomMarginMM := StrToIntDef(txtBottom.Text,0);
  91.   RVPrint1.HeaderYMM      := StrToIntDef(txtHeader.Text,0);
  92.   RVPrint1.FooterYMM      := StrToIntDef(txtFooter.Text,0);
  93.   RVPrint1.FormatPages(rvdoALL);
  94. end;
  95. procedure TForm1.New1Click(Sender: TObject);
  96. begin
  97.   RichViewEdit1.Clear;
  98.   RichViewEdit2.Clear;
  99.   RichViewEdit3.Clear;
  100.   RichViewEdit1.DeleteUnusedStyles(True,True,True);
  101.   RichViewEdit2.DeleteUnusedStyles(True,True,True);
  102.   RichViewEdit3.DeleteUnusedStyles(True,True,True);
  103.   RichViewEdit1.Format;
  104.   RichViewEdit2.Format;
  105.   RichViewEdit3.Format;
  106. end;
  107. procedure TForm1.OpenFile(const FileName: String);
  108. begin
  109.   RichViewEdit1.Clear;
  110.   RichViewEdit2.Clear;
  111.   RichViewEdit3.Clear;
  112.   RichViewEdit1.DeleteUnusedStyles(True,True,True);
  113.   RichViewEdit2.DeleteUnusedStyles(True,True,True);
  114.   RichViewEdit3.DeleteUnusedStyles(True,True,True);
  115.   RichViewEdit1.RTFReadProperties.SetHeader(RichViewEdit2.RVData);
  116.   RichViewEdit1.RTFReadProperties.SetFooter(RichViewEdit3.RVData);
  117.   RichViewEdit1.LoadRTF(FileName);
  118.   RichViewEdit1.Format;
  119.   RichViewEdit2.Format;
  120.   RichViewEdit3.Format;
  121.   txtHeader.Text := IntToStr(RichViewEdit1.RTFReadProperties.HeaderYMM);
  122.   txtFooter.Text := IntToStr(RichViewEdit1.RTFReadProperties.FooterYMM);
  123. end;
  124. procedure TForm1.FormCreate(Sender: TObject);
  125. begin
  126.   OpenFile(ExtractFilePath(Application.ExeName)+'doc.rtf');
  127. end;
  128. procedure TForm1.Open1Click(Sender: TObject);
  129. begin
  130.   if OpenDialog1.Execute then
  131.     OpenFile(OpenDialog1.FileName);
  132. end;
  133. procedure TForm1.PrintPreview1Click(Sender: TObject);
  134. var frm: TfrmPreview;
  135. begin
  136.   PreparePrinting;
  137.   frm := TfrmPreview.Create(Application);
  138.   frm.rvpp.RVPrint := RVPrint1;
  139.   frm.Button1Click(nil);
  140.   frm.ShowModal;
  141.   frm.Free;
  142. end;
  143. procedure TForm1.Print1Click(Sender: TObject);
  144. var PrintIt: Boolean;
  145. begin
  146.   {$IFDEF RICHVIEWDEF3}
  147.   PrintIt := PrinterSetupDialog1.Execute;
  148.   {$ELSE}
  149.   PrintIt := True;
  150.   {$ENDIF}
  151.   if PrintIt then begin
  152.     RVPrint1.AssignSource(RichViewEdit1);
  153.     RVPrint1.FormatPages(rvdoALL);
  154.     if RVPrint1.PagesCount>0 then
  155.       RVPrint1.Print('RichView Edit Demo',1,False);
  156.   end;
  157. end;
  158. procedure TForm1.Exit1Click(Sender: TObject);
  159. begin
  160.   Close;
  161. end;
  162. end.