Nowadays, many IM software chat dialogs can be displayed into a list of familiar familiar, and the mouse is moved to the link, and the user can click directly after clicking. URL, this is really useful for IM software, is it so convenient, is it difficult to achieve it? In fact, it is not difficult, as long as the setting makes your Richedit support link, automatically detect the URL, and then implement a response function for the connection click. In general, take a few lines of code.
I use a dialog project to explain it.
First, generate a dialog engineering, add a Richedit control on the interface, and add a code for the control class member variable of the control with the wizard, it is called M_re.
Now, the control supports hyperlinks and automatically detects the URL. In the OnInitDialog () function of the dialog box, we join the code:
DWORD Mask = m_re.GetEventMask (); Mask = Mask | ENM_LINK | ENM_MOUSEEVENTS | ENM_SCROLLEVENTS | ENM_KEYEVENTS; m_re.SetEventMask (Mask); :: SendMessage (m_re.m_hWnd, EM_AUTOURLDETECT, true, 0);
At this time, if you are compiled, you can enter if you have www.163.com in Richedit, it will be blue, the mouse is moved to the link, and the mouse pointer of the hand is displayed, but when you click on the link, what is it also Not doing, because we haven't told the computer yet, we should help us do after you click on the link. Let's tell the computer through a function, please help me open this link.
Open the link. Declaring our links in the message function declaration domain in the dialog class header file Click notification function
AFX_MSG VOID ONRICHEDITLINK (nmHDR * in_pnotifyheader, lresult * out_presult);
Then, in the CPP file of the dialog, the messages of our clicking link and my response function are mapped.
ON_NOTIFY (en_link, idc_richedit1, onricheditlink)
Finally, it is realized our response function:
ENLINK * l_pENLink = (ENLINK *) in_pNotifyHeader; * out_pResult = 0; switch (l_pENLink-> msg) {default: {} break; case WM_LBUTTONDOWN: // If the left mouse button is clicked {CString strURL; CHARRANGE crCharRange; CRichEdit * pTempEdit Ptemnedit = (crichedit *) crichedit :: fromHandle (l_penlink-> nmhdr.hwndfrom); Ptempedit-> getsel (crCharRange); // Get the original selected character Ptempedit-> setsel (l_penlink-> chrg); // Set RUL chosen
Strurl = ptempedit-> getselText (); // Get URL PTEMPEDIT-> Setsel (CRCHARRANGE); // Restore the original selected character CWAITCURSOR L_WAITCURSOR;
// use the current default browser to play URL shellexecute (this-> getsafehwnd (), _t ("open"), strurl, null, null, sw_shownormal); * OUT_PRESULT = 1;} Break; Case WM_LButtonup: {* out_presult = 1;} Break;} is so much, no, compiler.