Popup.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //feature x 2
  2. #include "stdafx.h"
  3. #include "Popup.h"
  4. #include "Misc.h"
  5. //feature x
  6. //test
  7. void InitToolInfo( TOOLINFO& ti )
  8. {
  9. //main branch change
  10. // INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
  11. ti.cbSize = TTTOOLINFO_V1_SIZE;
  12. ti.uFlags = TTF_ABSOLUTE | TTF_TRACK;
  13. ti.hwnd = NULL;
  14. ti.hinst = NULL;
  15. ti.uId = 0; // CPopup only uses uid 0
  16. ti.lpszText = NULL;
  17. // ToolTip control will cover the whole window
  18. ti.rect.left = 0;
  19. ti.rect.top = 0;
  20. ti.rect.right = 0;
  21. ti.rect.bottom = 0;
  22. }
  23. /*------------------------------------------------------------------*\
  24. CPopup - a tooltip that pops up manually (when Show is called).
  25. - technique learned from codeproject "ToolTipZen" by "Zarembo Maxim"
  26. \*------------------------------------------------------------------*/
  27. CPopup::CPopup()
  28. {
  29. Init();
  30. }
  31. // HWND_TOP
  32. CPopup::CPopup( int x, int y, HWND hWndPosRelativeTo, HWND hWndInsertAfter )
  33. {
  34. Init();
  35. m_hWndPosRelativeTo = hWndPosRelativeTo;
  36. m_hWndInsertAfter = hWndInsertAfter;
  37. SetPos( CPoint(x,y) );
  38. }
  39. CPopup::~CPopup()
  40. {
  41. Hide();
  42. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  43. ::DestroyWindow( m_hTTWnd );
  44. }
  45. void CPopup::Init()
  46. {
  47. // initialize variables
  48. m_bOwnTT = false;
  49. m_hTTWnd = NULL;
  50. m_bIsShowing = false;
  51. m_bAllowShow = true; // used by AllowShow()
  52. m_Pos.x = m_Pos.y = 0;
  53. m_bTop = true;
  54. m_bLeft = true;
  55. m_bCenterX = false;
  56. m_bCenterY = false;
  57. m_hWndPosRelativeTo = NULL;
  58. RECT rcScreen = DefaultMonitorRect();
  59. m_ScreenMaxX = rcScreen.right;
  60. m_ScreenMaxY = rcScreen.bottom;
  61. m_hWndInsertAfter = HWND_TOP; //HWND_TOPMOST
  62. SetTTWnd();
  63. }
  64. void CPopup::SetTTWnd( HWND hTTWnd, TOOLINFO* pTI )
  65. {
  66. if( pTI )
  67. m_TI = *pTI;
  68. else
  69. InitToolInfo( m_TI );
  70. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  71. {
  72. if( !::IsWindow(hTTWnd) )
  73. return; // we would have to recreate the one that already exists
  74. ::DestroyWindow( m_hTTWnd );
  75. }
  76. m_hTTWnd = hTTWnd;
  77. if( ::IsWindow(m_hTTWnd) )
  78. {
  79. m_bOwnTT = false;
  80. // if our uid tooltip already exists, get the data, else add it.
  81. if( ! ::SendMessage(m_hTTWnd, TTM_GETTOOLINFO, 0, (LPARAM)(LPTOOLINFO) &m_TI) )
  82. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  83. }
  84. else
  85. {
  86. m_bOwnTT = true;
  87. CreateToolTip();
  88. }
  89. }
  90. void CPopup::CreateToolTip()
  91. {
  92. if( m_hTTWnd != NULL )
  93. return;
  94. // CREATE A TOOLTIP WINDOW
  95. m_hTTWnd = CreateWindowEx(
  96. WS_EX_TOPMOST,
  97. TOOLTIPS_CLASS,
  98. NULL,
  99. TTS_NOPREFIX | TTS_ALWAYSTIP,
  100. CW_USEDEFAULT,
  101. CW_USEDEFAULT,
  102. CW_USEDEFAULT,
  103. CW_USEDEFAULT,
  104. NULL,
  105. NULL,
  106. NULL,
  107. NULL
  108. );
  109. m_bOwnTT = true;
  110. // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
  111. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  112. }
  113. void CPopup::SetTimeout( int timeout )
  114. {
  115. if( m_hTTWnd == NULL )
  116. return;
  117. ::SendMessage(m_hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, timeout);
  118. }
  119. void CPopup::SetPos( CPoint& pos )
  120. {
  121. m_Pos = pos;
  122. }
  123. void CPopup::SetPosInfo( bool bTop, bool bCenterY, bool bLeft, bool bCenterX )
  124. {
  125. m_bTop = bTop;
  126. m_bCenterY = bCenterY;
  127. m_bLeft = bLeft;
  128. m_bCenterX = bCenterX;
  129. }
  130. void CPopup::AdjustPos( CPoint& pos )
  131. {
  132. CRect rel(0,0,0,0);
  133. CRect rect(0,0,0,0);
  134. // ::SendMessage(m_hTTWnd, TTM_ADJUSTRECT, TRUE, (LPARAM)&rect);
  135. ::GetWindowRect(m_hTTWnd,&rect);
  136. if( ::IsWindow(m_hWndPosRelativeTo) )
  137. ::GetWindowRect(m_hWndPosRelativeTo, &rel);
  138. // move the rect to the relative origin
  139. rect.bottom = rect.Height() + rel.top;
  140. rect.top = rel.top;
  141. rect.right = rect.Width() + rel.left;
  142. rect.left = rel.left;
  143. // adjust the y position
  144. rect.OffsetRect( 0, pos.y - (m_bCenterY? rect.Height()/2: (m_bTop? 0: rect.Height())) );
  145. if( rect.bottom > m_ScreenMaxY )
  146. rect.OffsetRect( 0, m_ScreenMaxY - rect.bottom );
  147. // adjust the x position
  148. rect.OffsetRect( pos.x - (m_bCenterX? rect.Width()/2: (m_bLeft? 0: rect.Width())), 0 );
  149. if( rect.right > m_ScreenMaxX )
  150. rect.OffsetRect( m_ScreenMaxX - rect.right, 0 );
  151. pos.x = rect.left;
  152. pos.y = rect.top;
  153. }
  154. void CPopup::SendToolTipText( CString text )
  155. {
  156. m_csToolTipText = text;
  157. //Replace the tabs with spaces, the tooltip didn't like the \t s
  158. text.Replace(_T("\t"), _T(" "));
  159. m_TI.lpszText = (LPTSTR) (LPCTSTR) text;
  160. // this allows \n and \r to be interpreted correctly
  161. ::SendMessage(m_hTTWnd, TTM_SETMAXTIPWIDTH, 0, 500);
  162. // set the text
  163. ::SendMessage(m_hTTWnd, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  164. }
  165. void CPopup::Show( CString text, CPoint pos, bool bAdjustPos )
  166. {
  167. if( m_hTTWnd == NULL )
  168. return;
  169. m_csToolTipText = text;
  170. if( !m_bIsShowing )
  171. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(-10000,-10000));
  172. SendToolTipText( text );
  173. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_TI);
  174. if( bAdjustPos )
  175. AdjustPos(pos);
  176. // set the position
  177. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(pos.x,pos.y));
  178. // make sure the tooltip will be on top.
  179. ::SetWindowPos( m_hTTWnd, m_hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE );
  180. m_bIsShowing = true;
  181. }
  182. void CPopup::Show( CString text )
  183. {
  184. m_csToolTipText = text;
  185. Show( text, m_Pos );
  186. }
  187. void CPopup::AllowShow( CString text )
  188. {
  189. m_csToolTipText = text;
  190. if( m_bAllowShow )
  191. Show( text, m_Pos );
  192. }
  193. void CPopup::Hide()
  194. {
  195. if( m_hTTWnd == NULL )
  196. return;
  197. // deactivate if it is currently activated
  198. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_TI);
  199. m_bIsShowing = false;
  200. }