⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtf.htm

📁 对于学习很有帮助
💻 HTM
字号:
<!-- This document was created with HomeSite v2.5 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

<HTML>
<HEAD>
	<TITLE>UDDF - Rich Text Format</TITLE>
	<META NAME="Description" CONTENT="Rich Text Format section of the Delphi Developers FAQ" >
	<META NAME="Keywords" CONTENT="" >

</HEAD>
<BODY  bgcolor="#FFFFFF">
<CENTER>
<IMG SRC="../images/uddf.jpg"> </CENTER>
<HR SIZE="6" COLOR="LIME">

<CENTER><FONT SIZE="7" FACE="Arial Black" COLOR="RED">Rich Text Format</FONT></CENTER>


<H1><A NAME="rtf0">Storing a rich edit text in a db</A></H1>
<I><P>From: Mike Bardill &lt;rrmike@minster.york.ac.uk&gt;</P>
</I><P>Saving a TRichEdit to a file and storing the file is a perfectly good way of saving the data to the table,
but the same can be achieved without an intermediate file by using a TBlobStream. 
The example below is for reading a TRichEdit from a table, but a similar approach 'in reverse' with a bmWrite will save into the table. </P>
<P><HR></P>
<PRE>procedure ReadRichEditFromTable(Table : TTable; var RichEdit : TRichEdit);
var
  BlobStream : TBlobStream;
begin
  try
    BlobStream := TBlobStream.Create(Table.FieldByName('BODY') as TBlobField, bmRead);
    if (not Table.FieldByName('BLOBFieldName').IsNull) then
    begin
      RichEdit.Lines.LoadFromStream (BlobStream);
    end;
  finally
    BlobStream.Free;
  end;
end;</PRE>
<P><HR></P>

<P><H1><A NAME="rtf1">Word Count in Richedit</P></A></H1>
<P><I>From: ksudar@erols.com</I></P>

<PRE>>Does anyone know how to carry out a word count for the delphi richedit
>component ??
</PRE>

Someone posted this a few weeks ago.. I tried it and it seems to work.<P>

<HR><PRE>function GetWord: boolean;
var s: string; {presume no word>255 chars}
     c: char;
begin
result:= false;
s:= ' ';
while not eof(f) do
        begin
        read(f, c);
        if not (c in ['a'..'z','A'..'Z'{,... etcetera}]) then break;
        s:=s+c;
        end;
result:= (s&lt;&gt;' ');
end;
procedure GetWordCount(TextFile: string);
begin
        Count:= 0;
        assignfile(f, TextFile);
        reset(f);
        while not eof(f) do if GetWord then inc(Count);
        closefile(f);
end;
</PRE><HR>

<P><H1><A NAME="rtf2">RichEdit Error with Delphi 2.01 and NT 4</P></A></H1>
<P><I>James V. Bacus &lt;bacuslab@mcs.net&gt;</I></P>


<PRE>
I have written a program that collects information that a user selects, by
a number of checkboxes and buttons, to a non visible RichEdit box.  The
program was written under Windows 95 and works fine.  But under NT 4.0 the
line ...

RichEdit1.Print(''); 

returns a Divide by Zero Error.  The only way I have found round this is to
save the file and use Word to print the final file.

Does anyone have or know of any workrounds?

</PRE>
Yes, I have a solution and a fix...<P>

To fix this problem requires a minor change to the VCL unit ComCtrls.pas.<P>

I've tested this on many different systems running NT 4.0 and Win95, and all
seems to work well now.  It's actually a very simple fix, and here it is...<P>

<HR><PRE>{
A compatibility problem exists with the original RichEdit.Print method
code and the release of NT 4.0.  A EDivByZero exception is caused because
accessing the Printer.Handle property outside of a BeginDoc/EndDoc block
returns an Information Context (IC) handle under NT 4.0 instead of a
Device Context (DC) handle.  The EM_FORMATRANGE attempts to use this IC
instead of a real printer DC, which causes the exception.  If the Handle
property is accessed AFTER the BeginDoc, a true Device Context handle is
returned, and I have modified the code to handle this correctly.  I have
left the original position of BeginDoc in the code but remarked it out to
indicate the difference.    J.V.Bacus 11/12/96
}
procedure TCustomRichEdit.Print(const Caption: string);
var
  Range: TFormatRange;
  LastChar, MaxLen, LogX, LogY: Integer;
begin
  FillChar(Range, SizeOf(TFormatRange), 0);
  with Printer, Range do
  begin
    LogX := GetDeviceCaps(Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(Handle, LOGPIXELSY);
    // The repositioned BeginDoc to now be compatible with
    // both NT 4.0 and Win95
    BeginDoc;
    hdc := Handle;
    hdcTarget := hdc;
    if IsRectEmpty(PageRect) then
    begin
      rc.right := PageWidth * 1440 div LogX;
      rc.bottom := PageHeight * 1440 div LogY;
    end
    else begin
      rc.left := PageRect.Left * 1440 div LogX;
      rc.top := PageRect.Top * 1440 div LogY;
      rc.right := PageRect.Right * 1440 div LogX;
      rc.bottom := PageRect.Bottom * 1440 div LogY;
    end;
    rcPage := rc;
    Title := Caption;
    // The original position of BeginDoc
    { BeginDoc; }
    LastChar := 0;
    MaxLen := GetTextLen;
    chrg.cpMax := -1;
    repeat
      chrg.cpMin := LastChar;
      LastChar := SendMessage(Self.Handle, EM_FORMATRANGE, 1, Longint(@Range));
      if (LastChar &lt; MaxLen) and (LastChar &lt;&gt; -1) then NewPage;
    until (LastChar &gt;= MaxLen) or (LastChar = -1);
    EndDoc;
  end;
  SendMessage(Handle, EM_FORMATRANGE, 0, 0);
end;
</PRE><HR>

<P><H1><A NAME="rtf3">RTF to printer problem</P></A></H1>
<P><I>Carl Steinhilber [carl_steinhilber@eriver.com]</I></P>

<PRE>
&gt; One of my colleagues needs to print an RTF file. The problem is that the
&gt; RTF component that comes with D2 (and all the shareware/commercial
&gt; components we've found) wants to load the entire file before starting to
&gt; print. The file, prepared in advance, may be *very* large and may not fit
&gt; in memory. Quick printing is essential and waiting for the file to load
&gt; just isn't a good option.
</PRE>

One of the solutions I hit
upon that might work for you, particularly since you're
running under Win95, is shelling out to WordPad with an
undocumented feature:<p>

<HR><PRE>  
shellExecute(mainForm.handle,
               nil,
               'write.exe',
               'myfile.rtf /p',
               nil,
               SW_HIDE);
</PRE><HR>

(I found that using the WRITE.EXE stub is a bit more universal
because WORDPAD.EXE isn't always on the path.)<p>

The &quot;/p&quot; parameter is the undocumented feature. It will launch WordPad,
print the file, then close WordPad. And with SW_HIDE, the only thing
you see is the Printing status box.<p>

WordPad probably loads as much as it can into memory before
printing, but it should be able to handle any size file by
segmentation. And WordPad has a pretty small footprint, so it
loads and prints fairly quickly. It's also generally on every
Win95 system.<p>

<P><H1><A NAME="rtf4">translate RTF  to HTML</P></A></H1>
<P><I>From: johan@lindgren.pp.se</I></P>

<PRE>
&GT;   lopezj@iluso.ci.uv.es (Agustin Lopez Bueno) writes:
&GT;  I need translate the contents of a RTF component to HTML
&GT;  with Delphi. Anybody knows how to do this?
</PRE>
This is a routine I use to convert the content of a RichEdit to SGML-code. It does not produce a complete HTML-file but you will 
have to figure out which RTF-codes you should convert to which HTML-tags. <p>

<HR><PRE>function rtf2sgml (text : string) : string;
{Funktion f&ouml;r att konvertera en RTF-rad till SGML-text.}
var
temptext : string;
start : integer;
begin
text := stringreplaceall (text,'&AMP;','##amp;');
text := stringreplaceall (text,'##amp','&AMP;amp');
text := stringreplaceall (text,'\'+chr(39)+'e5','&AMP;aring;');
text := stringreplaceall (text,'\'+chr(39)+'c5','&AMP;Aring;');
text := stringreplaceall (text,'\'+chr(39)+'e4','&AMP;auml;');
text := stringreplaceall (text,'\'+chr(39)+'c4','&AMP;Auml;');
text := stringreplaceall (text,'\'+chr(39)+'f6','&AMP;ouml;');
text := stringreplaceall (text,'\'+chr(39)+'d6','&AMP;Ouml;');
text := stringreplaceall (text,'\'+chr(39)+'e9','&AMP;eacute;');
text := stringreplaceall (text,'\'+chr(39)+'c9','&AMP;Eacute;');
text := stringreplaceall (text,'\'+chr(39)+'e1','&AMP;aacute;');
text := stringreplaceall (text,'\'+chr(39)+'c1','&AMP;Aacute;');
text := stringreplaceall (text,'\'+chr(39)+'e0','&AMP;agrave;');
text := stringreplaceall (text,'\'+chr(39)+'c0','&AMP;Agrave;');
text := stringreplaceall (text,'\'+chr(39)+'f2','&AMP;ograve;');
text := stringreplaceall (text,'\'+chr(39)+'d2','&AMP;Ograve;');
text := stringreplaceall (text,'\'+chr(39)+'fc','&AMP;uuml;');
text := stringreplaceall (text,'\'+chr(39)+'dc','&AMP;Uuml;');
text := stringreplaceall (text,'\'+chr(39)+'a3','&AMP;#163;');
text := stringreplaceall (text,'\}','#]#');
text := stringreplaceall (text,'\{','#[#');
text := stringreplaceall (text,'{\rtf1\ansi\deff0\deftab720','');{Skall alltid tas bort}
text := stringreplaceall (text,'{\fonttbl',''); {Skall alltid tas bort}
text := stringreplaceall (text,'{\f0\fnil MS Sans Serif;}','');{Skall alltid tas bort}
text := stringreplaceall (text,'{\f1\fnil\fcharset2 Symbol;}','');{Skall alltid tas bort}
text := stringreplaceall (text,'{\f2\fswiss\fprq2 System;}}','');{Skall alltid tas bort}
text := stringreplaceall (text,'{\colortbl\red0\green0\blue0;}','');{Skall alltid tas bort}
{I version 2.01 av Delphi finns inte \cf0 med i RTF-rutan. Tog d&auml;rf&ouml;r bort
det efter \fs16 och la ist&auml;llet en egen tv&auml;tt av \cf0.}
//temptext := hamtastreng (text,'{\rtf1','\deflang');
//text := stringreplace (text,temptext,''); {H&auml;mta och radera allt fr&aring;n start till deflang}
text := stringreplaceall (text,'\cf0','');
temptext := hamtastreng (text,'\deflang','\pard');{Plocka fr&aring;n deflang till pard f&ouml;r att f&aring; }
text := stringreplace (text,temptext,'');{oavsett vilken lang det &auml;r. Norska o svenska &auml;r olika}
{H&auml;r skall vi plocka bort fs och flera olika siffror beroende p&aring; vilka alternativ vi godk&auml;nner.}
//text := stringreplaceall (text,'\fs16','');{8 punkter}
//text := stringreplaceall (text,'\fs20','');{10 punkter}
{Nu st&auml;dar vi ist&auml;llet bort alla tv&aring;siffriga fontsize.}
while pos ('\fs',text) &GT;0 do
  begin
    application.processmessages;
    start := pos ('\fs',text);
    Delete(text,start,5);
  end;
text := stringreplaceall (text,'\pard\plain\f0 ','&LT;P&GT;');
text := stringreplaceall (text,'\par \plain\f0\b\ul ','&LT;/P&GT;&LT;MELLIS&GT;');
text := stringreplaceall (text,'\plain\f0\b\ul ','&LT;/P&GT;&LT;MELLIS&GT;');
text := stringreplaceall (text,'\plain\f0','&LT;/MELLIS&GT;');
text := stringreplaceall (text,'\par }','&LT;/P&GT;');
text := stringreplaceall (text,'\par ','&LT;/P&GT;&LT;P&GT;');
text := stringreplaceall (text,'#]#','}');
text := stringreplaceall (text,'#[#','{');
text := stringreplaceall (text,'\\','\');
result := text;
end;
</PRE><HR>

<HR><PRE>//This is cut directly from the middle of a fairly long save routine that calls the above function.
//I know I could use streams instead of going through a separate file but I have not had the time to change this

            utfilnamn := mditted.exepath+stringreplace(stringreplace(extractfilename(pathname),'.TTT',''),'.ttt','') + 'ut.RTF';
             brodtext.lines.savetofile (utfilnamn);
             temptext := '';
             assignfile(tempF,utfilnamn);
             reset (tempF);
             try
                while not eof(tempF) do
                  begin
                     readln (tempF,temptext2);
                     temptext2 := stringreplaceall (temptext2,'\'+chr(39)+'b6','');
                     temptext2 := rtf2sgml (temptext2);
                     if temptext2 &LT;&GT;'' then temptext := temptext+temptext2;
                     application.processmessages;
                  end;
             finally
                    closefile (tempF);
             end;
             deletefile (utfilnamn);
             temptext := stringreplaceall (temptext,'&LT;/MELLIS&GT; ','&LT;/MELLIS&GT;');
             temptext := stringreplaceall (temptext,'&LT;/P&GT; ','&LT;/P&GT;');
             temptext := stringreplaceall (temptext,'&LT;/P&GT;'+chr(0),'&LT;/P&GT;');
             temptext := stringreplaceall (temptext,'&LT;/MELLIS&GT;&LT;/P&GT;','&LT;/MELLIS&GT;');
             temptext := stringreplaceall (temptext,'&LT;P&GT;&LT;/P&GT;','');
             temptext := stringreplaceall (temptext,'&LT;/P&GT;&LT;P&GT;&LT;/MELLIS&GT;','&LT;/MELLIS&GT;&LT;P&GT;');
             temptext := stringreplaceall (temptext,'&LT;/MELLIS&GT;','&LT;#MELLIS&GT;&LT;P&GT;');
             temptext := stringreplaceall (temptext,'&LT;#MELLIS&GT;','&LT;/MELLIS&GT;');
             temptext := stringreplaceall (temptext,'&LT;P&GT;&LT;P&GT;','&LT;P&GT;');
             temptext := stringreplaceall (temptext,'&LT;P&GT; ','&LT;P&GT;');
             temptext := stringreplaceall (temptext,'&LT;P&GT;-','&LT;P&GT;_');
             temptext := stringreplaceall (temptext,'&LT;P&GT;_','&LT;CITAT&GT;_');
             while pos('&LT;CITAT&GT;_',temptext)&GT;0 do
               begin
                 application.processmessages;
                 temptext2 := hamtastreng (temptext,'&LT;CITAT&GT;_','&LT;/P&GT;');
                 temptext := stringreplace (temptext,temptext2+'&LT;/P&GT;',temptext2+'&LT;/CITAT&GT;');
                 temptext := stringreplace (temptext,'&LT;CITAT&GT;_','&LT;CITAT&GT;-');
               end;
             writeln (F,'&LT;BRODTEXT&GT;'+temptext+'&LT;/BRODTEXT&GT;');
</PRE><HR>


<HR SIZE="6" COLOR="LIME">
<FONT SIZE="2">
<a href="mailto:rdb@ktibv.nl">Please email me</a> and tell me if you liked this page.<BR>
<SCRIPT LANGUAGE="JavaScript">
<!--
	document.write("Last modified " + document.lastModified);
// -->
</SCRIPT><P>
<TABLE BORDER=0 ALIGN="CENTER">
<TR>
	<TD>This page has been created with </TD>
	<TD> <A HREF="http://www.dexnet.com./homesite.html"><IMG SRC="../images/hs25ani.gif" WIDTH=88 HEIGHT=31 BORDER=0 ALT="HomeSite 2.5b">
</A></TD>
</TR>
</TABLE>

</FONT>



</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -