转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
讲述如何开发一个控件,很有价值(三)         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2207 更新时间:2009/4/23 18:44:03
ch 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:
procedure TForm1.RichEdit1Change(Sender: TObject);
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!

  1. Compile and Run...
  2. Click in the Control. Nothing...
  3. Move around in it using CursorKeys... Nothing...
  4. Click outside the control.. and then back inside.. Nothing...
  5. Press the [Space Bar].. Tag=1...
  6. Press [Backspace].. Tag=2...
  7. Press return.. Tag=3..
  8. Select some text.. No change..
  9. CTRL-C some text.. No change..
  10. CTRL-X some text.. Tag=4..
  11. 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:

  1. Select a token
  2. Manipulate it using the buttons provided to change Font, Size, Color, and Bold
  3. Move onto the next token
  4. 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:
 

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 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)
.

SelStart Position of the Cursor, or the beginning of the selected text SelLength 0 if SelStart = Cursor Pos, or length of selected Text SelText empty if no text selected, or actual text selected SelAttribute Default 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 doesn''''t give you access to the ''''RTF'''' representation of a single line. Plus I still can''''t 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;

上一页  [1] [2] [3]  下一页


[Delphi程序]讲述如何开发一个控件,很有价值(七)  [Delphi程序]讲述如何开发一个控件,很有价值(六)
[Delphi程序]讲述如何开发一个控件,很有价值(五)  [Delphi程序]讲述如何开发一个控件,很有价值(四)
[Delphi程序]讲述如何开发一个控件,很有价值(二)  [Delphi程序]讲述如何开发一个控件,很有价值
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台