1.3.3 小试牛刀--编程实现写邮件超级链接(4)

2013-10-07 15:52:56 · 作者: · 浏览: 83

1.3.3  小试牛刀--编程(www.cppentry.com)实现写邮件超级链接(4)

⑥ 定义函数SetLinkCursor()和GetLinkCursor(),用于分别设定鼠标的形状和获取鼠标的形状,具体代码如下:

  1. void CHyperLink::SetLinkCursor(HCURSOR hCursor)  
  2. {   
  3. m_hLinkCursor = hCursor;  
  4. if (m_hLinkCursor == NULL)  
  5. SetDefaultCursor();  
  6. }  
  7.  
  8. HCURSOR CHyperLink::GetLinkCursor() const  
  9. {  
  10. return m_hLinkCursor;  
  11. }  

⑦ 定义函数SetUnderline()和GetUnderline(),分别用于设置是否有下划线和获取是否具有下划线,具体代码如下:
  1. //设置下划线  
  2. void CHyperLink::SetUnderline(BOOL bUnderline /* = TRUE */)  
  3. {  
  4. m_bUnderline = bUnderline;  
  5.  
  6. if (::IsWindow(GetSafeHwnd()))  
  7. {  
  8. LOGFONT lf;  
  9. GetFont()->GetLogFont(&lf);  
  10. lf.lfUnderline = m_bUnderline;  
  11. m_Font.DeleteObject();  
  12. m_Font.CreateFontIndirect(&lf);  
  13. SetFont(&m_Font);  
  14. Invalidate();   
  15. }  
  16. }  
  17.  
  18. BOOL CHyperLink::GetUnderline() const  
  19. {   
  20. return m_bUnderline;   
  21. }  

⑧ 定义函数SetAutoSize()和GetAutoSize(),分别用于设置和获取是否是自动改变大小,具体代码如下:
  1. void CHyperLink::SetAutoSize(BOOL bAutoSize /* = TRUE */)  
  2. {  
  3. m_bAdjustToFit = bAutoSize;  
  4. if (::IsWindow(GetSafeHwnd()))  
  5. PositionWindow();  
  6. }  
  7.  
  8. BOOL CHyperLink::GetAutoSize() const  
  9. {   
  10. return m_bAdjustToFit;   
  11. }  

⑨ 定义函数PositionWindow(),用于调整窗体的大小,具体代码如下:
  1. void CHyperLink::PositionWindow()  
  2. {  
  3. if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit)   
  4. return;  
  5. CRect rect;  
  6. GetWindowRect(rect);  
  7. CWnd *pParent = GetParent();  
  8. if (pParent)  
  9. pParent->ScreenToClient(rect);  
  10. CString strWndText;  
  11. GetWindowText(strWndText);  
  12. CDC *pDC = GetDC();  
  13. CFont *pOldFont = pDC->SelectObject(&m_Font);  
  14. CSize Extent = pDC->GetTextExtent(strWndText);  
  15. pDC->SelectObject(pOldFont);  
  16. ReleaseDC(pDC);  
  17. DWORD dwStyle = GetStyle();  
  18. if (dwStyle & SS_CENTERIMAGE)  
  19. rect.DeflateRect(0, (rect.Height() - Extent.cy)/2);  
  20. else  
  21. rectrect.bottom = rect.top + Extent.cy;  
  22. if (dwStyle & SS_CENTER)     
  23. rect.DeflateRect((rect.Width() - Extent.cx)/2, 0);  
  24. else if (dwStyle & SS_RIGHT)   
  25. rectrect.left  = rect.right - Extent.cx;  
  26. else   
  27. rectrect.right = rect.left + Extent.cx;  
  28. SetWindowPos(NULL, rect.left, rect.top,   
  29. rect.Width(), rect.Height(), SWP_NOZORDER);  
  30. }