Crypt Library Demo 1.00
|
00001 // HyperlinkStatic.cpp : implementation file 00002 // 00003 00004 #include "stdafx.h" 00005 #include "HyperlinkStatic.h" 00006 00007 #ifdef _DEBUG 00008 #define new DEBUG_NEW 00009 #undef THIS_FILE 00010 static char THIS_FILE[] = __FILE__; 00011 #endif 00012 00014 // CHyperlinkStatic 00015 00016 CHyperlinkStatic::CHyperlinkStatic() 00017 { 00018 _strCaption = _strHyperlink = _T(""); 00019 _bMouseInControl = _bCreateFont = _bGetCaptionSize = false; 00020 00021 _hHandCursor = ::LoadCursor(0, MAKEINTRESOURCE(IDC_HAND)); 00022 _hArrowCursor = ::LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW)); 00023 } 00024 00025 CHyperlinkStatic::~CHyperlinkStatic() 00026 { 00027 } 00028 00029 BEGIN_MESSAGE_MAP(CHyperlinkStatic, CStatic) 00030 //{{AFX_MSG_MAP(CHyperlinkStatic) 00031 ON_WM_LBUTTONDOWN() 00032 ON_WM_PAINT() 00033 ON_WM_DESTROY() 00034 ON_WM_MOUSEMOVE() 00035 //}}AFX_MSG_MAP 00036 ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave) 00037 END_MESSAGE_MAP() 00038 00040 // CHyperlinkStatic message handlers 00041 00042 void CHyperlinkStatic::SetHyperlink(CString strHyperlink) 00043 { 00044 _strHyperlink = strHyperlink; 00045 } 00046 00047 void CHyperlinkStatic::SetCaption(CString strCaption) 00048 { 00049 _strCaption = strCaption; 00050 _bGetCaptionSize = false; 00051 } 00052 00053 void CHyperlinkStatic::OnLButtonDown(UINT nFlags, CPoint point) 00054 { 00055 if ( _bGetCaptionSize == false ) 00056 GetCaptionSize(); 00057 if (InCaptionRange(point)) 00058 ShellExecute(0, _T("open"), _strHyperlink, 0, 0, SW_SHOWNORMAL); 00059 CStatic::OnLButtonDown(nFlags, point); 00060 } 00061 00062 void CHyperlinkStatic::OnPaint() 00063 { 00064 if ( _bCreateFont == false ) 00065 CreateFont(); 00066 CPaintDC dc(this); 00067 CFont *pOldFont = (CFont*)dc.SelectObject(&_fontCaption); 00068 dc.SetBkMode(TRANSPARENT); 00069 dc.SetTextColor(RGB(0,0,255)); 00070 dc.TextOut(0, 0, _strCaption); 00071 dc.SelectObject(pOldFont); 00072 } 00073 00074 void CHyperlinkStatic::OnDestroy() 00075 { 00076 CStatic::OnDestroy(); 00077 _fontCaption.DeleteObject(); 00078 } 00079 00080 void CHyperlinkStatic::PreSubclassWindow() 00081 { 00082 ModifyStyle(0, SS_NOTIFY, TRUE); 00083 GetWindowText(_strCaption); 00084 _bGetCaptionSize = false; 00085 CStatic::PreSubclassWindow(); 00086 } 00087 00088 LRESULT CHyperlinkStatic::OnMouseLeave(WPARAM wParam, LPARAM lParam) 00089 { 00090 UNREFERENCED_PARAMETER(wParam); 00091 UNREFERENCED_PARAMETER(lParam); 00092 _bMouseInControl = false; 00093 ::SetCursor(_hArrowCursor); 00094 return 0; 00095 } 00096 00097 void CHyperlinkStatic::OnMouseMove(UINT nFlags, CPoint point) 00098 { 00099 if ( _bMouseInControl == false ) { 00100 //Track the mouse leave event 00101 TRACKMOUSEEVENT tme; 00102 tme.cbSize = sizeof(tme); 00103 tme.hwndTrack = GetSafeHwnd(); 00104 tme.dwFlags = TME_LEAVE; 00105 _TrackMouseEvent(&tme); 00106 _bMouseInControl = true; 00107 } 00108 else { 00109 if ( _bGetCaptionSize == false ) 00110 GetCaptionSize(); 00111 ::SetCursor((InCaptionRange(point))?_hHandCursor:_hArrowCursor); 00112 } 00113 CStatic::OnMouseMove(nFlags, point); 00114 } 00115 00116 void CHyperlinkStatic::CreateFont() 00117 { 00118 CFont* pFontParent = GetParent()->GetFont(); 00119 if ( pFontParent ) { 00120 LOGFONT lf; 00121 pFontParent->GetObject(sizeof(lf), &lf); 00122 lf.lfUnderline = TRUE; 00123 _fontCaption.CreateFontIndirect(&lf); 00124 _bCreateFont = true; 00125 } 00126 } 00127 00128 void CHyperlinkStatic::GetCaptionSize() 00129 { 00130 if (( _bGetCaptionSize == false ) && ( _bCreateFont )) { 00131 CClientDC dc(this); 00132 CFont *pOldFont = dc.SelectObject(&_fontCaption); 00133 _sizeCaption = dc.GetTextExtent(_strCaption); 00134 dc.SelectObject(pOldFont); 00135 _bGetCaptionSize = true; 00136 } 00137 } 00138 00139 bool CHyperlinkStatic::InCaptionRange(CPoint &point) 00140 { 00141 if ( _bGetCaptionSize == false ) 00142 return false; 00143 return (( point.x >= 0 )&&( point.x < _sizeCaption.cx ) && 00144 ( point.y >= 0 )&&( point.y < _sizeCaption.cy )); 00145 }