1)创建MFC(exe),窗体中放入一对静态文本框(Static Text)、一对button,导入一个cur手型鼠标指针到工程中;
2)Text1平行于button1,Text2平行于button2,所有控件的ID值默认即可;
3)Text1 & Text2 的标题分别为:“Weblog”,"E-mail",
button1 & button2的标题分别为:"http://www.wayce.net","mailto:mwcobain@gmail.com".
4)创建两个button的clicked事件。
button1事件代码:
void CLinktestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
ShellExecute(m_hWnd,NULL, "http://www.wayce.net", NULL,NULL,SW_SHOWMAXIMIZED);
}
button2事件代码:
void CLinktestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
ShellExecute(m_hWnd,NULL, "mailto:mwcobain@gmail.com", NULL,NULL,SW_SHOWMAXIMIZED);
}
5)使用类向导为dialog增加WM_SETCURSOR消息处理函数,函数代码如下:
BOOL CLinktestDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
//return CDialog::OnSetCursor(pWnd, nHitTest, message);
CRect rcButton1,rcButton2;
CPoint ptCursor;
CWnd *pStatic1=GetDlgItem(IDC_BUTTON1);
CWnd *pStatic2=GetDlgItem(IDC_BUTTON2);
pStatic1->GetWindowRect (rcButton1);
pStatic2->GetWindowRect (rcButton2);
GetCursorPos(&ptCursor);
if (rcButton1.PtInRect (ptCursor)||rcButton2.PtInRect (ptCursor))
{
CWinApp *pApp=AfxGetApp();
HICON hIconBang=pApp->LoadCursor (IDC_CURSOR1);
SetCursor(hIconBang);
return TRUE;
}
else
return CDialog::OnSetCursor
(pWnd, nHitTest, message);
}
效果如下:
图(1)用MFC实现超链接