URLLinkButton.cpp
上传用户:yxb0227
上传日期:2022-06-16
资源大小:19k
文件大小:9k
源码类别:

按钮控件

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // File: URLLinkButton.cpp
  3. // By: Nguyen Duc Thanh, Vegasoft, JSC.
  4. // Copyright 2004, Nguyen Duc Thanh.
  5. // Email: thanhnd@vegasoft.ws
  6. // Website: http:\www.vegasoft.ws
  7. // Date: July 15, 2004
  8. /////////////////////////////////////////////////////////////////////////////
  9. //
  10. // The CURLLinkButton class extends the functionality of CButton 
  11. // by providing support for URL links
  12. // It displays the URL link and invokes the shell when clicked.
  13. // Tooltip supported, full color customization
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. // This code is free for non-commercial use, providing this notice remains 
  18. // intact in the source files and all eventual changes are clearly marked 
  19. // with comments. Do not misrepresent the origin of this code.
  20. //
  21. // No warrantee of any kind, express or implied, is included with this
  22. // software; use at your own risk
  23. // 
  24. // If you find this code useful, credits would be nice!
  25. //
  26. /////////////////////////////////////////////////////////////////////////////
  27. #include "stdafx.h"
  28. #include "URLLinkButton.h"
  29. #ifdef _DEBUG
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34. HCURSOR  CURLLinkButton::m_hCursorHand = NULL;
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CURLLinkButton
  37. CURLLinkButton::CURLLinkButton()
  38. {
  39. m_bHover = FALSE;
  40. m_bTracking = FALSE;
  41. m_bVisited = FALSE;
  42. m_bKeyDown = FALSE;
  43. m_clrVisited = RGB( 0x80, 0x00, 0x80 );
  44. m_clrRegular = ::GetSysColor(COLOR_HIGHLIGHT);
  45. m_clrDisabled = ::GetSysColor(COLOR_GRAYTEXT);
  46. #if(WINVER >= 0x0500)
  47. m_clrHover = ::GetSysColor (COLOR_HOTLIGHT);
  48. #else
  49. m_clrHover = RGB (0x0, 0x0, 0xFF);
  50. #endif 
  51. m_pVisitedFont = NULL;
  52. // Initilize static member once
  53. if (m_hCursorHand==NULL)
  54. {
  55. #if(WINVER >= 0x0500)
  56. //Load system hand cursor
  57. m_hCursorHand = AfxGetApp()->LoadCursor (IDC_HAND);
  58. #else
  59. // Use a custom Hand cursor
  60. // Must add a cursor resourse in the project with ID: IDC_CURSOR_HAND
  61. //m_hCursorHand = AfxGetApp()->LoadCursor (IDC_CURSOR_HAND);
  62. // If you haven't the cursor resourse in your project
  63. // load it from WinHlp32 module 
  64. //**********************************
  65. TCHAR szWindowsDir[MAX_PATH];
  66. GetWindowsDirectory(szWindowsDir ,MAX_PATH);
  67. strcat(szWindowsDir,"\Winhlp32.exe");
  68. HMODULE hModule = LoadLibrary(szWindowsDir);
  69. if (hModule)
  70. m_hCursorHand = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
  71. //**********************************
  72. #endif 
  73. }
  74. }
  75. CURLLinkButton::~CURLLinkButton()
  76. {
  77. if (m_pVisitedFont->GetSafeHandle())
  78. {
  79. m_pVisitedFont->DeleteObject();
  80. delete m_pVisitedFont;
  81. }
  82. }
  83. BEGIN_MESSAGE_MAP(CURLLinkButton, CButton)
  84. //{{AFX_MSG_MAP(CURLLinkButton)
  85. ON_WM_MOUSEMOVE()
  86. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  87. ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
  88. ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
  89. ON_WM_SETCURSOR()
  90. ON_WM_KILLFOCUS()
  91. //}}AFX_MSG_MAP
  92. END_MESSAGE_MAP()
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CURLLinkButton message handlers
  95. void CURLLinkButton::OnMouseMove(UINT nFlags, CPoint point) 
  96. {
  97. if (!m_bTracking)
  98. {
  99. TRACKMOUSEEVENT tme;
  100. tme.cbSize = sizeof(tme);
  101. tme.hwndTrack = m_hWnd;
  102. tme.dwFlags = TME_LEAVE|TME_HOVER;
  103. tme.dwHoverTime = 1;
  104. m_bTracking = _TrackMouseEvent(&tme);
  105. }
  106. CButton::OnMouseMove(nFlags, point);
  107. }
  108. void CURLLinkButton::OnMouseHover(WPARAM wparam, LPARAM lparam) 
  109. {
  110. m_bHover=TRUE;
  111. Invalidate();
  112. if (m_ToolTip.m_hWnd)
  113. if (m_ToolTip.GetToolCount() != 0) 
  114. m_ToolTip.Activate(TRUE);
  115. }
  116. LRESULT CURLLinkButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
  117. {
  118. m_bTracking = FALSE;
  119. m_bHover=FALSE;
  120. Invalidate();
  121. if (m_ToolTip.m_hWnd)
  122. if (m_ToolTip.GetToolCount() != 0) 
  123. m_ToolTip.Activate(FALSE);
  124. return 0;
  125. }
  126. void CURLLinkButton::OnClicked() 
  127. {
  128. if (!IsWindowEnabled ())
  129. {
  130. return;
  131. }
  132. CWaitCursor wait;
  133. CString strURL = m_sURL;
  134. if (strURL.IsEmpty ())
  135. {
  136. GetWindowText (strURL);
  137. }
  138. if (::ShellExecute (NULL, _T("open"), m_sPrefix + strURL, NULL, NULL, NULL) < (HINSTANCE) 32)
  139. {
  140. //Can't open URL, send WM_LINK_CLICKED message to parent window
  141. CWnd *pParent = GetParent();
  142. if( pParent )
  143. {
  144. UINT uiCtrlID = GetDlgCtrlID();
  145. pParent -> SendMessage(WM_LINK_CLICKED, (WPARAM)uiCtrlID,(LPARAM)NULL);
  146. }
  147. }
  148. m_bVisited = TRUE;
  149. m_bHover = FALSE;
  150. Invalidate ();
  151. }
  152. BOOL CURLLinkButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  153. {
  154. if( nHitTest == HTCLIENT )
  155. {
  156. if( m_hCursorHand )
  157. {
  158. ::SetCursor( m_hCursorHand );
  159. return( TRUE );
  160. }
  161. }
  162. return CButton::OnSetCursor(pWnd, nHitTest, message);
  163. }
  164. void CURLLinkButton::OnKillFocus(CWnd* pNewWnd) 
  165. {
  166. CButton::OnKillFocus(pNewWnd);
  167. Invalidate();
  168. }
  169. void CURLLinkButton::SetURL (LPCTSTR lpszURL)
  170. {
  171. if (lpszURL == NULL)
  172. {
  173. m_sURL.Empty ();
  174. }
  175. else
  176. {
  177. m_sURL = lpszURL;
  178. }
  179. }
  180. void CURLLinkButton::SetURLPrefix (LPCTSTR lpszPrefix)
  181. {
  182. ASSERT (lpszPrefix != NULL);
  183. m_sPrefix = lpszPrefix;
  184. }
  185. void CURLLinkButton::SetDisplayText (LPCTSTR lpszDisplayText)
  186. {
  187. ASSERT (lpszDisplayText != NULL);
  188. SetWindowText(lpszDisplayText);
  189. SizeToContent();
  190. Invalidate();
  191. }
  192. BOOL CURLLinkButton::PreTranslateMessage(MSG* pMsg) 
  193. {
  194. switch (pMsg->message)
  195. {
  196. case WM_KEYDOWN:
  197. if (pMsg->wParam == VK_SPACE || pMsg->wParam == VK_RETURN)
  198. {
  199. m_bKeyDown = TRUE;
  200. return TRUE;
  201. }
  202. break;
  203. case WM_KEYUP:
  204. if (pMsg->wParam == VK_SPACE)
  205. {
  206. return TRUE;
  207. }
  208. else if (pMsg->wParam == VK_RETURN)
  209. {
  210. if (m_bKeyDown)
  211. {
  212. m_bKeyDown = FALSE;
  213. OnClicked ();
  214. return TRUE;
  215. }
  216. }
  217. break;
  218. }
  219. if (m_ToolTip.m_hWnd)
  220. m_ToolTip.RelayEvent(pMsg);
  221. m_bKeyDown = FALSE;
  222. return CButton::PreTranslateMessage(pMsg);
  223. }
  224. void CURLLinkButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  225. {
  226. CDC* pDC = CDC::FromHandle (lpDrawItemStruct->hDC);
  227. CRect rect = lpDrawItemStruct->rcItem;
  228. if( GetFocus() == this ) 
  229. {
  230. //Draw focus rectangle
  231. pDC->DrawFocusRect(rect);
  232. }
  233. //Create UL font
  234. if (!m_pVisitedFont->GetSafeHandle())
  235. {
  236. LOGFONT lfFont;
  237. GetFont() -> GetLogFont( &lfFont ); // Normal font
  238. //////////////////////////////////////////////////////////////////////////
  239. //Customize the visited font here
  240. lfFont.lfUnderline = TRUE; // Set UnderLine flag
  241. //lfFont.lfItalic = TRUE; // Set Italic flag
  242. m_pVisitedFont = new CFont;
  243. m_pVisitedFont->CreateFontIndirect( &lfFont ); // Create UL font
  244. }
  245. CFont* pOldFont;
  246. // Set text parameters:
  247. if (!IsWindowEnabled ())
  248. pDC->SetTextColor(m_clrDisabled);
  249. else
  250. {
  251. // Set visited font if hover
  252. if (m_bHover)
  253. pOldFont = pDC->SelectObject (m_pVisitedFont);
  254. pDC->SetTextColor(m_bHover ? m_clrHover : 
  255.  (m_bVisited?m_clrVisited:m_clrRegular));
  256. }
  257. pDC->SetBkMode (TRANSPARENT);
  258. // Obtain label:
  259. CString strLabel;
  260. GetWindowText (strLabel);
  261. CRect rectText = rect;
  262. pDC->DrawText (strLabel, rectText, DT_SINGLELINE);
  263.  
  264. if (m_bHover)
  265. pDC->SelectObject (pOldFont);
  266. }
  267. void CURLLinkButton::SizeToContent()
  268. {
  269. CClientDC dc (this);
  270. // Obtain label:
  271. CString strLabel;
  272. GetWindowText (strLabel);
  273. //Get the displayed text size
  274. CSize sizeText = dc.GetTextExtent(strLabel);
  275. //Resize the control
  276. SetWindowPos (NULL, -1, -1, sizeText.cx, sizeText.cy,
  277.      SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  278. }
  279. CString CURLLinkButton::GetDefaultTipText()
  280. {
  281. CString sTip = m_sURL;
  282. if (sTip.IsEmpty())
  283. GetWindowText (sTip);
  284. return m_sPrefix + sTip;
  285. }
  286. void CURLLinkButton::SetToolTipText(CString sTip)
  287. {
  288. //Use default tooltip if sTip is empty
  289. if (sTip.IsEmpty())
  290. sTip = GetDefaultTipText();
  291. if (m_ToolTip.m_hWnd == NULL)
  292. {
  293. m_ToolTip.Create(this);
  294. m_ToolTip.Activate(FALSE);
  295. }
  296. // If there is no tooltip defined then add it
  297. if (m_ToolTip.GetToolCount() == 0)
  298. {
  299. CRect rectBtn; 
  300. GetClientRect(rectBtn);
  301. m_ToolTip.AddTool(this, sTip, rectBtn, 1);
  302. }
  303. // Set text for tooltip
  304. m_ToolTip.UpdateTipText(sTip, this, 1);
  305. }
  306. void CURLLinkButton::SetToolTipTextColor(COLORREF clrTextColor)
  307. {
  308. m_ToolTip.SetTipTextColor(clrTextColor);
  309. }
  310. void CURLLinkButton::SetToolTipBkColor(COLORREF clrBkColor)
  311. {
  312. m_ToolTip.SetTipBkColor(clrBkColor);
  313. }
  314. void CURLLinkButton::SetToolTipColor(COLORREF clrTextColor, COLORREF clrBkColor)
  315. {
  316. SetToolTipTextColor(clrTextColor);
  317. SetToolTipBkColor(clrBkColor);
  318. }
  319. void CURLLinkButton::SetRegularColor(COLORREF clrRegular)
  320. {
  321. m_clrRegular = clrRegular;
  322. }
  323. void CURLLinkButton::SetHoverColor(COLORREF clrHover)
  324. {
  325. m_clrHover = clrHover;
  326. }
  327. void CURLLinkButton::SetVisitedColor(COLORREF clrVisited)
  328. {
  329. m_clrVisited = clrVisited;
  330. }
  331. void CURLLinkButton::SetLinkColor(COLORREF clrRegular, COLORREF clrHover, COLORREF clrVisited)
  332. {
  333. SetRegularColor(clrRegular);
  334. SetHoverColor(clrHover);
  335. SetVisitedColor(clrVisited);
  336. }
  337. void CURLLinkButton::PreSubclassWindow() 
  338. {
  339. ModifyStyle (BS_DEFPUSHBUTTON, BS_OWNERDRAW);
  340. CButton::PreSubclassWindow();
  341. }