home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / TIPS.ZIP / prj / ToolTips / ToolTipDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-14  |  4.7 KB  |  173 lines

  1. //==========================================================================
  2. // File : ToolTipDialog.cpp
  3. //
  4. // Implementation of class CToolTipDialog
  5. //
  6. // Author : H. Devos
  7. //
  8. // Created : 14/10/98
  9. //
  10. //                                         (C) n.v. dZine
  11. //==========================================================================
  12.  
  13. #include <AfxWin.h>
  14. #include <AfxCmn.h>
  15. #include "ToolTipDialog.h"
  16.  
  17. IMPLEMENT_DYNAMIC(CToolTipDialog, CDialog)
  18.  
  19. BEGIN_MESSAGE_MAP(CToolTipDialog, CDialog)
  20.     ON_WM_DESTROY()
  21. END_MESSAGE_MAP()
  22.  
  23. //--------------------------------------------------------------------------
  24. // Static variables
  25. //--------------------------------------------------------------------------
  26. BOOL CToolTipDialog::c_bShowToolTips = TRUE;
  27.  
  28. //--------------------------------------------------------------------------
  29. // Constructors and Destructors
  30. //--------------------------------------------------------------------------
  31.  
  32. /***************************************************************************
  33. * CToolTipDialog::CToolTipDialog()
  34. *
  35. * Constructor
  36. *
  37. */
  38. CToolTipDialog::CToolTipDialog()
  39. {
  40. }
  41.  
  42. CToolTipDialog::CToolTipDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd) :
  43.     CDialog(lpszTemplateName, pParentWnd)
  44. {
  45. }
  46.  
  47. CToolTipDialog::CToolTipDialog(UINT nIDTemplate, CWnd* pParentWnd) :
  48.     CDialog(nIDTemplate, pParentWnd)
  49. {
  50. }
  51.  
  52. /***************************************************************************
  53. * CToolTipDialog::~CToolTipDialog()
  54. *
  55. * Destructor
  56. *
  57. */
  58. CToolTipDialog::~CToolTipDialog()
  59. {
  60. }
  61.  
  62.  
  63.  
  64. //--------------------------------------------------------------------------
  65. // Operations
  66. //--------------------------------------------------------------------------
  67.  
  68.  
  69. /***************************************************************************
  70. * CToolTipDialog::OnInitDialog()
  71. *
  72. * Description : Performs initialization:
  73. *                Reads the resource strings to use as tooltips and
  74. *                creates the tooltip control
  75. *
  76. * Return value : see CDialog::OnInitDialog()
  77. *
  78. */
  79. BOOL CToolTipDialog::OnInitDialog()
  80. {
  81.     BOOL bResult = CDialog::OnInitDialog();
  82.     m_wndToolTip.Create(this);
  83.     m_wndToolTip.Activate(c_bShowToolTips);
  84.     CWnd *pWndChild = GetWindow(GW_CHILD);
  85.     CString strToolTip;
  86.     while (pWndChild)
  87.     {
  88.         int nID = pWndChild->GetDlgCtrlID();
  89.         if (strToolTip.LoadString(nID))
  90.         {
  91.             m_wndToolTip.AddTool(pWndChild, strToolTip);
  92.         }
  93.         pWndChild = pWndChild->GetWindow(GW_HWNDNEXT);
  94.     }
  95.     return bResult;
  96. }
  97.  
  98.  
  99. /***************************************************************************
  100. * CToolTipDialog::OnDestroy()
  101. *
  102. * Description : Cleanup: destroys the tooltip control
  103. *
  104. */
  105. void CToolTipDialog::OnDestroy()
  106. {
  107.     m_wndToolTip.DestroyWindow();
  108. }
  109.  
  110.  
  111. /***************************************************************************
  112. * CToolTipDialog::PreTranslateMessage()
  113. *
  114. * Description : Override of CWnd::PreTranslateMessage
  115. *                Passes mose messages to the tooltip control
  116. *                Call this version of the function from your override
  117. *
  118. * Return value : see CWnd::PreTranslateMessage
  119. *
  120. * Parameters :
  121. *   - pMsg : The current message as received in ProcessMessageFilter
  122. *
  123. */
  124. BOOL CToolTipDialog::PreTranslateMessage(MSG *pMsg)
  125. {
  126.     // As pointed out by Richard Collins, the original code did not work
  127.     // for controls that have child windows, like combo boxes.
  128.     // My original idea to solve the problem was adding every
  129.     // child of the control using m_wndToolTip.AddTool(), but this
  130.     // didn't work as I expected. This piece of code however does
  131.     // the job.
  132.     // It is quite simple: If the window isn't a direct child, take
  133.     // its parent and continue taking the parent until you reach the dialog.
  134.     // This is a piece of code that gets called over and over again, so
  135.     // it has to be optimized a little bit more than some other code.
  136.     // This is why I use window handles instead of CWnd pointers.
  137.     // Using CWnd pointers would cause the window map being searched
  138.     // all the time and creating temporary CWnd objects.
  139.     if (c_bShowToolTips &&
  140.         pMsg->message >= WM_MOUSEFIRST &&
  141.         pMsg->message <= WM_MOUSELAST)
  142.     {
  143.         MSG msg;
  144.         ::CopyMemory(&msg, pMsg, sizeof(MSG));
  145.         HWND hWndParent = ::GetParent(msg.hwnd);
  146.         while (hWndParent && hWndParent != m_hWnd)
  147.         {
  148.             msg.hwnd = hWndParent;
  149.             hWndParent = ::GetParent(hWndParent);
  150.         }
  151.         if (msg.hwnd)
  152.         {
  153.             m_wndToolTip.RelayEvent(&msg);
  154.         }
  155.     }
  156.     return CDialog::PreTranslateMessage(pMsg);
  157. }
  158.  
  159.  
  160. /***************************************************************************
  161. * CToolTipDialog::EnableToolTips()
  162. *
  163. * Description : Enables/disables tooltips in this application
  164. *
  165. * Parameters :
  166. *   - bEnable : TRUE : enable
  167. *                FALSE : disable
  168. *
  169. */
  170. void CToolTipDialog::EnableToolTips(BOOL bEnable)
  171. {
  172.     c_bShowToolTips = bEnable;
  173. }