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、程式運作效果如下:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicGcq5yaulGbvw1NwgDM5ADMy8CXzV2Zh1WS5JHduV0LcVXen5Waudmbhd3LcRXZu9lbkN3Yfd2bsJ2Xw9CXzV2Zh1WavwFdl5mLuR2cj5yZvxmYtA3Lc9CX6MHc0RHaiojIsJye.jpg)