ToolTipEx.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // ToolTipEx.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "ToolTipEx.h"
  6. #include "BitmapHelper.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define DELETE_BITMAP if(m_pBitmap) \
  13. { \
  14. m_pBitmap->DeleteObject(); \
  15. DELETE_PTR(m_pBitmap); \
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CToolTipEx
  19. CToolTipEx::CToolTipEx() :
  20. m_dwTextStyle(DT_EXPANDTABS|DT_EXTERNALLEADING|DT_NOPREFIX|DT_WORDBREAK),
  21. m_rectMargin(2, 2, 3, 3),
  22. m_pBitmap(NULL)
  23. {
  24. }
  25. CToolTipEx::~CToolTipEx()
  26. {
  27. DELETE_BITMAP
  28. m_Font.DeleteObject();
  29. }
  30. BEGIN_MESSAGE_MAP(CToolTipEx, CWnd)
  31. //{{AFX_MSG_MAP(CToolTipEx)
  32. ON_WM_PAINT()
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CToolTipEx message handlers
  37. BOOL CToolTipEx::Create(CWnd* pParentWnd)
  38. {
  39. // Get the class name and create the window
  40. CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS,
  41. LoadCursor(NULL, IDC_ARROW));
  42. // Create the window - just don't show it yet.
  43. if (!CWnd::CreateEx(WS_EX_TOPMOST, szClassName, _T(""),
  44. WS_POPUP|WS_BORDER,
  45. 0, 0, 10, 10, // size & position updated when needed
  46. pParentWnd->GetSafeHwnd(), 0, NULL))
  47. {
  48. return FALSE;
  49. }
  50. SetLogFont(GetSystemToolTipFont(), FALSE);
  51. return TRUE;
  52. }
  53. BOOL CToolTipEx::Show(CPoint point)
  54. {
  55. CRect rect = GetBoundsRect();
  56. CRect rcScreen;
  57. CRect crRectToScreen(point, CPoint(point.x + rect.right, point.y + rect.bottom));
  58. int nMonitor = GetMonitorFromRect(&crRectToScreen);
  59. GetMonitorRect(nMonitor, &rcScreen);
  60. if(crRectToScreen.right > rcScreen.right)
  61. {
  62. point.x -= (crRectToScreen.right - rcScreen.right);
  63. ///Add a border
  64. point.x -= 2;
  65. }
  66. if(crRectToScreen.bottom > rcScreen.bottom)
  67. {
  68. point.y -= (crRectToScreen.bottom - rcScreen.bottom);
  69. //add a border
  70. point.y -= 2;
  71. }
  72. ShowWindow(SW_HIDE);
  73. SetWindowPos(NULL,
  74. point.x, point.y,
  75. rect.Width(), rect.Height(),
  76. SWP_SHOWWINDOW|SWP_NOCOPYBITS|SWP_NOACTIVATE|SWP_NOZORDER);
  77. return TRUE;
  78. }
  79. BOOL CToolTipEx::Hide()
  80. {
  81. DELETE_BITMAP
  82. ShowWindow(SW_HIDE);
  83. return TRUE;
  84. }
  85. void CToolTipEx::OnPaint()
  86. {
  87. CPaintDC dc(this); // device context for painting
  88. CRect rect;
  89. GetClientRect(rect);
  90. CBrush Brush, *pOldBrush;
  91. Brush.CreateSolidBrush(GetSysColor(COLOR_INFOBK));
  92. pOldBrush = dc.SelectObject(&Brush);
  93. CFont *pOldFont = dc.SelectObject(&m_Font);
  94. dc.FillRect(&rect, &Brush);
  95. // Draw Text
  96. dc.SetBkMode(TRANSPARENT);
  97. rect.DeflateRect(m_rectMargin);
  98. if(m_pBitmap)
  99. {
  100. CDC MemDc;
  101. MemDc.CreateCompatibleDC(&dc);
  102. CBitmap* oldBitmap = MemDc.SelectObject(m_pBitmap);
  103. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  104. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  105. dc.BitBlt(rect.left, rect.top, nWidth, nHeight, &MemDc, 0, 0, SRCCOPY);
  106. MemDc.SelectObject(oldBitmap);
  107. rect.top += nHeight;
  108. }
  109. dc.DrawText(m_csText, rect, m_dwTextStyle);
  110. // Cleanup
  111. dc.SelectObject(pOldBrush);
  112. dc.SelectObject(pOldFont);
  113. }
  114. void CToolTipEx::PostNcDestroy()
  115. {
  116. CWnd::PostNcDestroy();
  117. delete this;
  118. }
  119. BOOL CToolTipEx::OnMsg(MSG* pMsg)
  120. {
  121. switch(pMsg->message)
  122. {
  123. case WM_WINDOWPOSCHANGING:
  124. case WM_LBUTTONDOWN:
  125. {
  126. //if (!IsCursorInToolTip())
  127. Hide();
  128. break;
  129. }
  130. case WM_KEYDOWN:
  131. {
  132. if(IsWindowVisible())
  133. {
  134. Hide();
  135. WPARAM vk = pMsg->wParam;
  136. if(vk == VK_ESCAPE)
  137. {
  138. return TRUE;
  139. }
  140. }
  141. else
  142. Hide();
  143. break;
  144. }
  145. case WM_LBUTTONDBLCLK:
  146. case WM_RBUTTONDOWN :
  147. case WM_RBUTTONDBLCLK:
  148. case WM_MBUTTONDOWN:
  149. case WM_MBUTTONDBLCLK:
  150. case WM_NCLBUTTONDOWN:
  151. case WM_NCLBUTTONDBLCLK:
  152. case WM_NCRBUTTONDOWN:
  153. case WM_NCRBUTTONDBLCLK:
  154. case WM_NCMBUTTONDOWN:
  155. case WM_NCMBUTTONDBLCLK:
  156. {
  157. Hide();
  158. break;
  159. }
  160. }
  161. return FALSE;
  162. }
  163. CRect CToolTipEx::GetBoundsRect()
  164. {
  165. CWindowDC dc(NULL);
  166. CFont *pOldFont = (CFont*) dc.SelectObject((CFont*)&m_Font);
  167. int nLineWidth = 0;
  168. if (nLineWidth == 0)
  169. {
  170. // Count the number of lines of text
  171. int nStart = 0, nNumLines = 0;
  172. CString strTextCopy = m_csText;
  173. do
  174. {
  175. nStart = strTextCopy.Find(_T("\n"));
  176. // skip found character
  177. if (nStart >= 0)
  178. strTextCopy = strTextCopy.Mid(nStart+1);
  179. nNumLines++;
  180. } while (nStart >= 0);
  181. // Find the widest line
  182. for (int i = 0; i < nNumLines; i++)
  183. {
  184. CString strLine = GetFieldFromString(m_csText, i, _T('\n')) + _T(" ");
  185. nLineWidth = max(nLineWidth, dc.GetTextExtent(strLine).cx);
  186. }
  187. }
  188. CRect rect(0, 0, max(0,nLineWidth), 0);
  189. dc.DrawText(m_csText, rect, DT_CALCRECT | m_dwTextStyle);
  190. dc.SelectObject(pOldFont);
  191. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  192. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  193. if(m_pBitmap)
  194. {
  195. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  196. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  197. rect.bottom += nHeight;
  198. if((rect.left + nWidth) > rect.right)
  199. rect.right = rect.left + nWidth;
  200. }
  201. return rect;
  202. }
  203. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  204. {
  205. CString strReturn;
  206. LPCTSTR pstrStart = ref.LockBuffer();
  207. LPCTSTR pstrBuffer = pstrStart;
  208. int nCurrent = 0;
  209. int nStart = 0;
  210. int nEnd = 0;
  211. int nOldStart = 0;
  212. while (nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  213. {
  214. if (*pstrBuffer == ch)
  215. {
  216. nOldStart = nStart;
  217. nStart = nEnd+1;
  218. nCurrent++;
  219. }
  220. nEnd++;
  221. pstrBuffer++;
  222. }
  223. // May have reached the end of the string
  224. if (*pstrBuffer == _T('\0'))
  225. {
  226. nOldStart = nStart;
  227. nEnd++;
  228. }
  229. ref.UnlockBuffer();
  230. if (nCurrent < nIndex)
  231. {
  232. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  233. return strReturn;
  234. }
  235. return ref.Mid(nOldStart, nEnd-nOldStart-1);
  236. }
  237. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  238. {
  239. static LOGFONT LogFont;
  240. NONCLIENTMETRICS ncm;
  241. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  242. if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0))
  243. return FALSE;
  244. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  245. return &LogFont;
  246. }
  247. BOOL CToolTipEx::SetLogFont(LPLOGFONT lpLogFont, BOOL bRedraw /*=TRUE*/)
  248. {
  249. ASSERT(lpLogFont);
  250. if (!lpLogFont)
  251. return FALSE;
  252. LOGFONT LogFont;
  253. // Store font as the global default
  254. memcpy(&LogFont, lpLogFont, sizeof(LOGFONT));
  255. // Create the actual font object
  256. m_Font.DeleteObject();
  257. m_Font.CreateFontIndirect(&LogFont);
  258. if (bRedraw && ::IsWindow(GetSafeHwnd()))
  259. Invalidate();
  260. return TRUE;
  261. }
  262. void CToolTipEx::SetBitmap(CBitmap *pBitmap)
  263. {
  264. DELETE_BITMAP
  265. m_pBitmap = pBitmap;
  266. }