Delphi is added to the link for Richedit

zhaozj2021-02-16  108

With the in-depth application of the network, various software has added some network functions. The commonly used text editor has only one function. When you turn a URL, the URL turns into a blue sky with underline. Color, click, your default web browser will be started, showing the content identified by the URL; if you entered "mailto: webmaster@yesky.com" then your default email program Will be opened so you can send an email through a text editor.

This seemingly magical function, you don't need to use a third-party VCL component to get it (according to the online "big cow", the third party component is much more, the brain will gradually degrade into gorilla due to long-term thinking The degree, so as little as possible). Tricheidt is a very useful VCL component provided by Delphi (you can find it in the Win32 tab of the Delphi component panel), which provides most of the features of a text editing program, or even format the paragraph, Change the color of the text, so that you can use it to make a written program similar to the Windows operating system, this time we rely on it.

To achieve a hyperlink function, you must first resolve problems that detect URL, Mailto, etc. in Trichrdit, or start with an instance, create a Delphi project, put two Trichrdit components on blank FORM, and the name of the component With the default name richedit1 and richrdit2, the implementation of the perceived function of the URL is:

1. Send a message (em_geteventmask) to the RicheDit component, get event template, which will specify which message notification will be sent to the parent window.

2. Send an EM_SETEVENTMASK message containing the ENM_LINK flag to eichedit, enm_link will be included in Mask, and the En_Link message will be sent when the mouse click ULR.

3. Send an EM_AUTOURDETECT message to the Richedit, the EM_AUTOURDETECT message will automatically detect the URL.

Then we will solve the problem of URL highlighting.

In the programs we just built, enter its OnCreate event, add a custom InitricHeDITURLDetection process in this event to be able to trigger it when the program is started; the RicheDit2 component is not added to the INITRICHEDITURLDETECTION process, Let the two effects, the code is as follows:

// This is our custom initrichedITURLDETECTION process // it is the most critical stepdure tForm1.initrichedITURLDITECTION (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_AUTOURLDECT, INTEGER (TRUE), 0); End; // This is a formcreate event, which joined our custom procedure /// The effect we want to implement procedure tform1.form1.form1; var s: string; begin initRicheditURLDITECTION (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: = send; Finally, we will automatically invoke the browser or mail program after the mouse click, we use the override form's WndProc method to capture the en_link message, then we use the shellexecute process to start the default The browser, the code is as follows:

// Form's WndProc Process Procedure TForm1.WndProc (var Msg: TMESSAGE); VAR P: ​​TenLink; SURL: String; CE: trichedit; recomgin if (msg.msg = WM_Notify).. Code = en_link) The begin p: = TenLink (POINTER (TWMNotify (MSG) .nmHDR); if (p.msg = wm_lbuttondown) The begin try CE: = trichedit (form1.activeControl); SendMessage (Ce.handle, EM_EXSETSEL, 0, Longint (@ (p.chrg))); surl: = ce.selte; shellexecute (Handle, 'Open', PCHAR (SURL), 0, 0, SW_SHOWNORMAL); End End; End; end; Inherited;

The main code is listed, and the results of the final procedure are shown below, and the difference between two Richedit is different. When seeing some technical highlights in other people's procedures, think more about how to solve with existing components, this is also a good opportunity to improve the level, waiting for a third-party VCL component, may really make you become one Gorilla.

Http://soft.yesky.com/softchannel/72342371928440832/20040803/1838284.shtml

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

New Post(0)