How to manage hyperlinks in rich Edit Control

Source: Internet
Author: User
Source: http://blog.csdn.net/zhymax/article/details/2280091、display a hyperlink in rich Edit Control

Display the hyperlink format in rich edit control, that is, add the cfe_link attribute to the selected text. You can use either of the following methods:

1. Automatically detect hyperlinks


Rich edit control has the URL detection function. It can automatically identify the URL text that meets the requirements, and automatically add the cfe_link attribute to the text, which is displayed as the hyperlink format, currently, the control can detect URL text with the following prefixes:

http:   file:   mailto:   ftp:   https:   gopher: nntp:   prospero:   telnet:   news:   wais:

(1) Enable Automatic Detection

CWnd *pRE = GetDlgItem(IDC_RICHEDIT1);pRE->SendMessage(EM_AUTOURLDETECT, TRUE, 0);

(2) Disable Automatic Detection

CWnd *pRE = GetDlgItem(IDC_RICHEDIT1);pRE->SendMessage(EM_AUTOURLDETECT, FALSE, 0);

(3) Determine whether the current Automatic Detection is Enabled

BOOL bEnable = pRE->SendMessage(EM_GETAUTOURLDETECT, 0, 0);

2. manually set text to hyperlink format


If you do not enable the URL detection function of rich edit control, you can manually set the cfe_link attribute for the text you are interested in to display it as a hyperlink.

(1) set cfe_link

CWnd *pRE = GetDlgItem(IDC_RICHEDIT1);CHARFORMAT2    cf; ZeroMemory(&cf, sizeof(CHARFORMAT2));cf.cbSize = sizeof(CHARFORMAT2); cf.dwMask = CFM_LINK; cf.dwEffects |= CFE_LINK; pRE->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

(2) Remove cfe_link

CWnd *pRE = GetDlgItem(IDC_RICHEDIT1);CHARFORMAT2    cf; ZeroMemory(&cf, sizeof(CHARFORMAT2));cf.cbSize = sizeof(CHARFORMAT2);cf.dwMask = CFM_LINK; cf.dwEffects &= ~CFE_LINK; pRE->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
Ii. Handling hyperlink events (en_link)

Using the above method, we can automatically or manually display a hyperlink, next, we need to handle the hyperlink events (wm_setcursor, wm_mousemove, wm_rbuttondown, wm_rbuttonup, wm_rbuttondbclk, wm_lbuttondown, wm_lbuttonup, wm_lbuttondbclk ), all of these notifications are sent to the parent window through the en_link message sent by rich edit control. The parent window can receive the en_link message in wm_notify. The specific implementation is as follows:

1. Notify rich Edit Control to send the en_link message, which is not sent by default. Add the following when initializing the control:

CWnd *pRE = GetDlgItem(IDC_RICHEDIT1);DWORD dwMask = pRE-> SendMessage(EM_GETEVENTMASK, 0L, 0L);dwMask |= ENM_LINK;pRE-> SendMessage(EM_SETEVENTMASK, 0L, (LPARAM)dwMask);

2. Process en_link in the wm_notify message.

Nmhdr * P = (nmhdr *) lparam; If (p-> code = en_link) {enlink * plink = (enlink *) lparam; Switch (plink-> MSG) {Case wm_lbuttondown: // press break with the left mouse button; Case wm_lbuttonup: {// release char strbuf [512] with the left mouse button; cwnd * pre = getdlgitem (idc_richedit1 ); pre-> sendmessage (em_exsetsel, 0, (lparam) & (plink-> chrg); pre-> sendmessage (em_getseltext, 0, (lparam) strbuf );:: shellExecute (null, _ T ("open"), _ T ("i0000e"), strbuf, null, sw_shownormal); break;} default: break ;}}
3. operations related to hyperlinks

Through the method described in the front, we can get two hyperlinks. One is to enable the automatic display of the controller when the URL is automatically detected. Generally, you only need to use iexplore to open the controller, for example, you can open a webpage, send an email, open a window, and add a webpage manually. You may need to perform different operations on each hyperlink, the question is how to differentiate different hyperlinks? This is the question we will discuss next.
To differentiate hyperlinks, you can distinguish them by text. Different texts correspond to different operations, which requires a ing between hyperlinktext and action, we can use cmap to save

Action = map [hyperlinktext]; If (Action = Action1) {// Action1} else if (Action = Action2) {// Action2} else {// other URLs, open with iexplore}

For some simple operations, this method can indeed meet the requirements. But if there are multiple hyperlinks of the same text and the operations corresponding to these hyperlinks are different, how should we differentiate them? Obviously, it cannot be uniquely identified by text. It must be distinguished by other features. Here I use the starting position of the hyperlink text, that is, CPMin in charrange, this position is unique in the text. When adding hypertext, <CPMin, Action> is mapped and stored in cmap, in this way, when we click a hyperlink, we can find the action through the starting position of each text CPMin to perform the corresponding operations:

Action = map [CPMin]; If (Action = Action1) {// Action1} else if (Action = Action2) {// Action2} else {// other URLs, open with iexplore}

Now we can accurately control the operations on each hyperlink. To save more information, replace <CPMin, hyperlink> with the action in <CPMin, Action> with a structure. For example:

typedef struct _HyperLink {       WORD   Action;        DWORD dwId;} HyperLink;

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.