Popup.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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;
  59. GetMonitorRect(-1, &rcScreen);
  60. m_ScreenMaxX = rcScreen.right;
  61. m_ScreenMaxY = rcScreen.bottom;
  62. m_hWndInsertAfter = HWND_TOP; //HWND_TOPMOST
  63. SetTTWnd();
  64. }
  65. void CPopup::SetTTWnd( HWND hTTWnd, TOOLINFO* pTI )
  66. {
  67. if( pTI )
  68. m_TI = *pTI;
  69. else
  70. InitToolInfo( m_TI );
  71. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  72. {
  73. if( !::IsWindow(hTTWnd) )
  74. return; // we would have to recreate the one that already exists
  75. ::DestroyWindow( m_hTTWnd );
  76. }
  77. m_hTTWnd = hTTWnd;
  78. if( ::IsWindow(m_hTTWnd) )
  79. {
  80. m_bOwnTT = false;
  81. // if our uid tooltip already exists, get the data, else add it.
  82. if( ! ::SendMessage(m_hTTWnd, TTM_GETTOOLINFO, 0, (LPARAM)(LPTOOLINFO) &m_TI) )
  83. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  84. }
  85. else
  86. {
  87. m_bOwnTT = true;
  88. CreateToolTip();
  89. }
  90. }
  91. void CPopup::CreateToolTip()
  92. {
  93. if( m_hTTWnd != NULL )
  94. return;
  95. // CREATE A TOOLTIP WINDOW
  96. m_hTTWnd = CreateWindowEx(
  97. WS_EX_TOPMOST,
  98. TOOLTIPS_CLASS,
  99. NULL,
  100. TTS_NOPREFIX | TTS_ALWAYSTIP,
  101. CW_USEDEFAULT,
  102. CW_USEDEFAULT,
  103. CW_USEDEFAULT,
  104. CW_USEDEFAULT,
  105. NULL,
  106. NULL,
  107. NULL,
  108. NULL
  109. );
  110. m_bOwnTT = true;
  111. // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
  112. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  113. }
  114. void CPopup::SetTimeout( int timeout )
  115. {
  116. if( m_hTTWnd == NULL )
  117. return;
  118. ::SendMessage(m_hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, timeout);
  119. }
  120. void CPopup::SetPos( CPoint& pos )
  121. {
  122. m_Pos = pos;
  123. }
  124. void CPopup::SetPosInfo( bool bTop, bool bCenterY, bool bLeft, bool bCenterX )
  125. {
  126. m_bTop = bTop;
  127. m_bCenterY = bCenterY;
  128. m_bLeft = bLeft;
  129. m_bCenterX = bCenterX;
  130. }
  131. void CPopup::AdjustPos( CPoint& pos )
  132. {
  133. CRect rel(0,0,0,0);
  134. CRect rect(0,0,0,0);
  135. // ::SendMessage(m_hTTWnd, TTM_ADJUSTRECT, TRUE, (LPARAM)&rect);
  136. ::GetWindowRect(m_hTTWnd,&rect);
  137. if( ::IsWindow(m_hWndPosRelativeTo) )
  138. ::GetWindowRect(m_hWndPosRelativeTo, &rel);
  139. // move the rect to the relative origin
  140. rect.bottom = rect.Height() + rel.top;
  141. rect.top = rel.top;
  142. rect.right = rect.Width() + rel.left;
  143. rect.left = rel.left;
  144. // adjust the y position
  145. rect.OffsetRect( 0, pos.y - (m_bCenterY? rect.Height()/2: (m_bTop? 0: rect.Height())) );
  146. if( rect.bottom > m_ScreenMaxY )
  147. rect.OffsetRect( 0, m_ScreenMaxY - rect.bottom );
  148. // adjust the x position
  149. rect.OffsetRect( pos.x - (m_bCenterX? rect.Width()/2: (m_bLeft? 0: rect.Width())), 0 );
  150. if( rect.right > m_ScreenMaxX )
  151. rect.OffsetRect( m_ScreenMaxX - rect.right, 0 );
  152. pos.x = rect.left;
  153. pos.y = rect.top;
  154. }
  155. void CPopup::SendToolTipText( CString text )
  156. {
  157. m_csToolTipText = text;
  158. //Replace the tabs with spaces, the tooltip didn't like the \t s
  159. text.Replace(_T("\t"), _T(" "));
  160. m_TI.lpszText = (LPTSTR) (LPCTSTR) text;
  161. // this allows \n and \r to be interpreted correctly
  162. ::SendMessage(m_hTTWnd, TTM_SETMAXTIPWIDTH, 0, 500);
  163. // set the text
  164. ::SendMessage(m_hTTWnd, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  165. }
  166. void CPopup::Show( CString text, CPoint pos, bool bAdjustPos )
  167. {
  168. if( m_hTTWnd == NULL )
  169. return;
  170. m_csToolTipText = text;
  171. if( !m_bIsShowing )
  172. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(-10000,-10000));
  173. SendToolTipText( text );
  174. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_TI);
  175. if( bAdjustPos )
  176. AdjustPos(pos);
  177. // set the position
  178. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(pos.x,pos.y));
  179. // make sure the tooltip will be on top.
  180. ::SetWindowPos( m_hTTWnd, m_hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE );
  181. m_bIsShowing = true;
  182. }
  183. void CPopup::Show( CString text )
  184. {
  185. m_csToolTipText = text;
  186. Show( text, m_Pos );
  187. }
  188. void CPopup::AllowShow( CString text )
  189. {
  190. m_csToolTipText = text;
  191. if( m_bAllowShow )
  192. Show( text, m_Pos );
  193. }
  194. void CPopup::Hide()
  195. {
  196. if( m_hTTWnd == NULL )
  197. return;
  198. // deactivate if it is currently activated
  199. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_TI);
  200. m_bIsShowing = false;
  201. }