📄 styleunit1.pas
字号:
unit StyleUnit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WPRTEDefs, WPCTRMemo, WPCTRRich, ExtCtrls, ComCtrls,
WPUtil, WPIOCSS, WPTbar, WP1Style, WPPanel, WPAction, WPCTRStyleCol, WPStyles,
Buttons, ActnList, WPRuler;
type
TStyleDemo = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Panel1: TPanel;
WPRichText1: TWPRichText;
InitTextA: TButton;
Label1: TLabel;
Label2: TLabel;
WPValueEdit1: TWPValueEdit;
CSS1: TMemo;
GetCSS1: TButton;
SetCSS1: TButton;
WPToolBar1: TWPToolBar;
WPRichText2: TWPRichText;
Label3: TLabel;
Label4: TLabel;
WPValueEdit2: TWPValueEdit;
Label5: TLabel;
ComboBox1: TComboBox;
Button1: TButton;
WPStyleCollection1: TWPStyleCollection;
ActionList1: TActionList;
WPToolButton1: TWPToolButton;
WPRuler1: TWPRuler;
WPVertRuler1: TWPVertRuler;
SaveFile: TButton;
TestRTF: TButton;
TestWPT: TButton;
ComboBox2: TComboBox;
Label6: TLabel;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Bevel1: TBevel;
procedure InitTextAClick(Sender: TObject);
procedure WPValueEdit1Change(Sender: TObject);
procedure GetCSS1Click(Sender: TObject);
procedure SetCSS1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure WPValueEdit2Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure WPRichText2OpenDialog(Sender: TObject;
DiaType: TWPCustomRtfEditDialog; var ResultValue: Boolean);
procedure FormShow(Sender: TObject);
procedure WPRuler1DblClick(Sender: TObject);
procedure SaveFileClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TestRTFClick(Sender: TObject);
procedure TestWPTClick(Sender: TObject);
procedure ComboBox2Change(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
StyleDemo: TStyleDemo;
implementation
uses WPRTEPaint;
{$R *.dfm}
procedure TStyleDemo.FormCreate(Sender: TObject);
begin
ComboBox1.Items := Screen.Fonts;
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('Times New Roman');
ComboBox2.Items := Screen.Fonts;
ComboBox2.ItemIndex := ComboBox1.Items.IndexOf('Verdana');
end;
procedure TStyleDemo.ComboBox1Change(Sender: TObject);
begin
WPRichText1.DefaultAttr.SetFontName(ComboBox1.Text);
WPRichText1.ReformatAll(true);
WPRichText1.Invalidate;
end;
procedure TStyleDemo.WPValueEdit2Change(Sender: TObject);
begin
WPRichText1.DefaultAttr.SetFontSize(WPValueEdit2.Value / 20);
WPRichText1.ReformatAll(true);
WPRichText1.Invalidate;
end;
procedure TStyleDemo.InitTextAClick(Sender: TObject);
var par: TParagraph;
sty: TWPTextStyle;
pos: Integer;
spanobj_open, spanobj_close: TWPTextObj;
ca: TWPCharAttr;
begin
WPRichText1.DefaultAttr.SetFontName(ComboBox1.Text);
WPRichText1.DefaultAttr.SetFontSize(WPValueEdit2.Value / 20);
WPRichText1.Clear;
// Set BODY
WPRichText1.ActiveText := WPRichText1.BodyText;
// ---------------------------------------------------- FIRST LINE -----------
// We use the DefaultAttr ----------------------------------------------------
par := WPRichText1.ActiveText.AppendPar;
par.SetText('1) This line uses no style - It uses WPRichText1.DefaultAttr !');
// ---------------------------------------------------- SECOND LINE ----------
// We use the default Style for the complete paragraph -----------------------
// Creates Style
sty := WPRichText1.ParStyles.AddStyle('FIRST');
// Create Paragraph
par := WPRichText1.ActiveText.AppendPar;
par.SetText('2) This line uses the FIRST style');
// Assign Style
par.ABaseStyle := sty;
// Set Props
sty.ASetAddCharStyle(WPSTY_BOLD);
sty.ASetFontName('Verdana');
sty.ASet(WPAT_CharFontSize, 13 * 100);
// ---------------------------------------------------- THIRD LINE -----------
// We embedd a SPAN style ----------------------------------------------------
// Creates Style
sty := WPRichText1.ParStyles.AddStyle('SECOND');
// Set Props
sty.ASetAddCharStyle(WPSTY_ITALIC);
sty.ASet(WPAT_CharFontSize, 10 * 100);
// Create Paragraph
par := WPRichText1.ActiveText.AppendPar;
pos := par.Insert(0, '3) SECOND STYLE - except for ', 0);
spanobj_open := par.InsertNewObject(pos, wpobjSPANStyle, true, false);
spanobj_open.StyleName := 'FIRST';
inc(pos);
pos := par.Insert(pos, '<SPAN "FIRST">', 0);
spanobj_close := par.InsertNewObject(pos, wpobjSPANStyle, true, true);
spanobj_close.SetTag(spanobj_open.NewTag); //<-- tags are used to link start with end!
inc(pos);
par.Insert(pos, ' - END', 0);
par.ABaseStyleName := 'SECOND';
// ----------------------------------------------------- 4th LINE ------------
// We use character attributes -----------------------------------------------
// 'ca' is used as buffer to create a char attribute
par := WPRichText1.ActiveText.AppendPar;
FillChar(ca, SizeOf(ca), 0);
pos := par.Insert(0, '4) DEFAULT "FIRST" ', 0);
par.RTFProps.AttrInterface.SetCharStyles(ca, WPSTY_ITALIC, WPSTY_ITALIC);
par.RTFProps.AttrInterface.SetFontSize(ca, 12);
pos := par.Insert(pos, '12ptITALIC ', ca);
FillChar(ca, SizeOf(ca), 0);
par.RTFProps.AttrInterface.SetFontName(ca, 'Courier New');
par.RTFProps.AttrInterface.SetFontSize(ca, 10);
par.RTFProps.AttrInterface.SetColor(ca, clGreen);
pos := par.Insert(pos, '10ptGreenCourier ', ca);
FillChar(ca, SizeOf(ca), 0);
par.RTFProps.AttrInterface.SetColor(ca, clBlue);
par.Insert(pos, 'blue text', ca);
// Assign the FIRST Style
par.ABaseStyleName := 'FIRST';
// ----------------------------------------------------- last LINE --------
// We use a character style -----------------------------------------------
// 'ca' is used as buffer to create a char attribute
par := WPRichText1.ActiveText.AppendPar;
FillChar(ca, SizeOf(ca), 0);
pos := par.Insert(0, '5) default ', 0);
par.RTFProps.AttrInterface.SetCharStyleSheet(ca,
par.RTFProps.ParStyles.GetID('FIRST'));
pos := par.Insert(pos, 'with chararacter-style FIRST', ca);
par.Insert(pos, ' default (RTF does not preserve character styles)', 0);
end;
procedure TStyleDemo.WPValueEdit1Change(Sender: TObject);
var sty: TWPTextStyle;
begin
sty := WPRichText1.ParStyles.FindTextStyle('FIRST');
if sty <> nil then
sty.ASet(WPAT_CharFontSize, Round(WPValueEdit1.Value / 20 * 100));
WPRichText1.ReformatAll(true); // Initialize because we changed the font
WPRichText1.Repaint;
end;
procedure TStyleDemo.GetCSS1Click(Sender: TObject);
var sty: TWPTextStyle;
begin
sty := WPRichText1.ParStyles.FindTextStyle('FIRST');
if sty <> nil then
CSS1.text := sty.AGet_CSS(true, false, true);
end;
procedure TStyleDemo.SetCSS1Click(Sender: TObject);
var sty: TWPTextStyle;
parser: TWPCSSParserStyleWP;
begin
sty := WPRichText1.ParStyles.FindTextStyle('FIRST');
if sty <> nil then
begin
parser := TWPCSSParserStyleWP.Create;
try
parser.AsString := CSS1.text;
parser.ApplyToStyle(sty);
WPRichText1.ReformatAll(true,true); // Initialize because we changed the font
finally
parser.Free;
end;
end;
end;
procedure TStyleDemo.ComboBox2Change(Sender: TObject);
var sty: TWPTextStyle;
begin
sty := WPRichText1.ParStyles.FindTextStyle('FIRST');
if sty <> nil then
begin
sty.ASetFontName(ComboBox2.Text);
WPRichText1.ReformatAll(true,true);
end;
end;
procedure TStyleDemo.Button1Click(Sender: TObject);
begin
with TWPStyleDlg.Create(Self) do
begin
EditBox := WPRichtext2;
Execute;
Free;
end;
end;
procedure TStyleDemo.WPRichText2OpenDialog(Sender: TObject;
DiaType: TWPCustomRtfEditDialog; var ResultValue: Boolean);
begin
WPStyleOpenDialog(Self, Sender,DiaType,ResultValue);
end;
procedure TStyleDemo.FormShow(Sender: TObject);
begin
// Assign the stored styles to the editor
WPStyleCollection1.Update;
end;
procedure TStyleDemo.WPRuler1DblClick(Sender: TObject);
begin
WPRuler1.Design := TWPRulerDesign((Integer(WPRuler1.Design) + 1)
mod (Integer(High(TWPRulerDesign))+1));
end;
procedure TStyleDemo.SaveFileClick(Sender: TObject);
begin
WPRichText1.SaveAs();
end;
procedure TStyleDemo.Button2Click(Sender: TObject);
begin
Memo1.Text := WPRichText1.ParStyles.GetWPCSS;
//WPRichText1.ParStyles.SaveToFile('c:\a.wpcss');
end;
procedure TStyleDemo.Button3Click(Sender: TObject);
begin
WPRichText1.ParStyles.SetWPCSS(Memo1.Text);
//WPRichText1.ParStyles.LoadFromFile('c:\a.wpcss');
WPRichText1.ReformatAll(true,true);
end;
procedure TStyleDemo.TestRTFClick(Sender: TObject);
begin
WPRichText1.AsString :=
WPRichText1.AsANSIString('RTF');
end;
procedure TStyleDemo.TestWPTClick(Sender: TObject);
begin
WPRichText1.AsString :=
WPRichText1.AsANSIString('WPTOOLS');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -