Telling how to develop a control, it is worthless (3)

zhaozj2021-02-17  46

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;}} {/ colorbl / red0 / green0 / blue0;} / deflang1033 / pard / plain / f2 / fs20 / par} plaintext.rtf - Too Seeh how much one text was handled {/ rtf1 / ANSI / defff0 / deftab720 {/ fonttbl {/ f0 / fswiss ms Sans Serif;} {/ f1 / froman / fcharset2 symbol;}}}}}}}} new roman;}} {/ colorTbl / red0 / green0 / blue0;} / DEFLANG1033 / PARD / PLAIN / F2 / FS20 This Is Plain Text / Par} Diffont.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;}} {/ colorbl / 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 N EW Roman;}}}}}}}} / blue0;} / deflang1033 / pard / plain / f2 / fs36 plain text Different Font / PLAIN / F2 / FS20 / PAR} Diffbolor.rtf - etc. My Favourite Of Course - BLUE. {/ {/ f0 / fswiss ms sans serif;} {/ f1 / froman / f2 / froman}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} {/ colorTBL / Red0 / green0 / blue0; / red0 / green0 / blue255;} / declang1033 / 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:

Initial Header (/ RTF1 / .....) FONTTABLE (/ f0 / fswiss ...) ColorTable (/ colorbl) miscellaneous default format (/ pard ....) Body of the file.

As A Result of That: WriteTobuff ('{/ → → {/ fonttbl {/ f0 / fswiss ms sansserif;} {/ f1 / froman / fcharset2 symbol;} {/ f2 / fmodern coOrier NEW;}} ' # 13 # 10); WriteTobuffer (' {/ colorbl / red0 / green0 / blue0;} ' # 13 # 10); WriteTobuffer (' / deflang1033 / pard / plain / f2 / fs20 " );

To Become:

WriteTobuffer ('{/ RTF1 / ANSI / DEFF0 / DEFTAB720'); WriteFontTable; WriteColortable; WriteTobuffer ('/ deflang1033 / pard / place / 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 Crashs 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); fstrbuffnd: = 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> = fstrbuffnd the allocstrbuff; Runstr ^: = tokenstr [i]; inc (RunStr); end; runstr ^: = # 0; TokenStr: = FStrBuff; end; {ScanForRtf} EXAMPLE - code snippet from Pas2Rtf demonstrating the "long comment" bugThe problem: if FStrBuff is enlarged using AllocStrBuff () (to make it bigger to handle a very long comment) the Windows Memory manager probably has to re-allocate it by moving the entire string buffer somewhere else in memory. RunStr however is not adjusted for this change and stillpoints to the old memory area, now unallocated.

THE FIX: Reallocate Runstr in The AllocstrBuff Runstr in The AllocstrBuff Runstr in The New Area of ​​Memory. Try and fix it yourself, or look in jhdpastortortf.pas.

Automatic Syntax Highlighting (My First Implement)

TO Understand How Automatic Syntax Highlighting Works, You Should Have A Close Look At What Happens in The Delphi 3.0 Editor. After All - IF Borland Was Happy With It - Who Am I To Argue :-)

Take Note by "Syntax" Changes and what is affected. In Retrospect The Difficult Thing is to import a highlighter That IS:

Fast Accurate Doesn't Flicker Isn't Obvious ("The Someone Is Chasing Me Phenomenon" .. You'll See)

1. WHOULD we do the re-highlighting?

In YourPasEdit the highlighting is done as the file is read in Once this is done, the only way to make use of that technique would be to write out the file everytime it changes and read it back in again -. Obviously a very slow process. In my case, I basically wanted to just reformat the line (s) that have been changed, immediately after the change had been done ie after every new character, DELETE or BACKSPACE or even Paste or DragDrop had been processed. I needed something that was Triggered Everytime The Control Was Effected In Such A Way.

What i neededed then...

2. Which Event - There's so mix to choose from?

A RichEdit, like any control, has a number of [Events] triggered when you do various things to the control. What is not obvious, is that many events trigger other events in turn. So in choosing which Event (s) to hang your Code Off You Have To Ensure That (a) It catches all situations where you need to "fix" the highlighting and (b) it doesn't become re-entrant (ie what you do in the [Event], DOESN'T Trigger Itself Again or Any Other [Event] That Would Call The "Highlighting Code"). from a Quick Look At The HelpFile, I Decided That [OnChange] Seemed a Likely Candidate. According to the delphi 3.0 HelpFile:

Write an OnChange event handler to take specific action whenever the text for the edit control may have changed. Use the Modified property to see if a change actually occurred. The Text property of the edit control will already be updated to reflect any changes. This event Provides the first Opportunity to respond to modifications That the user type Into the edit control.

You may be thinking however: "Heh What about those other things - like Methods and Properties Can not they also change the text?.?" They sure can - but most end up triggering [OnChange] anyhow.3 Is it what I. Want? - Rich Text Controls (from Delphi3 Helpfile)

The rich text component is a memo control that supports rich text formatting. That is, you can change the formatting of individual characters, words, or paragraphs. It includes a Paragraphs property that contains information on paragraph formatting. Rich text controls also have printing and TEXT-Searching Capabilities. by Default, The Rich Text Editor Supports

Font properties, such as typeface, size, color, bold, and italic format Format properties, such as alignment, tabs, indents, and numbering Automatic drag-and-drop of selected text Display in both rich text and plain text formats. (Set Plaintext to True to remove formatting)

Type tnotifyevent = procedure (sender: TOBJECT) OF Object; Property Onchange: TNotifyEvent

4. Is IT THE EVENT I WANT - IE [ONCHANGE] EVENT - THE RIGHT One?

Live Dangerously, Let's Give It A Go and See ... by Testing Our Assumtions Out:

SO i wrote my first [onchange] Event:

Create a new application place on it one richedit (richedit1) And one edit control (edit1) code the [onchange] for the richedit1 control like this:

Procedure tForm1.richedit1change (sender: Tobject); begin trichedit (sender) .tag: = trichedit (sender) .tag 1; edit1.text: = 'tag =' INTOSTR (TRICHEDIT (Sender) .tag);

In this case the Sender object is the RichEdit being changed. The code basically uses the RichEdit's Tag variable (initially 0) as a handy Control specific variable. Everytime the [OnChange] event is called, it increases the Tag by 1, and display its value in an Edit Control as text. You should pre-set the RichEdit control with some text in it, otherwise the following may be confusing! Compile and Run ... Click in the Control. Nothing ... Move around in it using CursorKeys ... Nothing ... Click Outside The Control .. and then back inside .. Nothing ... Press the [space bar] .. tag = 1 ... press [backspace] .. tag = 2 ... press Return .. tag = 3 .. select some text .. no change .. Ctrl-c Some text .. no change .. Ctrl-x Some text .. tag = 4 .. ctrl-v some text .. tag = 5 .

AS IT LOOKED Good So Far I Then Added to The Form1.onShow Event:

Richedit1.Lines.LoadFromfile ('c: /winzip.log'); {Just a plain text file hanging arrone

to see what happened And guess what -. an [OnChange] event was called sometime and "Tag = 1" was displayed in the Edit control as the proof when the Form appears for the first time So we can see that procedures do call Events. That apply to what theies.

5. What happens in syntax highlighting anyhow?

... Watch carefully in the Delphi Editor Now try and reproduce it Open a WordPad (since WordPad is a souped up RichEdit basically) Read in a source file (e.g any Unit1.pas) and do syntax highlighting manually:

SELECT A token manipulate it usning the button, ket, and bold move mobile phone

So therefore in [onchange] We'll Try and write code to reproduce what we have done manually.

6. Which text do I want .. and where do I get it Hunting through the Delphi Helpfile on RichEdit controls we find that the actual text information in the RichEdit control is stored (or rather can be accessed from) either?:

. RichEdit.Text Text contains a text string associated with the control TCaption = type string; property Text: TCaption; Description Use the Text property to read the Text of the control or specify a new string for the Text value By default, Text is. the control name. For edit controls and memos, the text appears within the control. For combo boxes, the text is the content of the edit control portion of the combo box. RichEdit.Lines Lines contains the individual lines of text in the rich text edit control property Lines:. TStrings; Description Use Lines to manipulate the text in the rich text edit control on a line by line basis Lines is a TStrings object, so TStrings methods may be used for Lines to perform manipulations such as counting the lines. OR Replacing The text in lines. To work with the text is one chunk, us.. To manipulate Individual Lines of text, The Lines Property Works Better.

Now Lines Seemed to Be What I Wanted - After All I Wanted The Syntax Highlighting To Work ON A LINE BY LINE Basis. So Let's have a look at whats been change.

Oh .. Look at what? How can I Tell Which line is the one thing is change?

. Unlike some Events, the [OnChange] is not passed any variable's save the identity of the RichEdit control affected The RichEdit Control does not have a runtime variable that tells us either The only variables are the SelStart and SelLength -. But their about SELECTING TEXT AREN ''? I Just Want To Know What Line I'm on :-( IT Was About Then That I Re-Read The Information on The SEL ??? Properties, and RECALLED My "Concept" Code. Selection - I realised -. was the name of the game By manipulating these variables I could reproduce what I was doing manually - selecting text -. as program code Once selected you can then manipulate the attributes of the selected text through the SelAttributes structure.

Let's Get Familiar with these Variables (in summary).

SelStartPosition of the Cursor, or the beginning of the selected textSelLength0 if SelStart = Cursor Pos, or length of selected TextSelTextempty if no text selected, or actual text selectedSelAttributeDefault attributes if I was to start typing at the Cursor position OR the actual attributes of the selected TEXT

There is actually no other way to access the attributes of the text already in the Control than by programmatically accessing them via manipulating Sel variables (* if you stick to using the defined properties and methods).

In the end RichEdit.Text and RichEdit.Lines are just plain old strings -. Not really "rich" at all The other thing to note is that SelStart is a 0-based index on the first character of RichEdit.Text - so it looks Like richedit.line is out the door.

7. Okay Implement: SELECT A TOKEN

Basically I wanted to start at the beginning of the line, send just that line to PasCon, and read it back in and replace the current line with the result. Trouble is RichEdit does not give you access to the 'RTF' representation of a single line Plus I still can not tell when the beginning or end of the line is Since the latter seems to be a nagging problem, we better fix that first - trouble is:..? How When all else fails - WinAPI calls of course .

Most visual controls in Delphi are in fact just native Windows controls encapsulate as Delphi types. You can still use Windows API functions to access the control underneath. This was it is possible to access information not accessable per Delphi public Properties, Method or Events. Time To delve Through Win32.hlp and See What It Has To Say About Richedit Controls. Its Stored In C: / Program Files / Borland / Delphi 3.0 / Help If You Don't have a shortcut to it.

Open win32.hlp -> [contents] -> [RichEdit Controls] -> [RICH Edit Controls]

I spent some time getting to know the "full" capabilites of the RichEdit control hidden behind Delphi's implementation of it. Much of what I learned came in handy later on (as you'll see) and as a result I derived my second two Delphi Rules:

Delphi Rule # 2: IF Your Project Hinges on The Capabilities of a Certain Control - Make Sure You KNOW Everthing About It - from The Beginning.

Delphi Rule # 3: "Reference" is not the Same as "summary" (Also KNown As Win32.hlp Rule # 1)

Eventually I discovered the key in the [Rich Edit Control Reference] under "Lines and Scrolling". I had thought this page was simply a summary of the messages discussed in the preceding help pages. Actually it included a number of extra messages not discussed elsewhere - the exact ones I was after Lines and Scrolling EM_LINEFROMCHAR -! give them a 0-based index and they'll return the line EM_LINEINDEX - give them a line and you get the index of the first character EM_LINELENGTH - give them a line and you Get the length of the line

So lets start coding.

(NB: To use the constants (em-?) You'll Have to Manually Add Richedit in the Uses Clause

procedure TForm1.RichEdit1Change (Sender: TObject); var WasSelStart, Row, BeginSelStart, EndSelStart: Integer; MyRe: TRichEdit; begin MyRe: = TRichEdit (Sender); WasSelStart: = MyRE.SelStart; Row: = MyRE.Perform (EM_LINEFROMCHAR, Myre.selstart, 0); BeginselStart: = myre.Perform (EM_LINDEX, ROW, 0); endselstart: = BeginselStart length (myre.lines.strings [rower); // i Didn't Use the EM_LINELENGTH MESSAGE, AS THE VARIABLES WAS AVAIABLE VIA DELPHI Edit1.text: = INTOSTR (WASSELSTART) '- INTOSTR (BEGINSELSTART) ' - INTSTR (EndselStart);

转载请注明原文地址:https://www.9cbs.com/read-30439.html

New Post(0)