📄 wpcreatedemotext.pas
字号:
unit WPCreateDemoText;
{ ------------------------------------------------------------------------------
This unit creates a demo text in a given TWPRichText component.
It works directly with the TParagraph object which makes it
very fast.
Please read this code carefully since it shows how to
set various paragraph attributes and also how to work
with character attributes.
------------------------------------------------------------------------------ }
interface
uses Graphics, WPCtrMemo, WPRTEDefs;
type TWPCreateDemoText = (wpNewText, wpAppendText, wpInsertText);
procedure CreateDemoText(RTFMemo : TWPCustomRtfEdit; Mode : TWPCreateDemoText);
implementation
procedure CreateDemoText(RTFMemo : TWPCustomRtfEdit; Mode : TWPCreateDemoText);
var par: TParagraph; // The current paragraph or table
row, cell: TParagraph; // the current rows and cells
cha: TWPStoredCharAttrInterface;
HeadlineA, TextA, RedTextA: Cardinal;
procedure CreateEmptyPar; // Create an empty paragraph
begin
par := par.AppendNewPar(true);
par.LoadedCharAttr := TextA; // Default attribute for empty paragraphs
end;
// -------------------------------------------------------------------------
procedure SetColoredBorder(ThisPar: TParagraph);
begin
ThisPar.ASet(WPAT_BorderFlags, WPBRD_DRAW_All4); // all lines
ThisPar.ASet(WPAT_BorderWidth, WPCentimeterToTwips(0.05)); // 0,5 mm
ThisPar.ASetColor(WPAT_BorderColorL, clRed); // Left
ThisPar.ASetColor(WPAT_BorderColorT, clGreen); // Top
ThisPar.ASetColor(WPAT_BorderColorR, clBlue); // Right
ThisPar.ASetColor(WPAT_BorderColorB, clYellow); // Bottom
end;
begin
// Clear and set page size and margins
if Mode=wpNewText then
begin
RTFMemo.Clear;
RTFMemo.Header.SetPageWH(
WPCentimeterToTwips(15),
WPCentimeterToTwips(29.7),
WPCentimeterToTwips(1.5),
WPCentimeterToTwips(1.5),
WPCentimeterToTwips(1.5),
WPCentimeterToTwips(1.5)
);
RTFMemo.CheckHasBody;
par := RTFMemo.FirstPar;
end
else if Mode=wpInsertText then
begin
par := RTFMemo.InsertPar;
end
else if Mode=wpAppendText then
begin
par := RTFMemo.ActiveText.LastPar;
CreateEmptyPar;
end;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Create the attributes!
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cha := RTFMemo.HeaderFooter.RTFProps.AttrHelper;
cha.Clear;
cha.SetFontName('Times New Roman');
cha.SetFontSize(12);
HeadlineA := cha.CharAttr;
cha.Clear;
cha.SetFontName('Arial');
cha.SetFontSize(11);
TextA := cha.CharAttr;
cha.SetColor(clRed);
RedTextA := cha.CharAttr;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ PART ONE
// +++++ CREATE PARAGRAPHS
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set the text of the first paragraph
par.SetText('A paragraph with a green background:', HeadlineA);
// append a new, clean paragraph and set attributes
par := par.AppendNewPar(true);
par.ASetColor(WPAT_BGColor, clGreen);
par.ASet(WPAT_ShadingValue, 30);
par.SetText('This is 30 % green', TextA);
// *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
// This code uses the *paragraph attributes* to set the font
// *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
par := par.AppendNewPar(true);
par.ASetColor(WPAT_BGColor, clGreen);
par.ASet(WPAT_ShadingValue, 30);
par.SetText('This is also 30 % green', 0); // Default!
par.ASetFontName('Times New Roman');
par.ASet(WPAT_CharFontSize, 1250); // 12.5pt
par.ASetCharStyle(true, WPSTY_ITALIC);
par.ASetCharStyle(true, WPSTY_UNDERLINE);
CreateEmptyPar;
// ---------------------------------------------------------------------------
// a new headline
par := par.AppendNewPar(true);
par.SetText('A paragraph with a red background and borders:', HeadlineA);
// append a new, clean paragraph and set attributes
par := par.AppendNewPar(true);
// Indents
par.ASet(WPAT_IndentLeft, WPCentimeterToTwips(2));
par.ASet(WPAT_IndentRight, WPCentimeterToTwips(2));
// Border
par.ASet(WPAT_BorderFlags,
WPBRD_DRAW_Left + WPBRD_DRAW_Right + WPBRD_DRAW_Top + WPBRD_DRAW_Bottom); // = WPBRD_DRAW_All4
par.ASet(WPAT_BorderWidth, WPCentimeterToTwips(0.05)); // 0,5 mm
par.ASetColor(WPAT_BGColor, clRed);
par.ASet(WPAT_ShadingValue, 50);
par.SetText('This is 50 % red, indented', TextA);
CreateEmptyPar;
// ---------------------------------------------------------------------------
par := par.AppendNewPar(true);
par.SetText('A paragraph with colored borders and spacing', HeadlineA);
par := par.AppendNewPar(true);
par.ASet(WPAT_SpaceBefore, WPCentimeterToTwips(0.1)); // 1 mm padding
par.ASet(WPAT_SpaceAfter, WPCentimeterToTwips(0.1)); // 1 mm padding
par.ASet(WPAT_IndentLeft, WPCentimeterToTwips(0.1)); // 1 mm padding
par.ASet(WPAT_SpaceBefore, WPCentimeterToTwips(0.1)); // 1 mm padding
SetColoredBorder(par);
par.SetText('Red, Green, Blue, Yellow', TextA);
CreateEmptyPar;
// ---------------------------------------------------------------------------
par := par.AppendNewPar(true);
par.SetText('A paragraph with tabstops', HeadlineA);
par := par.AppendNewPar(true);
par.TabstopAdd(WPCentimeterToTwips(3), tkLeft);
par.TabstopAdd(WPCentimeterToTwips(9), tkRight, tkDots);
par.TabstopAdd(WPCentimeterToTwips(12), tkRight, tkNoFill);
par.Append('START', RedTextA);
par.Append(#9 + 'from' + #9 + 'to' + #9, TextA);
par.Append('END', RedTextA);
CreateEmptyPar;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ PART TWO
// +++++ CREATE A HORIZONTAL LINE
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CreateEmptyPar;
with par.AppendNewObject(wpobjHorizontalLine) do
begin
IParam := clGreen;
// Width := WPCentimeterToTwips(10);
CParam := -WPCentimeterToTwips(1);
end;
CreateEmptyPar;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ PART THREE
// +++++ CREATE TABLES
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
par := par.AppendNewPar(true);
par.SetText('A table with colored borders', HeadlineA);
par := par.AppendNewTable; // a new table
par.ASet(WPAT_BoxWidth, WPCentimeterToTwips(9)); // 9 cm wide
par.ASet(WPAT_BoxMarginLeft, WPCentimeterToTwips(2)); // 2 cm from left margin
row := par.AppendNewRow(true); // the first row
row.ASet(WPAT_PaddingLeft, WPCentimeterToTwips(0.2)); // 2 mm padding
row.ASet(WPAT_PaddingRight, WPCentimeterToTwips(0.2)); // 2 mm padding
row.ASet(WPAT_PaddingTop, WPCentimeterToTwips(0.2)); // 2 mm padding
row.ASet(WPAT_PaddingBottom, WPCentimeterToTwips(0.2)); // 2 mm padding
cell := row.AppendNewCell;
SetColoredBorder(cell);
cell.SetText('Cell A', RedTextA);
cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(3)); // 3 cm wide
cell := cell.AppendNewCell(false); // new cell, same colors
cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(3)); // 3 cm wide
cell.SetText('Cell B', 0);
// *+*+*+*+*+*+*+*+* Alternative: Use Par Attributes
cell.ASetFontName('Times New Roman');
cell.ASet(WPAT_CharFontSize, 950); // 9.5pt
cell.ASetCharStyle(true, WPSTY_ITALIC);
cell.ASetCharStyle(true, WPSTY_UNDERLINE);
cell := cell.AppendNewCell(false); // new cell, same colors
cell.ADelAllFromTo(WPAT_FirstCharAttr, WPAT_LastCharAttr);
cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(3)); // 3 cm wide
cell.SetText('Line 1', TextA);
cell.AppendNewPar(true).SetText('Line 2',TextA);
// and a second row
row := row.AppendNewRow(true); // the first row
cell := row.AppendNewCell;
cell.ASet(WPAT_BorderFlags, WPBRD_DRAW_All4);
cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(9));
cell.SetText('second cell', HeadlineA);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RTFMemo.DelayedReformat;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -