📄 calctu.pas
字号:
unit CalcTU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WPTblCalc, WPRTEDefs, WPCTRMemo, WPCTRRich, ExtCtrls, WPEval,
StdCtrls, WPTbar;
type
TWPTableCalc = class(TForm)
WPRichText1: TWPRichText;
Panel1: TPanel;
AplusBDemo: TButton;
Recalc: TButton;
AverageDemo: TButton;
InvoiceDemo: TButton;
WPToolBar1: TWPToolBar;
DisplayParNames: TCheckBox;
ListNames: TButton;
ListCommands: TButton;
ListBox1: TListBox;
CreateNamedTable: TButton;
ModifyNamedTable: TButton;
Bevel1: TBevel;
ModifyNamedTable2: TButton;
Button1: TButton;
Button2: TButton;
Bevel2: TBevel;
CreateLongTable: TButton;
IncEdit: TEdit;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure AplusBDemoClick(Sender: TObject);
procedure RecalcClick(Sender: TObject);
procedure Panel1Resize(Sender: TObject);
procedure AverageDemoClick(Sender: TObject);
procedure InvoiceDemoClick(Sender: TObject);
procedure DisplayParNamesClick(Sender: TObject);
procedure ListNamesClick(Sender: TObject);
procedure ListCommandsClick(Sender: TObject);
procedure CreateNamedTableClick(Sender: TObject);
procedure ModifyNamedTableClick(Sender: TObject);
procedure ModifyNamedTable2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure CreateLongTableClick(Sender: TObject);
private
procedure AverageDemoCell(RowNr, ColNr: Integer; par: TParagraph);
procedure InvoiceDemoCell(RowNr, ColNr: Integer; par: TParagraph);
procedure LongTableDemoCell(RowNr, ColNr: Integer; par: TParagraph);
public
WPFormulaInterface1 : TWPFormulaInterface;
WPEvalEngine1 : TWPEvalEngine;
Increment, Total,Part : Extended;
HiddenText : Cardinal;
procedure DoTextObjectPaintCalcEvent(Sender: TObject;
pobj: TWPTextObj; par : TParagraph;
page : TWPVirtPage;
pagenr : Integer;
textbody : TWPRTFDataBlock;
var ResultText : WideString;
PaintCanvas : TCanvas; var UseIt : Boolean);
end;
var
WPTableCalc: TWPTableCalc;
implementation
{$R *.dfm}
procedure TWPTableCalc.FormCreate(Sender: TObject);
begin
WPFormulaInterface1 := TWPFormulaInterface.Create(self);
WPEvalEngine1 := TWPEvalEngine.Create(Self);
WPFormulaInterface1.EvalEngine := WPEvalEngine1;
WPRichText1.ProtectedProp := [ppParProtected];
WPFormulaInterface1.OnTextObjectPaintCalc := DoTextObjectPaintCalcEvent;
WPRichText1.FormatOptions := [wpfDontBreakTableRows,wpDisableSpeedReformat];
end;
procedure TWPTableCalc.FormDestroy(Sender: TObject);
begin
WPFormulaInterface1.Free;
WPEvalEngine1.Free;
end;
procedure TWPTableCalc.RecalcClick(Sender: TObject);
begin
WPRichText1.RecalcText(true,true);
end;
procedure TWPTableCalc.Panel1Resize(Sender: TObject);
begin
Recalc.Left := Width-Recalc.Width-20;
end;
procedure TWPTableCalc.DisplayParNamesClick(Sender: TObject);
begin
if DisplayParNames.Checked then
WPRichText1.ViewOptions := WPRichText1.ViewOptions + [wpShowParCalcNames,wpShowParCalcCommands]
else WPRichText1.ViewOptions := WPRichText1.ViewOptions - [wpShowParCalcNames,wpShowParCalcCommands];
end;
procedure TWPTableCalc.ListNamesClick(Sender: TObject);
begin
ListBox1.Items.Clear;
WPRichText1.HeaderFooter.ListNamesAndCommands(
[wpListParCalcNames,wpListTxtObjectNames], ListBox1.Items);
end;
procedure TWPTableCalc.ListCommandsClick(Sender: TObject);
begin
ListBox1.Items.Clear;
WPRichText1.HeaderFooter.ListNamesAndCommands(
[wpListParCommands,wpListTxtObjectCommands], ListBox1.Items);
end;
procedure TWPTableCalc.AplusBDemoClick(Sender: TObject);
var par : TParagraph;
begin
WPRichText1.Clear;
WPRichText1.CheckHasBody;
par := WPRichText1.ActiveText.AppendPar(nil, nil);
par.SetText('Value A');
par.ASet(WPAT_ParProtected,1);
par.ASetBorderFlags(WPBRD_DRAW_Bottom);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText(FloatToStr(1));
par.ASetStringProp(WPAT_PAR_NAME,'PAR_A');
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText('Value B');
par.ASet(WPAT_ParProtected,1);
par.ASetBorderFlags(WPBRD_DRAW_Bottom);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText(FloatToStr(2));
par.ASetStringProp(WPAT_PAR_NAME,'PAR_B');
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText('Sum A + B');
par.ASet(WPAT_ParProtected,1);
par.ASetBorderFlags(WPBRD_DRAW_Bottom);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText(FloatToStr(0));
par.ASetStringProp(WPAT_PAR_COMMAND,'PAR_A+PAR_B');
par.ASetCharStyle(true, WPSTY_BOLD);
par.ASet(WPAT_ParProtected,1);
// ---------------------------------------------------------------------------
WPRichText1.RecalcText(true,true);
end;
procedure TWPTableCalc.AverageDemoCell(RowNr, ColNr: Integer; par: TParagraph);
begin
par.SetText(FloatToStr(Random(1000)));
par.ASetStringProp(WPAT_PAR_NAME,'PAR_B');
end;
procedure TWPTableCalc.AverageDemoClick(Sender: TObject);
var par : TParagraph;
begin
WPRichText1.Clear;
WPRichText1.CheckHasBody;
par := WPRichText1.ActiveParagraph;
par.SetText('Calculate average of all values in this table:');
par.ASet(WPAT_ParProtected,1);
par.ASetBorderFlags(WPBRD_DRAW_Bottom);
// ---------------------------------------------------------------------------
par := WPRichText1.TableAdd(4,5,[wptblActivateBorders,wptblAppendTableAtEnd],nil, AverageDemoCell);
// ---------------------------------------------------------------------------
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText('Cell Count');
par.ASet(WPAT_ParProtected,1);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText(FloatToStr(0));
par.ASetStringProp(WPAT_PAR_COMMAND,'valcount(PAR_B)');
par.ASetCharStyle(true, WPSTY_BOLD);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText('Sum');
par.ASet(WPAT_ParProtected,1);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText(FloatToStr(0));
par.ASetStringProp(WPAT_PAR_COMMAND,'(PAR_B)');
par.ASetCharStyle(true, WPSTY_BOLD);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText('Average');
par.ASet(WPAT_ParProtected,1);
par := WPRichText1.ActiveText.AppendPar(nil, par);
par.SetText(FloatToStr(0));
par.ASetStringProp(WPAT_PAR_COMMAND,'average(PAR_B)');
par.ASetCharStyle(true, WPSTY_BOLD);
// ---------------------------------------------------------------------------
WPRichText1.RecalcText(true,true);
end;
procedure TWPTableCalc.InvoiceDemoCell(RowNr, ColNr: Integer; par: TParagraph);
const prods : array[1..5] of string =
( 'Cool', 'Master', 'Hummer', 'High Performace', 'Better' );
begin
// Set the widths of the rows
case ColNr of
1 : par.ASet(WPAT_COLWIDTH_PC, 500); // % * 100 !
2 : par.ASet(WPAT_COLWIDTH_PC, 2500);
else
begin
par.ASet(WPAT_COLWIDTH_PC, 1400);
par.ASet(WPAT_Alignment, Integer(paralRight));
end;
end;
// Set the text and the cell names and commands
if RowNr=1 then // Header Row ------------------------------
begin
case ColNr of
1 : ;
2 : par.SetText('Product');
3 : par.SetText('Price');
4 : par.SetText('Amount');
5 : par.SetText('net');
6 : par.SetText('+VAT');
7 : par.SetText('total');
end;
par.ASetColor(WPAT_FGColor, $A0A0A0);
par.ASet(WPAT_ParProtected,1);
par.ASet(WPAT_Alignment, Integer(paralCenter));
end else
if RowNr=7 then // Footer Row ------------------------------
begin
par.ADel(WPAT_BorderWidth); // Delete the border width for ALL linese
par.ASet(WPAT_BorderWidthT, 40); // ANd set the top line to 40 twips
par.ASetAdd( WPAT_BorderFlags, WPBRD_DRAW_Top); // Add a flag!
// par.ParentRow.ASet(WPAT_BoxMinHeight, WPCentimeterToTwips(1.5));
par.ASet(WPAT_SpaceBefore, WPCentimeterToTwips(0.3));
par.ASet(WPAT_SpaceAfter, WPCentimeterToTwips(0.3));
par.ASet(WPAT_ParProtected,1);
case ColNr of
1 : ;
2 : ;
3 : ;
4 : ;
5 : begin
par.ASetStringProp(WPAT_PAR_COMMAND, 'PAR_NET');
end;
6 : begin
par.ASetStringProp(WPAT_PAR_COMMAND, 'PAR_VAT');
end;
7 : begin
par.ASetStringProp(WPAT_PAR_COMMAND, 'PAR_TOTAL');
par.ASetCharStyle(true, WPSTY_BOLD);
end;
end;
end else // Data Rows ------------------------------
begin
case ColNr of
1 : begin
par.SetText(IntToStr(RowNr-1));
par.ASet(WPAT_ParProtected,1);
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -