#if !defined(AFX_HYPERLINKSTATIC_H__32A71426_1315_407C_9D90_A484C5589D80__INCLUDED_)#define AFX_HYPERLINKSTATIC_H__32A71426_1315_407C_9D90_A484C5589D80__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// HyperlinkStatic.h : header file///////////////////////////////////////////////////////////////////////////////// CHyperlinkStatic windowclass CHyperlinkStatic : public CStatic{// Constructionpublic:CHyperlinkStatic();// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CHyperlinkStatic)protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL// Implementationpublic:virtual ~CHyperlinkStatic();// Generated message map functionsprotected://{{AFX_MSG(CHyperlinkStatic)afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg void OnPaint();afx_msg void OnDestroy();afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);//}}AFX_MSGafx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);DECLARE_MESSAGE_MAP()public://*********************************************************************// function : 設定超級串連//*********************************************************************void SetHyperlink(CString strUrl);//*********************************************************************// function : 設定顯示文本//*********************************************************************void SetCaption(CString strCaption);//*********************************************************************// function : 設定預設顏色//*********************************************************************void SetDefaultColor(COLORREF color){m_crDefault = color;m_crCurrent = color;}//*********************************************************************// function : 設定滑鼠覆蓋顏色//*********************************************************************void SetOnMouseColor(COLORREF color){ m_crOnMouse = color; }//*********************************************************************// function : 修改字型大小//*********************************************************************void SetFontSizeIncrement(int nIncrement){m_nFontIncrement = nIncrement;}//*********************************************************************// function : 設定是否有底線//*********************************************************************void SetUnderLine(BOOL bUnderLine){m_bUnderLine = bUnderLine;}//*********************************************************************// function : 擷取文本尺寸//*********************************************************************CSize GetCaptionSize();private:CString _strCaption, _strHyperlink;CFont _fontCaption;CSize _sizeCaption;bool _bCreateFont, _bMouseInControl, _bGetCaptionSize;HCURSOR_hHandCursor, _hArrowCursor;void CreateFont(CFont *pFont, LONG lFontIncrement);void DrawText(CDC* pDC, COLORREF color);void SetCaptionSize();bool InCaptionRange(CPoint &point);//*********************************************************************// function : 設定制定文本在某個字型下的尺寸//*********************************************************************CSize GetTextSize(CFont *pFont, const CString &strText);private:int m_nFontIncrement;BOOL m_bUnderLine;COLORREF m_crDefault;// 預設字型顏色COLORREF m_crOnMouse;// 滑鼠覆蓋時的顏色COLORREF m_crCurrent;// 當前字型};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_HYPERLINKSTATIC_H__32A71426_1315_407C_9D90_A484C5589D80__INCLUDED_)
// HyperlinkStatic.cpp : implementation file//#include "stdafx.h"#include "HyperlinkStatic.h"#include "resource.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CHyperlinkStaticCHyperlinkStatic::CHyperlinkStatic() :m_nFontIncrement(0){_strCaption = _strHyperlink = _TT(""); _bMouseInControl = _bCreateFont = _bGetCaptionSize = false;_hHandCursor = ::LoadCursor(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDC_CUR_HAND));_hArrowCursor = ::LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW));ASSERT(_hHandCursor && _hArrowCursor);// 預設屬性m_bUnderLine = TRUE;// 有底線m_crDefault = RGB(0, 0, 255);// 藍色m_crOnMouse = m_crDefault;// 滑鼠覆蓋時,顏色不變m_crCurrent = m_crDefault;// 當前字型藍色}CHyperlinkStatic::~CHyperlinkStatic(){}BEGIN_MESSAGE_MAP(CHyperlinkStatic, CStatic)//{{AFX_MSG_MAP(CHyperlinkStatic)ON_WM_LBUTTONDOWN()ON_WM_PAINT()ON_WM_DESTROY()ON_WM_MOUSEMOVE()ON_WM_CREATE()//}}AFX_MSG_MAPON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CHyperlinkStatic message handlersvoid CHyperlinkStatic::SetHyperlink(CString strHyperlink){_strHyperlink = strHyperlink;}void CHyperlinkStatic::SetCaption(CString strCaption){_strCaption = strCaption;_bGetCaptionSize = false;Invalidate(TRUE);}void CHyperlinkStatic::OnLButtonDown(UINT nFlags, CPoint point) {if (!_bGetCaptionSize)SetCaptionSize();if (!_strHyperlink.IsEmpty() && InCaptionRange(point))ShellExecute(0, "open", _strHyperlink, 0, 0, SW_SHOWNORMAL);CStatic::OnLButtonDown(nFlags, point);}void CHyperlinkStatic::DrawText(CDC* pDC, COLORREF color){if (NULL == pDC)return;CFont *pOldFont = (CFont*)pDC->SelectObject(&_fontCaption);int nBkMode = pDC->SetBkMode(TRANSPARENT);COLORREF nOldColor = pDC->SetTextColor(color);pDC->TextOut(0, 0, _strCaption);pDC->SelectObject(pOldFont);pDC->SetBkMode(nBkMode);pDC->SetTextColor(nOldColor);}void CHyperlinkStatic::OnPaint() {if (!_fontCaption.m_hObject){CreateFont(&_fontCaption, m_nFontIncrement);_bCreateFont = TRUE;}CPaintDC dc(this);//CRect rctClient;//this->GetClientRect(&rctClient);////CDC memDC;//CBitmap memBmp;//memDC.CreateCompatibleDC(&dc);//memBmp.CreateCompatibleBitmap(&dc, rctClient.Width(), rctClient.Height());//CBitmap* pOldBmp = memDC.SelectObject(&memBmp);//CBrush bgBrush(0xffffff);//CRect rctDraw(0, 0, rctClient.Width(), rctClient.Height());//memDC.FillRect(rctDraw, &bgBrush);DrawText(&dc, m_crCurrent);//dc.BitBlt(0, 0, rctClient.Width(), rctClient.Height(),//&memDC, 0, 0, SRCCOPY);//memDC.SelectObject(pOldBmp);/*if ( _bCreateFont == false )CreateFont();CPaintDC dc(this);CFont *pOldFont = (CFont*)dc.SelectObject(&_fontCaption);dc.SetBkMode(TRANSPARENT);dc.SetTextColor(m_tagFontColor);dc.TextOut(0, 0, _strCaption);dc.SelectObject(pOldFont);*/}void CHyperlinkStatic::OnDestroy() {CStatic::OnDestroy();_fontCaption.DeleteObject();}void CHyperlinkStatic::PreSubclassWindow() {ModifyStyle(0, SS_NOTIFY, TRUE);GetWindowText(_strCaption);_bGetCaptionSize = FALSE;CStatic::PreSubclassWindow();}LRESULT CHyperlinkStatic::OnMouseLeave(WPARAM wParam, LPARAM lParam){_bMouseInControl = false;// 恢複游標if (!_strHyperlink.IsEmpty())::SetCursor(_hArrowCursor);// 恢複字型顏色if (m_crCurrent != m_crDefault){m_crCurrent = m_crDefault;Invalidate(TRUE);}return 0;}void CHyperlinkStatic::OnMouseMove(UINT nFlags, CPoint point) {if ( _bMouseInControl == false ) {//Track the mouse leave eventTRACKMOUSEEVENT tme;tme.cbSize = sizeof(tme); tme.hwndTrack = GetSafeHwnd(); tme.dwFlags = TME_LEAVE; _TrackMouseEvent(&tme);_bMouseInControl = true;}else {if (!_bGetCaptionSize)SetCaptionSize();// 顯示手形游標if (!_strHyperlink.IsEmpty())::SetCursor(_hHandCursor);//(InCaptionRange(point))?_hHandCursor:_hArrowCursor);// 改變字型顏色if (m_crCurrent != m_crOnMouse){m_crCurrent = m_crOnMouse;Invalidate(TRUE);}}CStatic::OnMouseMove(nFlags, point);}void CHyperlinkStatic::CreateFont(CFont *pFont, LONG lFontIncrement){CFont* pFontParent = GetParent()->GetFont();ASSERT(pFontParent);LOGFONT lf;pFontParent->GetObject(sizeof(lf), &lf);lf.lfUnderline = m_bUnderLine;lf.lfHeight += lFontIncrement;pFont->CreateFontIndirect(&lf);}//*********************************************************************// function : 擷取制定文本在某個字型下的尺寸//*********************************************************************CSize CHyperlinkStatic::GetTextSize(CFont *pFont, const CString &strText){ASSERT(pFont);CClientDC dc(this);CFont *pOldFont = dc.SelectObject(pFont);CSize size = dc.GetTextExtent(strText);dc.SelectObject(pOldFont);return size;}//*********************************************************************// function : 設定制定文本在某個字型下的尺寸//*********************************************************************void CHyperlinkStatic::SetCaptionSize(){if (!_bCreateFont){CreateFont(&_fontCaption, m_nFontIncrement);_bCreateFont = TRUE;}if (!_bGetCaptionSize){_sizeCaption = GetTextSize(&_fontCaption, _strCaption);_bGetCaptionSize = TRUE;}}//*********************************************************************// function : 擷取文本尺寸//*********************************************************************CSize CHyperlinkStatic::GetCaptionSize(){if (!_bGetCaptionSize)SetCaptionSize();return _sizeCaption;}bool CHyperlinkStatic::InCaptionRange(CPoint &point){if ( _bGetCaptionSize == false )return false;return (( point.x >= 0 )&&( point.x < _sizeCaption.cx ) &&( point.y >= 0 )&&( point.y < _sizeCaption.cy ));}int CHyperlinkStatic::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CStatic::OnCreate(lpCreateStruct) == -1)return -1;CreateFont(&_fontCaption, m_nFontIncrement);_bCreateFont = TRUE;return 0;}