要实现这个功能需要一个函数
HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
具体的大家可以查看一下MSDN 每个参数的具体意思
实例:
新建一个superlink对话框应用程序
添加两个按钮
ID CAPTION
IDC_BUTTON1 c++奋斗乐园
IDC_BUTTON2 给我写信
然后分别添加消息响应函数
void CSuperlinkDlg::OnButton1()
{
// TODO: Add your control notification handler code here
// ShellExecute(m_hWnd,NULL,"www.cppleyuan.com",NULL,NULL,SW_SHOWMAXIMIZED);
// ShellExecute(m_hWnd,"explore","1",NULL,NULL,SW_SHOWNORMAL);
ShellExecute(m_hWnd,NULL,"http://www.computerworld.com.cn",NULL,NULL,SW_SHOWMAXIMIZED);
}
void CSuperlinkDlg::OnButton2()
{
// TODO: Add your control notification handler code here
ShellExecute(m_hWnd,NULL,"mailto:932414406@qq.com",NULL,NULL,SW_SHOWNORMAL);
}
这个函数的第二个参数 lpOperation, 可以有多个选项可选
1)open 2)edit 3)explore(浏览) 4)print(打印) 5)find 6)NULL
我们可以在OnButton1 函数中把之前写的ShellExecute函数注释起来,然后重写个
ShellExecute(m_hWnd,"explore","1",NULL,NULL,SW_SHOWNORMAL); 同时要在当前目录下新建一个
名为1 的文件夹
或者也可以这么写
ShellExecute(m_hWnd,"open","1.doc",NULL,NULL,SW_SHOWNORMAL); 同时要在当前目录下新建一个
名为1.doc的word文档
等等 其他的读者可以自己尝试 也可以不在当前目录下 ,只不过要指定lpDirectory 的值
现在我们再在 对话框上放一个静态文本,同样把它实现为一个超链接,
但是静态文本不能相应 消息,所以我们要把静态文本的熟悉的NOTIFY勾上,并且把ID改了,这里我们改为 IDC_CPPLY,
然后就可以添加消息响应函数了
void CSuperlinkDlg::OnCpply()
{
// TODO: Add your control notification handler code here
ShellExecute(m_hWnd,NULL,"www.cppleyuan.com",NULL,NULL,SW_SHOWNORMAL);
}
下面我们要做的就是当鼠标放在超连接上显示一个 手 的形状
我们自己在vc中new一个 cursor

然后为 CsuperlinkDlg 类添加一个 WM_SETCURSOR 消息响应函数,并添加如下代码
BOOL CSuperlinkDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
CRect rect1,rect2,rect_static;
CPoint ptcursor;
GetDlgItem(IDC_BUTTON1)->GetWindowRect(rect1);
GetDlgItem(IDC_BUTTON2)->GetWindowRect(rect2);
GetDlgItem(IDC_CPPLY)->GetWindowRect(rect_static);
GetCursorPos(&ptcursor);
if(rect1.PtInRect(ptcursor)||rect2.PtInRect(ptcursor)||rect_static.PtInRect(ptcursor))
{
CWinApp *pApp=AfxGetApp();
HICON hIconBang=pApp->LoadCursor(IDC_CURSOR1);
SetCursor(hIconBang);
return TRUE;
}
else
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}