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

2008-04-09 04:31:05来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


inc(RunStr);
end;

RunStr^:= #0;
TokenStr:= FStrBuff;

end; { ScanForRtf }

EXAMPLE - code snippet from Pas2Rtf demonstrating the "long comment" bug

The 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 routine so it points to the correct place in the new area of memory. Try and fix it yourself, or look at my garsely spaghetti code in jhdPasToRtf.pas.

Automatic Syntax Highlighting (my first implementation)

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 when the "syntax" changes and what is affected. In retrospect the difficult thing is to implement a highlighter that is:

  1. Fast
  2. Accurate
  3. Doesn''''t flicker
  4. Isn''''t obvious ("the someone is chasing me phenomenon".. you''''ll see)

1. When should 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 i.e. 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 needed then was an [Event].

2. Which event - there''''s so many 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 (i.e. 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 types into the edit control.

You may be thinking however: "Heh? What about those other things - like Methods and Properties. Can''''t 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 assumptions out:

So I wrote my first [OnChange] event:

  1. Create a New application
  2. place on it one RichEdit (RichEdit1) and one Edit control (Edit1)
  3. Code the [OnChange] for the RichEdit1 control like this:

    标签:

    版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:用DELPHI给OICQ动手术(二)

下一篇:讲述如何开发一个控件,很有价值(四)