Solved

Tracking tooltips in Visual C++

Posted on 2008-06-24
3
1,440 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modbus - whats the maximum I can store in one register? 4 80
Remove Office macro by VC++ 10 105
Work with App store 7 71
Increment column based of a FK 8 32
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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