Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.3 KiB

  1. // HotLink.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "HotLink.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define COLOR_BLUE RGB(0, 0, 0xFF)
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CHotLink
  14. CHotLink::CHotLink():
  15. m_CapturedMouse( FALSE ),
  16. m_fBrowse( FALSE ),
  17. m_fExplore( FALSE ),
  18. m_fOpen( FALSE ),
  19. m_fInitializedFont( FALSE )
  20. {
  21. }
  22. CHotLink::~CHotLink()
  23. {
  24. }
  25. BEGIN_MESSAGE_MAP(CHotLink, CButton)
  26. //{{AFX_MSG_MAP(CHotLink)
  27. ON_WM_LBUTTONDOWN()
  28. ON_WM_LBUTTONUP()
  29. ON_WM_MOUSEMOVE()
  30. //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. //------------------------------------------------------------------------
  33. // set the title string
  34. void CHotLink::SetTitle( CString sz )
  35. {
  36. // set the title
  37. SetWindowText( sz );
  38. // force the window to redraw
  39. Invalidate( TRUE );
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CHotLink message handlers
  43. //------------------------------------------------------------------------
  44. void CHotLink::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
  45. {
  46. // prep the device context
  47. CDC* pdc = CDC::FromHandle(lpDrawItemStruct->hDC);
  48. // get the drawing rect
  49. CRect rect = lpDrawItemStruct->rcItem;
  50. if ( ! m_fInitializedFont )
  51. {
  52. // get the window font
  53. CFont* pfont = GetFont();
  54. LOGFONT logfont;
  55. pfont->GetLogFont( &logfont );
  56. // modify the font - add underlining
  57. logfont.lfUnderline = TRUE;
  58. // set the font back
  59. pfont->CreateFontIndirect( &logfont );
  60. SetFont( pfont, TRUE );
  61. m_fInitializedFont = TRUE;
  62. }
  63. // draw the text in blue
  64. pdc->SetTextColor( COLOR_BLUE );
  65. // draw the text
  66. CString sz;
  67. GetWindowText( sz );
  68. pdc->DrawText( sz, &rect, DT_LEFT|DT_SINGLELINE|DT_VCENTER );
  69. // get the extents fo the text for later reference
  70. m_cpTextExtents = pdc->GetOutputTextExtent( sz );
  71. }
  72. //------------------------------------------------------------------------
  73. // calculate the rectangle that surrounds the text
  74. void CHotLink::GetTextRect( CRect &rect )
  75. {
  76. // get the main rect
  77. GetClientRect( rect );
  78. // reduce it by the width of the text
  79. rect.right = rect.left + m_cpTextExtents.cx;
  80. }
  81. //------------------------------------------------------------------------
  82. void CHotLink::OnLButtonDown(UINT nFlags, CPoint point)
  83. {
  84. // don't do the hotlink thing if there is no text
  85. CString sz;
  86. GetWindowText( sz );
  87. if ( sz.IsEmpty() )
  88. return;
  89. CRect rect;
  90. GetTextRect( rect );
  91. if ( !m_CapturedMouse && rect.PtInRect(point) )
  92. {
  93. SetCapture( );
  94. m_CapturedMouse = TRUE;
  95. }
  96. }
  97. //------------------------------------------------------------------------
  98. void CHotLink::OnLButtonUp(UINT nFlags, CPoint point)
  99. {
  100. // only bother if we have the capture
  101. if ( m_CapturedMouse )
  102. {
  103. ReleaseCapture();
  104. if ( m_fBrowse )
  105. Browse();
  106. if ( m_fExplore )
  107. Explore();
  108. if ( m_fOpen )
  109. Open();
  110. }
  111. }
  112. //------------------------------------------------------------------------
  113. void CHotLink::Browse()
  114. {
  115. // get the window text
  116. CString sz;
  117. GetWindowText( sz );
  118. // and do it to it!
  119. ShellExecute(
  120. NULL, // handle to parent window
  121. NULL, // pointer to string that specifies operation to perform
  122. sz, // pointer to filename or folder name string
  123. NULL, // pointer to string that specifies executable-file parameters
  124. NULL, // pointer to string that specifies default directory
  125. SW_SHOW // whether file is shown when opened
  126. );
  127. }
  128. //------------------------------------------------------------------------
  129. void CHotLink::Explore()
  130. {
  131. // get the window text
  132. CString sz;
  133. GetWindowText( sz );
  134. // and do it to it!
  135. ShellExecute(
  136. NULL, // handle to parent window
  137. "explore", // pointer to string that specifies operation to perform
  138. sz, // pointer to filename or folder name string
  139. NULL, // pointer to string that specifies executable-file parameters
  140. NULL, // pointer to string that specifies default directory
  141. SW_SHOW // whether file is shown when opened
  142. );
  143. }
  144. //------------------------------------------------------------------------
  145. void CHotLink::Open()
  146. {
  147. // get the window text
  148. CString sz;
  149. GetWindowText( sz );
  150. // and do it to it!
  151. ShellExecute(
  152. NULL, // handle to parent window
  153. "open", // pointer to string that specifies operation to perform
  154. sz, // pointer to filename or folder name string
  155. NULL, // pointer to string that specifies executable-file parameters
  156. NULL, // pointer to string that specifies default directory
  157. SW_SHOW // whether file is shown when opened
  158. );
  159. }
  160. //------------------------------------------------------------------------
  161. void CHotLink::OnMouseMove(UINT nFlags, CPoint point)
  162. {
  163. CRect rect;
  164. GetTextRect( rect );
  165. // if the mouse is over the hot area, show the right cursor
  166. if ( rect.PtInRect(point) )
  167. ::SetCursor(AfxGetApp()->LoadCursor( IDC_BROWSE ));
  168. // CButton::OnMouseMove(nFlags, point);
  169. }