手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

讲述如何开发一个控件,很有价值(三)

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

blank.rtf - empty -so I could see the "plain" header line

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman Times New Roman;}}
{\colortbl\red0\green0\blue0;}\deflang1033\pard\plain\f2\fs20 \par }

plaintext.rtf - too see how having any text was handled

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman Times New Roman;}}{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f2\fs20 this is plain text
\par }

difffont.rtf - different font, same size, same text

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman Times New Roman;}{\f3\fswiss\fprq2 Arial;}}{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f3\fs20 plain text different font\plain\f2\fs20
\par }

diffsize.rtf - text set to 18 point in the default font

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman Times New Roman;}}{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f2\fs36 plain text different font\plain\f2\fs20
\par }

diffcolor.rtf - etc. my favourite of course - blue.

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman Times New Roman;}}{\colortbl\red0\green0\blue0;\red0\green0\blue255;}
\deflang1033\pard\plain\f2\fs20\cf1 plain text different font\plain\f2\fs20
\par }

Looking at the resultant codes you see how the RTF stream is formatted. It comprises a:

  1. INITIAL HEADER (\rtf1\.....)
  2. FONTTABLE (\f0\fswiss...)
  3. COLORTABLE (\colortbl)
  4. MISCELLANEOUS
  5. DEFAULT FORMAT (\pard....)
  6. BODY OF THE FILE.

As a result of that I rewrote this code:

WriteToBuffer(''''{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS
SansSerif;}{\f1\froman\fcharset2 Symbol;}{\f2\fmodern Courier New;}}'''' #13 #10);
WriteToBuffer(''''{\colortbl\red0\green0\blue0;}'''' #13 #10);
WriteToBuffer(''''\deflang1033\pard\plain\f2\fs20 '''');

to become:

WriteToBuffer(''''{\rtf1\ansi\deff0\deftab720'''');
WriteFontTable;
WriteColorTable;
WriteToBuffer(''''\deflang1033\pard\plain\f0\fs20 '''');

The procedures Write[Font,Color]Table basically creates a table of fonts/colors we can reference later on. Each Font and Color type is stored by index in a TList internally. It acts as a lookup tables - by matching the Font name or Color value we can find the [num] to code into the RTF stream at the required moment:

\f[num] = the index of which Font you want to use, as pre-set in the "on the fly" font table
\fs[num] = point size - (for example 20 = 10point)
\cf[num] = the index of which Color to use, as preset in "on the fly" color table
\cb[num] = which background color to use - (ignored in RichEdit version 2.0)

PROBLEM#2 Crashes in long comments or text (existing problem)

There is a bug in ScanForRtf. Can you see it?

procedure TPasConversion.AllocStrBuff;
begin

FStrBuffSize:= FStrBuffSize 1024;
ReAllocMem(FStrBuff, FStrBuffSize);
FStrBuffEnd:= FStrBuff 1023;

end; { AllocStrBuff }

procedure TPasConversion.ScanForRtf;
var
i: Integer;
begin

RunStr:= FStrBuff;
FStrBuffEnd:= FStrBuff 1023;

for i:=1 to TokenLen do
begin
Case TokenStr[i] of
''''\'''', ''''{'''', ''''}'''':
begin
RunStr^:= ''''\'''';
inc(RunStr);
end
end;

if RunStr >= FStrBuffEnd then AllocStrBuff;
RunStr^:= TokenStr[i];

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!