VC中顯示文本的控件實(shí)在用著不爽,于是自己查了一些資料,自己寫(xiě)一個(gè)類,與大家分享:)
- 頭文件:TFLinkStaticCtrl.h
- #if !defined(AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_)
- #define AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_
-
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
-
-
-
-
-
-
-
-
-
-
-
-
-
- class CTFLinkStaticCtrl : public CStatic
- {
-
- public:
- CTFLinkStaticCtrl();
-
-
- public:
- COLORREF TextColorNormal;
- COLORREF TextColorMouseHover;
- BOOL UnderlineNormal;
- BOOL UnderlineMouseHover;
- BOOL Linked;
-
-
- public:
-
- void SetWindowText(LPCTSTR lpString);
- CString GetWindowText();
- int GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const;
- void GetWindowText(CString& rString) const;
-
- void SetFont(LPLOGFONT plf);
-
- void OpenUrl(LPCTSTR lpszUrl);
-
-
-
-
- protected:
- virtual void PreSubclassWindow();
-
-
-
- public:
- virtual ~CTFLinkStaticCtrl();
-
-
- protected:
-
- afx_msg void OnPaint();
- afx_msg void OnMouseMove(UINT nFlags, CPoint point);
- afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
- afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
-
- afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
- afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
- DECLARE_MESSAGE_MAP()
- private:
-
- void InitControl();
-
- CFont* ControlFont;
-
- BOOL IsMouseHovered;
-
- BOOL IsLButtonDown;
-
- BOOL m_bTracking;
-
- CBitmap *m_pBackupBackground;
-
- CRect RectText;
-
- CString WindowText;
- };
-
-
-
-
-
-
- #endif // !defined(AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_)
-
- 實(shí)現(xiàn)文件:TFLinkStaticCtrl.cpp
-
-
-
- #include "stdafx.h"
- #include "TFLinkStaticCtrl.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- #if(WINVER < 0x500)
- #undef WINVER
- #define WINVER 0x500
- #define IDC_HAND MAKEINTRESOURCE(32649)
- #endif
-
-
-
-
-
-
-
-
-
-
-
- CTFLinkStaticCtrl::CTFLinkStaticCtrl()
- {
-
- ControlFont = NULL;
- IsMouseHovered = FALSE;
- IsLButtonDown = FALSE;
- m_bTracking = FALSE;
- TextColorNormal = RGB(0,0,0);
- TextColorMouseHover = RGB(0,0,0);
- UnderlineNormal = FALSE;
- UnderlineMouseHover = FALSE;
- m_pBackupBackground = NULL;
- Linked = FALSE;
- }
-
- CTFLinkStaticCtrl::~CTFLinkStaticCtrl()
- {
-
- if(ControlFont)
- {
- ControlFont->DeleteObject();
- delete ControlFont;
- ControlFont = NULL;
- }
-
- if(m_pBackupBackground)
- {
- m_pBackupBackground->DeleteObject();
- delete m_pBackupBackground;
- m_pBackupBackground = NULL;
- }
- }
-
-
- BEGIN_MESSAGE_MAP(CTFLinkStaticCtrl, CStatic)
-
- ON_WM_PAINT()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
-
- ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
- ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
- END_MESSAGE_MAP()
-
-
-
-
-
- void CTFLinkStaticCtrl::InitControl()
- {
-
- if(ControlFont)
- return;
-
- ModifyStyle(0, SS_NOTIFY|BS_OWNERDRAW);
-
- CWnd* pWnd = GetParent();
- if(!pWnd)
- return;
-
- LOGFONT lf;
- pWnd->GetFont()->GetLogFont(&lf);
-
- ControlFont = new CFont();
- ControlFont->CreateFontIndirect(&lf);
- CStatic::SetFont(ControlFont);
- CStatic::GetWindowText(WindowText);
- }
-
-
- void CTFLinkStaticCtrl::SetFont(LPLOGFONT plf)
- {
-
- if(plf == NULL)
- return;
-
- plf->lfUnderline = FALSE;
- CFont* pFont = new CFont();
- pFont->CreateFontIndirect(plf);
- CStatic::SetFont(pFont);
- delete ControlFont;
- ControlFont = pFont;
- }
-
- void CTFLinkStaticCtrl::OnPaint()
- {
-
-
- CPaintDC dc(this);
-
- dc.SetBkMode(TRANSPARENT);
-
- CFont* pOldFont;
- pOldFont = (CFont*)dc.SelectObject(ControlFont);
-
- TEXTMETRIC tmText;
- dc.GetTextMetrics(&tmText);
- CString Text;
-
- GetWindowText(Text);
- dc.SetTextColor(TextColorNormal);
-
- if(m_pBackupBackground == NULL)
- {
- CDC dcMem;
- dcMem.CreateCompatibleDC(&dc);
- GetClientRect(&RectText);
- m_pBackupBackground = new CBitmap();
- m_pBackupBackground->CreateCompatibleBitmap(&dc, RectText.Width(), RectText.Height());
- dcMem.SelectObject(m_pBackupBackground);
- dcMem.BitBlt(0, 0, RectText.Width(), RectText.Height(), &dc, 0, 0, SRCCOPY);
- dcMem.DeleteDC();
- }
- else
- {
-
- CDC dcTmp;
- dcTmp.CreateCompatibleDC(&dc);
- dcTmp.SelectObject(m_pBackupBackground);
- dc.BitBlt(0, 0, RectText.Width(), RectText.Height()+1, &dcTmp, 0, 0, SRCCOPY);
- dcTmp.DeleteDC();
-
-
- CRect RectTmp;
- GetClientRect(&RectTmp);
-
- if(RectTmp != RectText)
- {
- RectText = RectTmp;
- m_pBackupBackground->DeleteObject();
-
- CDC dcMem;
- dcMem.CreateCompatibleDC(&dc);
- m_pBackupBackground->CreateCompatibleBitmap(&dc, RectText.Width(), RectText.Height());
- dcMem.SelectObject(m_pBackupBackground);
- dcMem.BitBlt(0, 0, RectText.Width(), RectText.Height(), &dc, 0, 0, SRCCOPY);
- dcMem.DeleteDC();
- }
- }
-
- if(Linked)
- {
-
- if(IsMouseHovered)
- {
-
- dc.SetTextColor(TextColorMouseHover);
-
- if(UnderlineMouseHover)
- {
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, 1, TextColorMouseHover);
- pOldPen = (CPen*)dc.SelectObject(&Pen);
- dc.MoveTo(0, tmText.tmHeight);
- dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);
- dc.SelectObject(pOldPen);
- Pen.DeleteObject();
- }
- }
- else
- {
-
- dc.SetTextColor(TextColorNormal);
- if(UnderlineNormal)
- {
-
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, 1, TextColorNormal);
- pOldPen = (CPen*)dc.SelectObject(&Pen);
- dc.MoveTo(0, tmText.tmHeight);
- dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);
- dc.SelectObject(pOldPen);
- Pen.DeleteObject();
- }
- }
- }
- else
- {
- if(UnderlineNormal)
- {
-
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, 1, TextColorNormal);
- pOldPen = (CPen*)dc.SelectObject(&Pen);
- dc.MoveTo(0, tmText.tmHeight);
- dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);
- dc.SelectObject(pOldPen);
- Pen.DeleteObject();
- }
- dc.SetTextColor(TextColorNormal);
- }
-
- dc.TextOut(0, 0, Text);
- dc.SelectObject(pOldFont);
- }
-
- void CTFLinkStaticCtrl::OnMouseMove(UINT nFlags, CPoint point)
- {
-
- if(!Linked)
- return;
-
- if (!m_bTracking)
- {
- TRACKMOUSEEVENT tme;
- tme.cbSize = sizeof(tme);
- tme.hwndTrack = m_hWnd;
- tme.dwFlags = TME_LEAVE | TME_HOVER;
- tme.dwHoverTime = 1;
- m_bTracking = _TrackMouseEvent(&tme);
- }
- CStatic::OnMouseMove(nFlags, point);
- }
-
- LRESULT CTFLinkStaticCtrl::OnMouseLeave(WPARAM wParam, LPARAM lParam)
- {
- m_bTracking = FALSE;
- IsMouseHovered = FALSE;
- HCURSOR hCursor = SetCursor(::LoadCursor(NULL, IDC_ARROW));
- Invalidate();
- return 0;
- }
-
- LRESULT CTFLinkStaticCtrl::OnMouseHover(WPARAM wParam, LPARAM lParam)
- {
- m_bTracking = TRUE;
- IsMouseHovered = TRUE;
- HCURSOR hCursor = SetCursor(::LoadCursor(NULL, IDC_HAND));
- Invalidate();
- return 0;
- }
-
- void CTFLinkStaticCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
-
- if(!Linked)
- return;
- SetCapture();
- IsLButtonDown = TRUE;
- BringWindowToTop();
- CStatic::OnLButtonDown(nFlags, point);
- }
-
- void CTFLinkStaticCtrl::OnLButtonUp(UINT nFlags, CPoint point)
- {
-
- if(IsLButtonDown)
- {
-
- IsLButtonDown = FALSE;
- ReleaseCapture();
- CWnd* pWnd = GetParent();
- if(pWnd)
- {
- pWnd->SendMessage(BN_CLICKED, (WPARAM)GetDlgCtrlID(), (LPARAM)GetSafeHwnd());
- }
- }
- CStatic::OnLButtonUp(nFlags, point);
- }
-
- void CTFLinkStaticCtrl::PreSubclassWindow()
- {
-
- InitControl();
- CStatic::PreSubclassWindow();
- }
-
- void CTFLinkStaticCtrl::SetWindowText(LPCTSTR lpString)
- {
- WindowText = lpString;
- Invalidate();
- }
-
- CString CTFLinkStaticCtrl::GetWindowText()
- {
- return WindowText;
- }
-
- int CTFLinkStaticCtrl::GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const
- {
- int nLen = WindowText.GetLength();
- if(nLen <= nMaxCount)
- {
- _stprintf(lpszStringBuf, _T("%s"), WindowText);
- return nLen;
- }
- else
- {
- _stprintf(lpszStringBuf, _T("%s"), WindowText.Left(nMaxCount));
- return nMaxCount;
- }
- }
-
- void CTFLinkStaticCtrl::GetWindowText(CString& rString) const
- {
- rString = WindowText;
- }
-
- void CTFLinkStaticCtrl::OpenUrl(LPCTSTR lpszUrl)
- {
- ShellExecute(NULL, _T("open"), lpszUrl, NULL, NULL, SW_SHOW);
- }
使用說(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