TITLETIP.CPP
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:6k
源码类别:

Internet/IE编程

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////////////////
  2. // TitleTip.cpp : implementation file
  3. //
  4. // Code taken from www.codeguru.com. - thanks Zafir!
  5. #include "stdafx.h"
  6. #include "TitleTip.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CTitleTip
  14. CTitleTip::CTitleTip()
  15. {
  16. // Register the window class if it has not already been registered.
  17. WNDCLASS wndcls;
  18. HINSTANCE hInst = AfxGetInstanceHandle();
  19. if(!(::GetClassInfo(hInst, TITLETIP_CLASSNAME, &wndcls)))
  20. {
  21. // otherwise we need to register a new class
  22. wndcls.style = CS_SAVEBITS ;
  23. wndcls.lpfnWndProc = ::DefWindowProc;
  24. wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
  25. wndcls.hInstance = hInst;
  26. wndcls.hIcon = NULL;
  27. wndcls.hCursor = LoadCursor( hInst, IDC_ARROW );
  28. wndcls.hbrBackground = (HBRUSH)(COLOR_INFOBK + 1); 
  29. wndcls.lpszMenuName = NULL;
  30. wndcls.lpszClassName = TITLETIP_CLASSNAME;
  31. if (!AfxRegisterClass(&wndcls))
  32. AfxThrowResourceException();
  33. }
  34. }
  35. CTitleTip::~CTitleTip()
  36. {
  37. }
  38. BEGIN_MESSAGE_MAP(CTitleTip, CWnd)
  39. //{{AFX_MSG_MAP(CTitleTip)
  40. ON_WM_MOUSEMOVE()
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CTitleTip message handlers
  45. BOOL CTitleTip::Create(CWnd * pParentWnd)
  46. {
  47. ASSERT_VALID(pParentWnd);
  48. DWORD dwStyle = WS_BORDER | WS_POPUP; 
  49. DWORD dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
  50. m_pParentWnd = pParentWnd;
  51. return CreateEx( dwExStyle, TITLETIP_CLASSNAME, NULL, dwStyle, 0, 0, 0, 0, 
  52. NULL, NULL, NULL );
  53. }
  54. // Show   - Show the titletip if needed
  55. // rectTitle  - The rectangle within which the original 
  56. //     title is constrained - in client coordinates
  57. // lpszTitleText - The text to be displayed
  58. // xoffset  - Number of pixel that the text is offset from
  59. //    left border of the cell
  60. void CTitleTip::Show(CRect rectTitle, LPCTSTR lpszTitleText, int xoffset /*=0*/)
  61. {
  62. ASSERT( ::IsWindow( GetSafeHwnd() ) );
  63. ASSERT( !rectTitle.IsRectEmpty() );
  64. // If titletip is already displayed, don't do anything.
  65. if( IsWindowVisible() ) 
  66. return;
  67. // Do not display the titletip is app does not have focus
  68. if( GetFocus() == NULL )
  69. return;
  70. // Define the rectangle outside which the titletip will be hidden.
  71. // We add a buffer of one pixel around the rectangle
  72. m_rectTitle.top    = -1;
  73. m_rectTitle.left   = -xoffset-1;
  74. m_rectTitle.right  = rectTitle.Width()-xoffset;
  75. m_rectTitle.bottom = rectTitle.Height()+1;
  76. // Determine the width of the text
  77. m_pParentWnd->ClientToScreen( rectTitle );
  78. CClientDC dc(this);
  79. CString strTitle = _T(" "); strTitle += lpszTitleText; strTitle += _T(" ");
  80. CFont *pFont = m_pParentWnd->GetFont();  // use same font as ctrl
  81. CFont *pFontDC = dc.SelectObject( pFont );
  82. CSize size = dc.GetTextExtent( strTitle );
  83. CRect rectDisplay = rectTitle;
  84. rectDisplay.left += xoffset;
  85. rectDisplay.right = rectDisplay.left + size.cx + xoffset;
  86. // Do not display if the text fits within available space
  87. if( rectDisplay.right <= rectTitle.right-xoffset )
  88. return;
  89. // Show the titletip
  90. SetWindowPos( &wndTop, rectDisplay.left, rectDisplay.top, 
  91. rectDisplay.Width(), rectDisplay.Height(), 
  92. SWP_SHOWWINDOW|SWP_NOACTIVATE );
  93. dc.SetBkMode( TRANSPARENT );
  94. dc.TextOut( 0, 0, strTitle );
  95. dc.SelectObject( pFontDC );
  96. SetCapture();
  97. }
  98. void CTitleTip::Hide()
  99. {
  100.    if (!::IsWindow(GetSafeHwnd()))
  101.         return;
  102.     if (GetCapture()->GetSafeHwnd() == GetSafeHwnd())
  103.         ReleaseCapture();
  104. ShowWindow( SW_HIDE );
  105. }
  106. void CTitleTip::OnMouseMove(UINT nFlags, CPoint point) 
  107. {
  108.     if (!m_rectTitle.PtInRect(point)) 
  109.     {
  110.         Hide();
  111.         
  112.         // Forward the message
  113.         ClientToScreen( &point );
  114.         CWnd *pWnd = WindowFromPoint( point );
  115.         if ( pWnd == this ) 
  116.             pWnd = m_pParentWnd;
  117.         
  118.         int hittest = (int)pWnd->SendMessage(WM_NCHITTEST,0,MAKELONG(point.x,point.y));
  119.         
  120.         if (hittest == HTCLIENT) {
  121.             pWnd->ScreenToClient( &point );
  122.             pWnd->PostMessage( WM_MOUSEMOVE, nFlags, MAKELONG(point.x,point.y) );
  123.         } else {
  124.             pWnd->PostMessage( WM_NCMOUSEMOVE, hittest, MAKELONG(point.x,point.y) );
  125.         }
  126.     }
  127. }
  128. BOOL CTitleTip::PreTranslateMessage(MSG* pMsg) 
  129. {
  130. CWnd *pWnd;
  131. int hittest;
  132. switch (pMsg->message)
  133. {
  134. case WM_LBUTTONDOWN:
  135. case WM_RBUTTONDOWN:
  136. case WM_MBUTTONDOWN:
  137. POINTS pts = MAKEPOINTS( pMsg->lParam );
  138. POINT  point;
  139. point.x = pts.x;
  140. point.y = pts.y;
  141. ClientToScreen( &point );
  142. pWnd = WindowFromPoint( point );
  143. if( pWnd == this ) 
  144. pWnd = m_pParentWnd;
  145. hittest = (int)pWnd->SendMessage(WM_NCHITTEST,0,MAKELONG(point.x,point.y));
  146. if (hittest == HTCLIENT) {
  147. pWnd->ScreenToClient( &point );
  148. pMsg->lParam = MAKELONG(point.x,point.y);
  149. } else {
  150. switch (pMsg->message) {
  151. case WM_LBUTTONDOWN: 
  152. pMsg->message = WM_NCLBUTTONDOWN;
  153. break;
  154. case WM_RBUTTONDOWN: 
  155. pMsg->message = WM_NCRBUTTONDOWN;
  156. break;
  157. case WM_MBUTTONDOWN: 
  158. pMsg->message = WM_NCMBUTTONDOWN;
  159. break;
  160. }
  161. pMsg->wParam = hittest;
  162. pMsg->lParam = MAKELONG(point.x,point.y);
  163. }
  164.         Hide();
  165. pWnd->PostMessage(pMsg->message,pMsg->wParam,pMsg->lParam);
  166. return TRUE;
  167. case WM_KEYDOWN:
  168. case WM_SYSKEYDOWN:
  169.         Hide();
  170. m_pParentWnd->PostMessage( pMsg->message, pMsg->wParam, pMsg->lParam );
  171. return TRUE;
  172. }
  173. if( GetFocus() == NULL )
  174. {
  175.         Hide();
  176. return TRUE;
  177. }
  178. return CWnd::PreTranslateMessage(pMsg);
  179. }