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.

199 lines
5.4 KiB

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