天刀觉智大师线索:靜态文本 链接控件 背景透明的静态文本

来源:百度文库 编辑:中财网 时间:2024/06/28 10:51:14

VC中显示文本的控件实在用着不爽,于是自己查了一些资料,自己写一个类,与大家分享:)

 view plaincopy to clipboardprint?

  1. 头文件:TFLinkStaticCtrl.h  
  2. #if !defined(AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_)  
  3. #define AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_  
  4.  
  5. #if _MSC_VER > 1000  
  6. #pragma once  
  7. #endif // _MSC_VER > 1000   
  8. // TFLinkStaticCtrl.h : header file   
  9. //   
  10.   
  11. //////////////////////////////////////////////////////////////////////////   
  12. //*功能: 靜态文本/链接/背景透明   
  13. //*作者: 童方   
  14. //*联系:QQ:58408454 Mail:shfhere@qq.com   
  15. //*时间: 2010年4月整理   
  16. //////////////////////////////////////////////////////////////////////////   
  17.   
  18. /////////////////////////////////////////////////////////////////////////////   
  19. // CTFLinkStaticCtrl window   
  20.   
  21. class CTFLinkStaticCtrl : public CStatic   
  22. {   
  23. // Construction   
  24. public:   
  25.     CTFLinkStaticCtrl();   
  26.   
  27. // Attributes   
  28. public:   
  29.     COLORREF TextColorNormal;       //*文本颜色   
  30.     COLORREF TextColorMouseHover;   //*鼠标移到控件上面时的文本颜色(链接功能时有效)   
  31.     BOOL UnderlineNormal;           //*是否带下划线   
  32.     BOOL UnderlineMouseHover;       //*鼠标移到控件上面时是否带下划线(链接功能时有效)   
  33.     BOOL Linked;                    //*是否开启链接功能   
  34.   
  35. // Operations   
  36. public:   
  37.     //*重载处理文本内容函数   
  38.     void SetWindowText(LPCTSTR lpString);   
  39.     CString GetWindowText();   
  40.     int GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const;    
  41.     void GetWindowText(CString& rString) const;   
  42.     //*设置字体   
  43.     void SetFont(LPLOGFONT plf);   
  44.     //*打开网址,可以控件的OnClick事件中调用   
  45.     void OpenUrl(LPCTSTR lpszUrl);   
  46.   
  47. // Overrides   
  48.     // ClassWizard generated virtual function overrides   
  49.     //{{AFX_VIRTUAL(CTFLinkStaticCtrl)   
  50.     protected:   
  51.     virtual void PreSubclassWindow();   
  52.     //}}AFX_VIRTUAL   
  53.   
  54. // Implementation   
  55. public:   
  56.     virtual ~CTFLinkStaticCtrl();   
  57.   
  58.     // Generated message map functions   
  59. protected:   
  60.     //{{AFX_MSG(CTFStatic)   
  61.     afx_msg void OnPaint();   
  62.     afx_msg void OnMouseMove(UINT nFlags, CPoint point);   
  63.     afx_msg void OnLButtonDown(UINT nFlags, CPoint point);   
  64.     afx_msg void OnLButtonUp(UINT nFlags, CPoint point);   
  65.     //}}AFX_MSG   
  66.     afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);   
  67.     afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);   
  68.     DECLARE_MESSAGE_MAP()   
  69. private:   
  70.     //*初始化控件   
  71.     void InitControl();   
  72.     //*控件字体   
  73.     CFont* ControlFont;   
  74.     //*鼠标是否在控件上面   
  75.     BOOL IsMouseHovered;   
  76.     //*是否按下鼠标左键   
  77.     BOOL IsLButtonDown;   
  78.     //*处理MouseMove事件时的标志变量   
  79.     BOOL m_bTracking;   
  80.     //*开启链接效果时父窗口背景备份   
  81.     CBitmap *m_pBackupBackground;   
  82.     //*文本占用区域   
  83.     CRect RectText;   
  84.     //*文本内容   
  85.     CString WindowText;   
  86. };   
  87.   
  88. /////////////////////////////////////////////////////////////////////////////   
  89.   
  90. //{{AFX_INSERT_LOCATION}}   
  91. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.  
  92.  
  93. #endif // !defined(AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_)   
  94.   
  95. 实现文件:TFLinkStaticCtrl.cpp   
  96. // TFLinkStaticCtrl.cpp : implementation file   
  97. //  
  98.  
  99. #include "stdafx.h"  
  100. #include "TFLinkStaticCtrl.h"  
  101.  
  102. #ifdef _DEBUG  
  103. #define new DEBUG_NEW  
  104. #undef THIS_FILE   
  105. static char THIS_FILE[] = __FILE__;  
  106. #endif  
  107.  
  108. #if(WINVER < 0x500)  
  109. #undef WINVER  
  110. #define WINVER 0x500  
  111. #define IDC_HAND            MAKEINTRESOURCE(32649)  
  112. #endif   
  113.   
  114. //////////////////////////////////////////////////////////////////////////   
  115. //*功能: 靜态文本/链接/背景透明   
  116. //*作者: 童方   
  117. //*联系:QQ:58408454 Mail:shfhere@qq.com   
  118. //*时间: 2010年4月整理   
  119. //////////////////////////////////////////////////////////////////////////   
  120.   
  121. /////////////////////////////////////////////////////////////////////////////   
  122. // CTFLinkStaticCtrl   
  123.   
  124. CTFLinkStaticCtrl::CTFLinkStaticCtrl()   
  125. {   
  126.     //*设置成员变量初始值   
  127.     ControlFont = NULL;   
  128.     IsMouseHovered = FALSE;   
  129.     IsLButtonDown = FALSE;   
  130.     m_bTracking = FALSE;   
  131.     TextColorNormal = RGB(0,0,0);   
  132.     TextColorMouseHover = RGB(0,0,0);   
  133.     UnderlineNormal = FALSE;   
  134.     UnderlineMouseHover = FALSE;   
  135.     m_pBackupBackground = NULL;   
  136.     Linked = FALSE;   
  137. }   
  138.   
  139. CTFLinkStaticCtrl::~CTFLinkStaticCtrl()   
  140. {   
  141.     //*释放字体对象   
  142.     if(ControlFont)   
  143.     {   
  144.         ControlFont->DeleteObject();   
  145.         delete ControlFont;   
  146.         ControlFont = NULL;   
  147.     }   
  148.     //*释放背景位图对象   
  149.     if(m_pBackupBackground)   
  150.     {   
  151.         m_pBackupBackground->DeleteObject();   
  152.         delete m_pBackupBackground;   
  153.         m_pBackupBackground = NULL;   
  154.     }   
  155. }   
  156.   
  157.   
  158. BEGIN_MESSAGE_MAP(CTFLinkStaticCtrl, CStatic)   
  159.     //{{AFX_MSG_MAP(CTFLinkStaticCtrl)   
  160.     ON_WM_PAINT()   
  161.     ON_WM_MOUSEMOVE()   
  162.     ON_WM_LBUTTONDOWN()   
  163.     ON_WM_LBUTTONUP()   
  164.     //}}AFX_MSG_MAP   
  165.     ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)   
  166.     ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)   
  167. END_MESSAGE_MAP()   
  168.   
  169. /////////////////////////////////////////////////////////////////////////////   
  170. // CTFLinkStaticCtrl message handlers   
  171.   
  172. //*初始化控件   
  173. void CTFLinkStaticCtrl::InitControl()   
  174. {   
  175.     //*如果已经初始化,则返回   
  176.     if(ControlFont)   
  177.         return;   
  178.     //*修改控件样式,使控件可以收到鼠标消息通知   
  179.     ModifyStyle(0, SS_NOTIFY|BS_OWNERDRAW);   
  180.     //*得到父窗口   
  181.     CWnd* pWnd = GetParent();   
  182.     if(!pWnd)   
  183.         return;   
  184.     //*得到父窗口字体信息   
  185.     LOGFONT lf;   
  186.     pWnd->GetFont()->GetLogFont(&lf);   
  187.     //*将当前控件字体设置为父窗口字体   
  188.     ControlFont = new CFont();   
  189.     ControlFont->CreateFontIndirect(&lf);   
  190.     CStatic::SetFont(ControlFont);   
  191.     CStatic::GetWindowText(WindowText);   
  192. }   
  193.   
  194. //*设置字体   
  195. void CTFLinkStaticCtrl::SetFont(LPLOGFONT plf)   
  196. {   
  197.     //*判断传如参数是否为空   
  198.     if(plf == NULL)   
  199.         return;   
  200.     //*去掉下划线属性, 因为在OnPaint中手动绘下划线了   
  201.     plf->lfUnderline = FALSE;   
  202.     CFont* pFont = new CFont();   
  203.     pFont->CreateFontIndirect(plf);   
  204.     CStatic::SetFont(pFont);   
  205.     delete ControlFont;   
  206.     ControlFont = pFont;   
  207. }   
  208.   
  209. void CTFLinkStaticCtrl::OnPaint()    
  210. {   
  211.     //*初始化控件   
  212. //  InitControl();   
  213.     CPaintDC dc(this);   
  214.     //*设置背景模式为透明   
  215.     dc.SetBkMode(TRANSPARENT);   
  216.     //*载入控件当前字体   
  217.     CFont* pOldFont;   
  218.     pOldFont = (CFont*)dc.SelectObject(ControlFont);   
  219.     //*得到字体信息   
  220.     TEXTMETRIC tmText;   
  221.     dc.GetTextMetrics(&tmText);   
  222.     CString Text;   
  223.     //*得到窗口文本   
  224.     GetWindowText(Text);   
  225.     dc.SetTextColor(TextColorNormal);   
  226.     //*如果首次运行, 备份背景内容   
  227.     if(m_pBackupBackground == NULL)   
  228.     {   
  229.         CDC dcMem;   
  230.         dcMem.CreateCompatibleDC(&dc);   
  231.         GetClientRect(&RectText);   
  232.         m_pBackupBackground = new CBitmap();   
  233.         m_pBackupBackground->CreateCompatibleBitmap(&dc, RectText.Width(), RectText.Height());   
  234.         dcMem.SelectObject(m_pBackupBackground);   
  235.         dcMem.BitBlt(0, 0, RectText.Width(), RectText.Height(), &dc, 0, 0, SRCCOPY);   
  236.         dcMem.DeleteDC();   
  237.     }   
  238.     else  
  239.     {   
  240.         //*非首次运行时, 将背景还原   
  241.         CDC dcTmp;   
  242.         dcTmp.CreateCompatibleDC(&dc);   
  243.         dcTmp.SelectObject(m_pBackupBackground);   
  244.         dc.BitBlt(0, 0, RectText.Width(), RectText.Height()+1, &dcTmp, 0, 0, SRCCOPY);   
  245.         dcTmp.DeleteDC();   
  246.            
  247.         //*计算当前文本区域   
  248.         CRect RectTmp;   
  249.         GetClientRect(&RectTmp);   
  250.         //*如果与原来文本区域不同, 则重新备份背景区域   
  251.         if(RectTmp != RectText)   
  252.         {   
  253.             RectText = RectTmp;   
  254.             m_pBackupBackground->DeleteObject();   
  255.                
  256.             CDC dcMem;   
  257.             dcMem.CreateCompatibleDC(&dc);   
  258.             m_pBackupBackground->CreateCompatibleBitmap(&dc, RectText.Width(), RectText.Height());   
  259.             dcMem.SelectObject(m_pBackupBackground);   
  260.             dcMem.BitBlt(0, 0, RectText.Width(), RectText.Height(), &dc, 0, 0, SRCCOPY);   
  261.             dcMem.DeleteDC();   
  262.         }   
  263.     }   
  264.     //*如果开启链接效果   
  265.     if(Linked)   
  266.     {   
  267.         //*如果鼠标移动到控件上面   
  268.         if(IsMouseHovered)   
  269.         {   
  270.             //*设置当前文本颜色为TextColorMouseHover   
  271.             dc.SetTextColor(TextColorMouseHover);   
  272.             //*如果显示下划线, 则绘制下划线   
  273.             if(UnderlineMouseHover)   
  274.             {   
  275.                 CPen Pen, *pOldPen;   
  276.                 Pen.CreatePen(PS_SOLID, 1, TextColorMouseHover);   
  277.                 pOldPen = (CPen*)dc.SelectObject(&Pen);   
  278.                 dc.MoveTo(0, tmText.tmHeight);   
  279.                 dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);   
  280.                 dc.SelectObject(pOldPen);   
  281.                 Pen.DeleteObject();   
  282.             }   
  283.         }   
  284.         else  
  285.         {   
  286.             //*如果鼠标不在控件上面, 则正常绘制   
  287.             dc.SetTextColor(TextColorNormal);   
  288.             if(UnderlineNormal)   
  289.             {   
  290.                 //*绘制下划线   
  291.                 CPen Pen, *pOldPen;   
  292.                 Pen.CreatePen(PS_SOLID, 1, TextColorNormal);   
  293.                 pOldPen = (CPen*)dc.SelectObject(&Pen);   
  294.                 dc.MoveTo(0, tmText.tmHeight);   
  295.                 dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);   
  296.                 dc.SelectObject(pOldPen);   
  297.                 Pen.DeleteObject();   
  298.             }   
  299.         }   
  300.     }   
  301.     else  
  302.     {   
  303.         if(UnderlineNormal)   
  304.         {   
  305.             //*绘制下划线   
  306.             CPen Pen, *pOldPen;   
  307.             Pen.CreatePen(PS_SOLID, 1, TextColorNormal);   
  308.             pOldPen = (CPen*)dc.SelectObject(&Pen);   
  309.             dc.MoveTo(0, tmText.tmHeight);   
  310.             dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);   
  311.             dc.SelectObject(pOldPen);   
  312.             Pen.DeleteObject();   
  313.         }   
  314.         dc.SetTextColor(TextColorNormal);   
  315.     }   
  316.     //*绘制文本   
  317.     dc.TextOut(0, 0, Text);   
  318.     dc.SelectObject(pOldFont);   
  319. }   
  320.   
  321. void CTFLinkStaticCtrl::OnMouseMove(UINT nFlags, CPoint point)    
  322. {   
  323.     // TODO: Add your message handler code here and/or call default   
  324.     if(!Linked)   
  325.         return;   
  326.     //*分别处理鼠标移到控件上与移出控件的消息   
  327.     if (!m_bTracking)   
  328.     {   
  329.         TRACKMOUSEEVENT tme;   
  330.         tme.cbSize = sizeof(tme);   
  331.         tme.hwndTrack = m_hWnd;   
  332.         tme.dwFlags = TME_LEAVE | TME_HOVER;   
  333.         tme.dwHoverTime = 1;   
  334.         m_bTracking = _TrackMouseEvent(&tme);   
  335.     }   
  336.     CStatic::OnMouseMove(nFlags, point);   
  337. }   
  338.   
  339. LRESULT CTFLinkStaticCtrl::OnMouseLeave(WPARAM wParam, LPARAM lParam)   
  340. {   
  341.     m_bTracking = FALSE;   
  342.     IsMouseHovered = FALSE;   
  343.     HCURSOR hCursor = SetCursor(::LoadCursor(NULL, IDC_ARROW));   
  344.     Invalidate();   
  345.     return 0;   
  346. }   
  347.   
  348. LRESULT CTFLinkStaticCtrl::OnMouseHover(WPARAM wParam, LPARAM lParam)   
  349. {   
  350.     m_bTracking = TRUE;   
  351.     IsMouseHovered = TRUE;   
  352.     HCURSOR hCursor = SetCursor(::LoadCursor(NULL, IDC_HAND));   
  353.     Invalidate();   
  354.     return 0;   
  355. }   
  356.   
  357. void CTFLinkStaticCtrl::OnLButtonDown(UINT nFlags, CPoint point)    
  358. {   
  359.     // TODO: Add your message handler code here and/or call default   
  360.     if(!Linked)   
  361.         return;   
  362.     SetCapture();   
  363.     IsLButtonDown = TRUE;   
  364.     BringWindowToTop();   
  365.     CStatic::OnLButtonDown(nFlags, point);   
  366. }   
  367.   
  368. void CTFLinkStaticCtrl::OnLButtonUp(UINT nFlags, CPoint point)    
  369. {   
  370.     // TODO: Add your message handler code here and/or call default   
  371.     if(IsLButtonDown)   
  372.     {   
  373.         //*向父窗口发送点击消息   
  374.         IsLButtonDown = FALSE;   
  375.         ReleaseCapture();   
  376.         CWnd* pWnd = GetParent();   
  377.         if(pWnd)   
  378.         {   
  379.             pWnd->SendMessage(BN_CLICKED, (WPARAM)GetDlgCtrlID(), (LPARAM)GetSafeHwnd());   
  380.         }   
  381.     }   
  382.     CStatic::OnLButtonUp(nFlags, point);   
  383. }   
  384.   
  385. void CTFLinkStaticCtrl::PreSubclassWindow()    
  386. {   
  387.     // TODO: Add your specialized code here and/or call the base class   
  388.     InitControl();   
  389.     CStatic::PreSubclassWindow();   
  390. }   
  391.   
  392. void CTFLinkStaticCtrl::SetWindowText(LPCTSTR lpString)   
  393. {   
  394.     WindowText = lpString;   
  395.     Invalidate();   
  396. }   
  397.   
  398. CString CTFLinkStaticCtrl::GetWindowText()   
  399. {   
  400.     return WindowText;   
  401. }   
  402.   
  403. int CTFLinkStaticCtrl::GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const  
  404. {   
  405.     int nLen = WindowText.GetLength();   
  406.     if(nLen <= nMaxCount)   
  407.     {   
  408.         _stprintf(lpszStringBuf, _T("%s"), WindowText);   
  409.         return nLen;   
  410.     }   
  411.     else  
  412.     {   
  413.         _stprintf(lpszStringBuf, _T("%s"), WindowText.Left(nMaxCount));   
  414.         return nMaxCount;   
  415.     }   
  416. }   
  417.   
  418. void CTFLinkStaticCtrl::GetWindowText(CString& rString) const  
  419. {   
  420.     rString = WindowText;   
  421. }   
  422.   
  423. void CTFLinkStaticCtrl::OpenUrl(LPCTSTR lpszUrl)   
  424. {   
  425.     ShellExecute(NULL, _T("open"), lpszUrl, NULL, NULL, SW_SHOW);   
  426. }  
  

 

使用说明:

1、将该类加入到工程

2、在窗口上添加Static控件并设置相应的ID

3、映射为CTFLinkStaticCtrl类的成员变量

4、初始化

如:m_Link, m_Show

链接效果初始化

 m_Link.TextColorNormal = RGB(0,0,255);
 m_Link.TextColorMouseHover = RGB(0,0,255);
 m_Link.UnderlineNormal = FALSE;
 m_Link.UnderlineMouseHover = TRUE;
 m_Link.Linked = TRUE;

静态文本初始化

 m_Show.TextColorNormal = RGB(255,0,0);
 m_Show.Linked = FALSE;

5、响应点击(只有Linked属性设置为TRUE才响应此事件)

用向导为相应的Static添加OnClick事件

void YourClass::OnLinkClick()

{

    //如果转向网页,则调用

    m_Link.OpenURL(www.baidu.com);

    //如果响应其他功能,则相当于按钮的点击事件,添加你需要的功能

}

示例代码:http://download.csdn.net/source/2230734