Delphi Add link for RichEdit

Source: Internet
Author: User

With the deep application of the network, a variety of software has added some network functions, commonly used text editors also appear such a function, when you have entered a URL after the Web site from black to the underlined treasure blue, click, your default Web browser will be started, Displays the content of the Web site identified by the URL, and if you enter "mailto:webmaster@yesky.com" then your default email program will be opened so that you can send an email using a text editor.

This seemingly magical feature does not need a third-party VCL component to handle (according to the online "Daniel", the Third-party components used more, the brain will be due to long-term lack of thinking and gradually degenerated into a gorilla degree, so as far as possible). Tricheidt is a very useful VCL component provided by Delphi (you can find it in the Win32 tab of the Delphi Palette), it provides most of the functions of a text editor, and can even format paragraphs and change the color of text, so You can use it to make a Windows operating system with its own WordPad program, this time we rely on it.

To realize the function of the hyperlink, we must first solve the problem of how to detect the URL, mailto and so on in Trichrdit, or start with the example, create a new Delphi project, put two Trichrdit components on the blank form, The name of the component is RichEdit1 and RichRdit2 with the default name, and the realization of the perceptual function of the URL is:

1, send a message (Em_geteventmask) to the RichEdit component, get the event template (Eventmask), it will specify which message notification (notification) will be sent to the parent window.

2, send a em_seteventmask message containing the ENM_LINK flag to Eichedit,enm_link will be included in the mask, when the mouse clicks the ULR message will be sent.

3, send Em_autourldetect message to Richedit,em_autourldetect message will automatically detect the URL.

Then, we'll solve the URL highlighting problem.

In the program we just built, find the RichEdit1 component, enter its OnCreate event, add a custom initrichediturldetection procedure to the event to trigger it when the program starts The OnCreate event of the RichEdit2 component does not join the initrichediturldetection process, so that the two compare the effects, as shown in the following code:

//这个是我们自定义的InitRichEditURLDetection过程
//它是最关键的一步
procedure TForm1.InitRichEditURLDetection(RE: TRichEdit);
 var
  mask: Word;
 begin
  mask := SendMessage(RE.Handle, EM_GETEVENTMASK, 0, 0);
  SendMessage(RE.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
  SendMessage(RE.Handle, EM_AUTOURLDETECT, Integer(True), 0);
 end;
 //这是FormCreate事件,它加入了我们自定义的过程
 //使得程序一运行就出现我们想要实现的效果
 procedure TForm1.FormCreate(Sender: TObject);
  var
   s: string;
  begin
   InitRichEditURLDetection(RichEdit1);
   s:='http://www.yesky.com' + #13#10 +
   'mailto:software@chinabyte.com';
   RichEdit1.Text := s;
   s:= 'http://www.yesky.com' + #13#10 +
   'mailto:software@chinabyte.com';
   RichEdit2.Text := s
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.