Solved

Tracking tooltips in Visual C++

Posted on 2008-06-24
3
1,402 Views
Last Modified: 2013-11-20
I wish to create a simple dialog-based application (using MFC) with a tooltip that can display different text according to where the mouse pointer is located, and also switch off this tooltip at other regions
of the dialog.

Any suggestions or pointers to existing code would be most welcome.

Thanks in advance

Andy
0
Comment
Question by:arrayjet_vin
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 125 total points
ID: 21857783
Whenever I want to add a tooltip to a control, I use the following tooltip class, featured in "Programming Windows with MFC" by Jeff Prosise.

class CMyToolTipCtrl : public CToolTipCtrl
{
public:
    BOOL AddWindowTool (CWnd* pWnd, LPCTSTR pszText);
    BOOL AddRectTool (CWnd* pWnd, LPCTSTR pszText,
        LPCRECT pRect, UINT nIDTool);
};

BOOL CMyToolTipCtrl::AddWindowTool (CWnd* pWnd, LPCTSTR pszText)
{
    TOOLINFO ti;
    ti.cbSize = sizeof (TOOLINFO);
    ti.uFlags = TTF_IDISHWND ¦ TTF_SUBCLASS;
    ti.hwnd = pWnd->GetParent ()->GetSafeHwnd ();
    ti.uId = (UINT) pWnd->GetSafeHwnd ();
    ti.hinst = AfxGetInstanceHandle ();
    ti.lpszText = (LPTSTR) pszText;

    return (BOOL) SendMessage (TTM_ADDTOOL, 0, (LPARAM) &ti);
}

BOOL CMyToolTipCtrl::AddRectTool (CWnd* pWnd, LPCTSTR pszText,
    LPCRECT lpRect, UINT nIDTool)
{
    TOOLINFO ti;
    ti.cbSize = sizeof (TOOLINFO);
    ti.uFlags = TTF_SUBCLASS;
    ti.hwnd = pWnd->GetSafeHwnd ();
    ti.uId = nIDTool;
    ti.hinst = AfxGetInstanceHandle ();
    ti.lpszText = (LPTSTR) pszText;
    ::CopyRect (&ti.rect, lpRect);

    return (BOOL) SendMessage (TTM_ADDTOOL, 0, (LPARAM) &ti);
}

"With this infrastructure in place, creating a tool from a child window controlsubclassing and allrequires just one simple statement:

m_ctlTT.AddWindowTool (pWnd, _T ("This is a window"));

Creating a tool from a rectangle in a window is equally simple:

m_ctlTT.AddRectTool (pWnd, _T ("This is a rectangle"),
    CRect (32, 32, 64, 64), IDT_RECTANGLE);
"

Assuming m_ctlTT is a CMyToolTipCtrl object, which has been constructed and Create()'d, in the code above, we add a tooltip to the control 'pwnd' and the rectangle with bounds (32, 32, 64, 64) within 'pwnd'.

I think that has answered your question body, but your question *title* mentions "tracking" tooltips, which are tooltips that redraw themselves very quickly so the effect of "tracking" with the mouse pointer is achieved.  The above code does not satisy that, and, interestingly enough, I myself have never found a working solution to achieve this, although I have seen it many times in various apps.

HTH
0
 

Author Comment

by:arrayjet_vin
ID: 21865786
Thanks for the feedback mrwad99.

I think I have figured out how to track the tooltip, inside the message handling function dealing with mouse movements. It seems to work fine, I've omitted irrelevant imformation to help clarify it a bit:

void CMultiplePrintrunDialog::OnMouseMove( UINT nFlags, CPoint point )
{      
      // extraneous code here etc    

        BOOL bToolTip = // Code to set BOOL bToolTip to TRUE/FALSE, a function of the [x,y] position of the            
                                    // mouse pointer and various other things...

      // If TRUE then activate tooltip                   
      if ( bToolTip == TRUE )
      {
            // Set the tooltip text to be displayed            
            CString tooltip = // depends on location of mouse pointer and other stuff...;                  
            
            // Create the tooltip
            hwndTT = CreateWindowEx(
                                    WS_EX_TOPMOST,
                                    TOOLTIPS_CLASS,
                                    NULL,
                                    TTS_NOPREFIX | TTS_ALWAYSTIP,            
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL
                                    );

            // Initialise members of the TOOLINFO structure, including the text to be displayed            
            ti.cbSize = sizeof( TOOLINFO );
            ti.uFlags = TTF_TRACK;
            ti.hwnd   = NULL;
            ti.hinst  = NULL;
            ti.uId    = 0;
            ti.lpszText = (LPSTR)(LPCSTR) tooltip;                              
            
            // Register tool with tooltip control
            ::SendMessage(
                        hwndTT,
                        TTM_ADDTOOL,
                        0,
                        (LPARAM) (LPTOOLINFO) &ti
                        );

            // Set the position of the tracking ToolTip
                int x_val = // Some code to ascertain this;
            int y_val = // ditto
            ::SendMessage(
                        hwndTT,
                        TTM_TRACKPOSITION,
                        0,
                        (LPARAM)(DWORD) MAKELONG( x_val, y_val )
                        );

            // Activate the tracking tooltip
            ::SendMessage(
                        hwndTT,
                        TTM_TRACKACTIVATE,
                        TRUE,
                        (LPARAM)(LPTOOLINFO) &ti
                        );            
      }      
        // Otherwise deactivate the tracking tooltip
      else
      {                  
            ::SendMessage(
                        hwndTT,
                        TTM_TRACKACTIVATE,
                        FALSE,
                        (LPARAM)(LPTOOLINFO) &ti
                        );                              
      }
            
      CDialog::OnMouseMove(nFlags, point);
}
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 21865931
Glad you got it sorted.  If you feel my comment assisted, please close this question by accepting it as an answer :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel Use VBA to get user's Mac Address for their computer 5 89
sum67 challenge 35 89
MaxSpan challenge 9 68
sumHeights2  challenge 7 78
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now