<h2>Question</h2> I need to place a hyperlink to my dialog and to handle clicks on this link in my application code. How can I do it?
<h2>Answer</h2> You can simulate a link in dialog by a number of ways. First of all you can draw a link text on screen in <i>WM_PAINT</i> handler. You should select desired (most probably underlined) font into dialog device context, and draw link text to dialog. Your OnPaint method should contain code like this:
Code:
CFont* pOldFont = dc.SelectObject(&m_fontUnderlined);
dc.SetTextColor(rgbLinkColor);
dc.DrawText(strFirstLinkText, m_rtFirstLink, 0);
dc.SelectObject(pOldFont);
Also don't forget to check does stylus pressed inside link text. This could be done in <i>WM_LBUTTONDOWN</i> handler:
Code:
if (m_rtFirstLink.PtInRect(point)) {
//link activated
}
Link rectangle should be calculated in advance:
Code:
CFont* pOldFont = dc.SelectObject(&m_fontUnderlined);
CSize szFirstLinkSize = dc.GetTextExtent(strFirstLinkText);
m_rtFirstLink = CRect(
CPoint(rectClient.left+(rectClient.Width()-szFirstLinkSize.cx)/2,
rectClient.top+50), szFirstLinkSize);
The other way is to use custom drawn static control. You should set underlined font in this static:
Code:
GetDlgItem(IDC_LINK_2)->SetFont(&m_fontUnderlined);
For font color changing you should handle WM_CTLCOLOR message and update color in provided device context:
Code:
HBRUSH CTestHyperlinkDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID()==IDC_LINK_2) {
pDC->SetTextColor(rgbLinkColor);
}
return hbr;
}
Link activation is received through <i>BN_CLICKED</i> notification in dialog. Ensure that <i>Notify</i> check box is set in link static styles. It is responsible for <i>SS_NOTIFY</i> style.
Also you can use controls that supports links, such as <i>RichInk</i> or <i>HTML Viewer</i> controls. RichInk supports <i>EM_INSERTLINKS</i> message for link insertion and <i>HTML Viewer</i> could be fed with html code. Please, look at related resources section of this article for mere information.
You can download a <a href="samples/TestHyperlink.zip">sample program</a> that uses a number of approaches discussed in this article. This is a eVC4 MFC dialog based application. In case of pure WinAPI application implementation details will change but these approaches still apply.
<h2>Related resources:</h2>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/sections/ui.html">Section: User Interface</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/sections/html.html">Section: HTML in Programs</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/articles/buttoncolor.html">QA: How to create a colored button?</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnppc2k/html/ppc_viewer.asp">Article: Developing in C++ with the HTML Viewer Control</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/guide_ppc/htm/_rapier_ref_em_insertlinks.asp">Article: EM_INSERTLINKS message</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnce30/html/richink.asp">Article: Using Rich Ink Technology in Microsoft Windows CE 3.0</a>