ToolTipEx.cpp 6.8 KB

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