国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
靜態(tài)文本 鏈接控件 背景透明的靜態(tài)文本

VC中顯示文本的控件實(shí)在用著不爽,于是自己查了一些資料,自己寫(xiě)一個(gè)類,與大家分享:)

 

  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. //*功能: 靜態(tài)文本/鏈接/背景透明   
  13. //*作者: 童方   
  14. //*聯(lián)系:QQ:58408454 Mail:shfhere@qq.com   
  15. //*時(shí)間: 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;   //*鼠標(biāo)移到控件上面時(shí)的文本顏色(鏈接功能時(shí)有效)   
  31.     BOOL UnderlineNormal;           //*是否帶下劃線   
  32.     BOOL UnderlineMouseHover;       //*鼠標(biāo)移到控件上面時(shí)是否帶下劃線(鏈接功能時(shí)有效)   
  33.     BOOL Linked;                    //*是否開(kāi)啟鏈接功能   
  34.   
  35. // Operations   
  36. public:   
  37.     //*重載處理文本內(nèi)容函數(shù)   
  38.     void SetWindowText(LPCTSTR lpString);   
  39.     CString GetWindowText();   
  40.     int GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const;    
  41.     void GetWindowText(CString& rString) const;   
  42.     //*設(shè)置字體   
  43.     void SetFont(LPLOGFONT plf);   
  44.     //*打開(kāi)網(wǎng)址,可以控件的OnClick事件中調(diào)用   
  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.     //*鼠標(biāo)是否在控件上面   
  75.     BOOL IsMouseHovered;   
  76.     //*是否按下鼠標(biāo)左鍵   
  77.     BOOL IsLButtonDown;   
  78.     //*處理MouseMove事件時(shí)的標(biāo)志變量   
  79.     BOOL m_bTracking;   
  80.     //*開(kāi)啟鏈接效果時(shí)父窗口背景備份   
  81.     CBitmap *m_pBackupBackground;   
  82.     //*文本占用區(qū)域   
  83.     CRect RectText;   
  84.     //*文本內(nèi)容   
  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. 實(shí)現(xiàn)文件: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. //*功能: 靜態(tài)文本/鏈接/背景透明   
  116. //*作者: 童方   
  117. //*聯(lián)系:QQ:58408454 Mail:shfhere@qq.com   
  118. //*時(shí)間: 2010年4月整理   
  119. //////////////////////////////////////////////////////////////////////////   
  120.   
  121. /////////////////////////////////////////////////////////////////////////////   
  122. // CTFLinkStaticCtrl   
  123.   
  124. CTFLinkStaticCtrl::CTFLinkStaticCtrl()   
  125. {   
  126.     //*設(shè)置成員變量初始值   
  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.     //*釋放字體對(duì)象   
  142.     if(ControlFont)   
  143.     {   
  144.         ControlFont->DeleteObject();   
  145.         delete ControlFont;   
  146.         ControlFont = NULL;   
  147.     }   
  148.     //*釋放背景位圖對(duì)象   
  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.     //*如果已經(jīng)初始化,則返回   
  176.     if(ControlFont)   
  177.         return;   
  178.     //*修改控件樣式,使控件可以收到鼠標(biāo)消息通知   
  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.     //*將當(dāng)前控件字體設(shè)置為父窗口字體   
  188.     ControlFont = new CFont();   
  189.     ControlFont->CreateFontIndirect(&lf);   
  190.     CStatic::SetFont(ControlFont);   
  191.     CStatic::GetWindowText(WindowText);   
  192. }   
  193.   
  194. //*設(shè)置字體   
  195. void CTFLinkStaticCtrl::SetFont(LPLOGFONT plf)   
  196. {   
  197.     //*判斷傳如參數(shù)是否為空   
  198.     if(plf == NULL)   
  199.         return;   
  200.     //*去掉下劃線屬性, 因?yàn)樵贠nPaint中手動(dòng)繪下劃線了   
  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.     //*設(shè)置背景模式為透明   
  215.     dc.SetBkMode(TRANSPARENT);   
  216.     //*載入控件當(dāng)前字體   
  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.     //*如果首次運(yùn)行, 備份背景內(nèi)容   
  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.         //*非首次運(yùn)行時(shí), 將背景還原   
  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.         //*計(jì)算當(dāng)前文本區(qū)域   
  248.         CRect RectTmp;   
  249.         GetClientRect(&RectTmp);   
  250.         //*如果與原來(lái)文本區(qū)域不同, 則重新備份背景區(qū)域   
  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.     //*如果開(kāi)啟鏈接效果   
  265.     if(Linked)   
  266.     {   
  267.         //*如果鼠標(biāo)移動(dòng)到控件上面   
  268.         if(IsMouseHovered)   
  269.         {   
  270.             //*設(shè)置當(dāng)前文本顏色為T(mén)extColorMouseHover   
  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.             //*如果鼠標(biāo)不在控件上面, 則正常繪制   
  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.     //*分別處理鼠標(biāo)移到控件上與移出控件的消息   
  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.         //*向父窗口發(fā)送點(diǎn)擊消息   
  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. }  
  

 

使用說(shuō)明:

1、將該類加入到工程

2、在窗口上添加Static控件并設(shè)置相應(yīng)的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;

靜態(tài)文本初始化

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

5、響應(yīng)點(diǎn)擊(只有Linked屬性設(shè)置為T(mén)RUE才響應(yīng)此事件)

用向?qū)橄鄳?yīng)的Static添加OnClick事件

void YourClass::OnLinkClick()

{

    //如果轉(zhuǎn)向網(wǎng)頁(yè),則調(diào)用

    m_Link.OpenURL(www.baidu.com);

    //如果響應(yīng)其他功能,則相當(dāng)于按鈕的點(diǎn)擊事件,添加你需要的功能

}

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

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
MFC可編輯列表框控件功能實(shí)現(xiàn)
windows編程——背景圖片和透明特效的使用3
深入淺出VC++串口編程之基于Win32 API
MFC消息
深入淺出Win32多線程程序設(shè)計(jì)
MFC,實(shí)現(xiàn)三態(tài)樹(shù)形控件以及父子節(jié)點(diǎn)聯(lián)動(dòng)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服