讲述如何开发一个控件,很有价值(三)(3)
2008-04-09 04:31:05来源:互联网 阅读 ()
begin
TRichEdit(Sender).Tag := TRichEdit(Sender).Tag 1;
Edit1.Text := ''''Tag='''' IntToStr(TRichEdit(Sender).Tag);
end;
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 around }
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 they are doing.
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 using the buttons provided to change Font, Size, Color, and Bold
- Move onto the next token
- Goto 1
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:
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.LinesLines 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 of text, adding lines, deleting lines, or replacing the text in lines.
To work with the text as one chunk, use the Text property. 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 changed.
Oh.. look at what? How can I tell which line is the one that is changed?
Unlike some Events, the [OnChange] isn''''t passed any variable''''s save the identity of the RichEdit control affected. The RichEdit Control doesn''''t have a runtime variable that tells us either. The only variables are the SelStart and SelLength - but their about selecting text aren''''t they? 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)
.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 讲述如何开发一个控件,很有价值(七) 2008-04-09
- 讲述如何开发一个控件,很有价值(六) 2008-04-09
- 讲述如何开发一个控件,很有价值(四) 2008-04-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
