|
| 1 | +// HyperlinkStatic.cpp : implementation file |
| 2 | +// |
| 3 | + |
| 4 | +#include "stdafx.h" |
| 5 | +#include "HyperlinkStatic.h" |
| 6 | + |
| 7 | +#ifdef _DEBUG |
| 8 | +#define new DEBUG_NEW |
| 9 | +#undef THIS_FILE |
| 10 | +static char THIS_FILE[] = __FILE__; |
| 11 | +#endif |
| 12 | + |
| 13 | +///////////////////////////////////////////////////////////////////////////// |
| 14 | +// CHyperlinkStatic |
| 15 | + |
| 16 | +CHyperlinkStatic::CHyperlinkStatic() |
| 17 | +{ |
| 18 | + _strCaption = _strHyperlink = _T(""); |
| 19 | + _bMouseInControl = _bCreateFont = _bGetCaptionSize = false; |
| 20 | + |
| 21 | + _hHandCursor = ::LoadCursor(nullptr, IDC_HAND); |
| 22 | + _hArrowCursor = ::LoadCursor(nullptr, IDC_ARROW); |
| 23 | +} |
| 24 | + |
| 25 | +CHyperlinkStatic::~CHyperlinkStatic() |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +BEGIN_MESSAGE_MAP(CHyperlinkStatic, CStatic) |
| 30 | + //{{AFX_MSG_MAP(CHyperlinkStatic) |
| 31 | + ON_WM_LBUTTONDOWN() |
| 32 | + ON_WM_PAINT() |
| 33 | + ON_WM_DESTROY() |
| 34 | + ON_WM_MOUSEMOVE() |
| 35 | + //}}AFX_MSG_MAP |
| 36 | + ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave) |
| 37 | +END_MESSAGE_MAP() |
| 38 | + |
| 39 | +///////////////////////////////////////////////////////////////////////////// |
| 40 | +// CHyperlinkStatic message handlers |
| 41 | + |
| 42 | +void CHyperlinkStatic::SetHyperlink(CString strHyperlink) |
| 43 | +{ |
| 44 | + _strHyperlink = strHyperlink; |
| 45 | +} |
| 46 | + |
| 47 | +void CHyperlinkStatic::SetCaption(CString strCaption) |
| 48 | +{ |
| 49 | + _strCaption = strCaption; |
| 50 | + _bGetCaptionSize = false; |
| 51 | +} |
| 52 | + |
| 53 | +void CHyperlinkStatic::OnLButtonDown(UINT nFlags, CPoint point) |
| 54 | +{ |
| 55 | + if ( _bGetCaptionSize == false ) |
| 56 | + GetCaptionSize(); |
| 57 | + if (InCaptionRange(point)) |
| 58 | + ShellExecute(0, _T("open"), _strHyperlink, 0, 0, SW_SHOWNORMAL); |
| 59 | + CStatic::OnLButtonDown(nFlags, point); |
| 60 | +} |
| 61 | + |
| 62 | +void CHyperlinkStatic::OnPaint() |
| 63 | +{ |
| 64 | + if ( _bCreateFont == false ) |
| 65 | + CreateFont(); |
| 66 | + CPaintDC dc(this); |
| 67 | + CFont *pOldFont = (CFont*)dc.SelectObject(&_fontCaption); |
| 68 | + dc.SetBkMode(TRANSPARENT); |
| 69 | + dc.SetTextColor(RGB(0,0,255)); |
| 70 | + dc.TextOut(0, 0, _strCaption); |
| 71 | + dc.SelectObject(pOldFont); |
| 72 | +} |
| 73 | + |
| 74 | +void CHyperlinkStatic::OnDestroy() |
| 75 | +{ |
| 76 | + CStatic::OnDestroy(); |
| 77 | + _fontCaption.DeleteObject(); |
| 78 | +} |
| 79 | + |
| 80 | +void CHyperlinkStatic::PreSubclassWindow() |
| 81 | +{ |
| 82 | + ModifyStyle(0, SS_NOTIFY, TRUE); |
| 83 | + GetWindowText(_strCaption); |
| 84 | + _bGetCaptionSize = false; |
| 85 | + CStatic::PreSubclassWindow(); |
| 86 | +} |
| 87 | + |
| 88 | +LRESULT CHyperlinkStatic::OnMouseLeave(WPARAM wParam, LPARAM lParam) |
| 89 | +{ |
| 90 | + UNREFERENCED_PARAMETER(wParam); |
| 91 | + UNREFERENCED_PARAMETER(lParam); |
| 92 | + _bMouseInControl = false; |
| 93 | + ::SetCursor(_hArrowCursor); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +void CHyperlinkStatic::OnMouseMove(UINT nFlags, CPoint point) |
| 98 | +{ |
| 99 | + if ( _bMouseInControl == false ) { |
| 100 | + //Track the mouse leave event |
| 101 | + TRACKMOUSEEVENT tme; |
| 102 | + tme.cbSize = sizeof(tme); |
| 103 | + tme.hwndTrack = GetSafeHwnd(); |
| 104 | + tme.dwFlags = TME_LEAVE; |
| 105 | + _TrackMouseEvent(&tme); |
| 106 | + _bMouseInControl = true; |
| 107 | + } |
| 108 | + else { |
| 109 | + if ( _bGetCaptionSize == false ) |
| 110 | + GetCaptionSize(); |
| 111 | + ::SetCursor((InCaptionRange(point))?_hHandCursor:_hArrowCursor); |
| 112 | + } |
| 113 | + CStatic::OnMouseMove(nFlags, point); |
| 114 | +} |
| 115 | + |
| 116 | +void CHyperlinkStatic::CreateFont() |
| 117 | +{ |
| 118 | + CFont* pFontParent = GetParent()->GetFont(); |
| 119 | + if ( pFontParent ) { |
| 120 | + LOGFONT lf; |
| 121 | + pFontParent->GetObject(sizeof(lf), &lf); |
| 122 | + lf.lfUnderline = TRUE; |
| 123 | + _fontCaption.CreateFontIndirect(&lf); |
| 124 | + _bCreateFont = true; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void CHyperlinkStatic::GetCaptionSize() |
| 129 | +{ |
| 130 | + if (( _bGetCaptionSize == false ) && ( _bCreateFont )) { |
| 131 | + CClientDC dc(this); |
| 132 | + CFont *pOldFont = dc.SelectObject(&_fontCaption); |
| 133 | + _sizeCaption = dc.GetTextExtent(_strCaption); |
| 134 | + dc.SelectObject(pOldFont); |
| 135 | + _bGetCaptionSize = true; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +bool CHyperlinkStatic::InCaptionRange(CPoint &point) |
| 140 | +{ |
| 141 | + if ( _bGetCaptionSize == false ) |
| 142 | + return false; |
| 143 | + return (( point.x >= 0 )&&( point.x < _sizeCaption.cx ) && |
| 144 | + ( point.y >= 0 )&&( point.y < _sizeCaption.cy )); |
| 145 | +} |
0 commit comments