1、添加成员变量m_RectLink,用来保存文本框的坐标,添加两个静态文本控件,将默认ID改掉:
class CSampleDlg : public CDialog
{
// Construction
public:
CSampleDlg(CWnd* pParent = NULL); // standard constructor
// Implementation
protected:
HICON m_hIcon;
//用于保存静态文本框的屏幕坐标
RECT m_pRectLink;
......
}
2、获取static控件的区域,在初始化函数里添加代码如下:
BOOL CSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//将静态文本的屏幕坐标存放在m_pRectLink中
GetDlgItem(IDC_LINK) -> GetWindowRect(&m_pRectLink);
//将屏幕坐标转换为客户坐标
ScreenToClient(&m_pRectLink);
return TRUE; // return TRUE unless you set the focus to a control
}
3、添加Windows消息WM_MOUSEMOVE,代码如下:
void CSampleDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//下面设置鼠标在静态文本区时,将光标设成小手状
if (point.x > m_pRectLink.left && point.x < m_pRectLink.right && point.y > m_pRectLink.top && point.y < m_pRectLink.bottom )
//此处添加判断坐标算法
{
HCURSOR hCursor;
hCursor = AfxGetApp() -> LoadCursor(IDC_HAND);
//将鼠标设为小手状
SetCursor(hCursor);
}
CDialog::OnMouseMove(nFlags, point);
}
4、添加Windows消息WM_LBUTTONDOWN,代码如下:
void CSampleDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//此处添加判断坐标算法
if (point.x > m_pRectLink.left && point.x < m_pRectLink.right && point.y > m_pRectLink.top && point.y < m_pRectLink.bottom)
{
//鼠标左键按下
if (nFlags == MK_LBUTTON)
{
//为改善鼠标效果,此处加入以上变换鼠标形状的代码
ShellExecute(0, NULL, "http://blog.csdn.net/wangningyu", NULL,NULL, SW_NORMAL);
//也可以添加电子邮件的链接
ShellExecute(0, NULL, "mailto:[email protected]", NULL,NULL, SW_NORMAL);
}
}
CDialog::OnLButtonDown(nFlags, point);
}
5、程序运行效果如下: