1.3.3 小试牛刀--编程(www.cppentry.com)实现写邮件超级链接(2)
(2) 在文件HyperLink.cpp中定义类成员函数的具体实现,接下来开始讲解此文件的具体实现过程。
① 定义函数OnMouseMove和OnSetCursor实现鼠标移动事件,具体代码如下:
- //鼠标移动事件
- void CHyperLink::OnMouseMove(UINT nFlags, CPoint point)
- {
- CStatic::OnMouseMove(nFlags, point);
- //判断鼠标是否在控件上方
- if (m_bOverControl)
- {
- CRect rect;
- GetClientRect(rect);
-
- if (!rect.PtInRect(point))
- {
- m_bOverControl = FALSE;
- ReleaseCapture();
- RedrawWindow();
- return;
- }
- }
- else // 鼠标移过控件
- {
- m_bOverControl = TRUE;
- RedrawWindow();
- SetCapture();
- }
- }
- BOOL CHyperLink::OnSetCursor(
- CWnd* /*pWnd*/,
- UINT /*nHitTest*/,
- UINT /*message*/)
- {
- if (m_hLinkCursor)
- {
- ::SetCursor(m_hLinkCursor);
- return TRUE;
- }
- return FALSE;
- }
② 定义函数PreSubclassWindow()实现鼠标移动事件,具体代码如下: - void CHyperLink::PreSubclassWindow()
- {
- // 获得鼠标单击事件
- DWORD dwStyle = GetStyle();
- ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
-
- // 如果URL为空,设定为窗体名称
- if (m_strURL.IsEmpty())
- GetWindowText(m_strURL);
-
- // 同时检查窗体标题是否为空,如果为空则设定为URL
- CString strWndText;
- GetWindowText(strWndText);
- if (strWndText.IsEmpty()) {
- ASSERT(!m_strURL.IsEmpty());
- SetWindowText(m_strURL);
- }
-
- // 创建字体
- LOGFONT lf;
- GetFont()->GetLogFont(&lf);
- lf.lfUnderline = m_bUnderline;
- m_Font.CreateFontIndirect(&lf);
- SetFont(&m_Font);
-
- PositionWindow(); // 调整窗体大小
- SetDefaultCursor(); // 设定默认鼠标形状
-
- //创建提示信息
- CRect rect;
- GetClientRect(rect);
- m_ToolTip.Create(this);
- m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
-
- CStatic::PreSubclassWindow();
- }