Solved

Dynamic tooltip

Posted on 2004-08-05
17
2,291 Views
Last Modified: 2013-11-20
I have CWnd-derived class and need to show dynamic tooltip in this window. Window doesn't have child windows and must show tooltip for itself. Tooltip string depends on the mouse position (for example, it may show x and y coordinates). Tooltip may be very simple (no need in multiline, balloon etc.)
0
Comment
Question by:AlexFM
  • 7
  • 4
  • 2
  • +2
17 Comments
 
LVL 12

Expert Comment

by:migel
ID: 11723978
Hi!
IMHO you need to use MFC buildin tooltipsupport
call EnableToolTips(TRUE) int he your CWnd derived window
and catch TTN_NEEDTEXT notify.

ON_NOTIFY_EX( TTN_NEEDTEXT, 0, ToolTipNeedText)

for example:
BOOL CMyWnd::ToolTipNeedText( UINT id, NMHDR* pTTTStruct, LRESULT* pResult )
{
      TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;

      static CString string;
      int col;
      long row;
      POINT point;

      GetCursorPos(&point);
      ScreenToClient(&point);
         string.Format("pos : %d: %d", point.x, point.y);
         pTTT->lpszText = const_cast<LPTSTR>((LPCTSTR)string);
         return TRUE;
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11724086
You can implement basic tooltip functionality without CToolTipCtrl. Just derive a CWnd object:

MyToolTip.h
-----------
class CMyTooltip : public CWnd
{
public: // create from serialization only
    CMyTooltip() {};
    DECLARE_DYNCREATE(CMyTooltip)
public:
    virtual ~CMyTooltip() {};

protected:
    //{{AFX_MSG(CMyTooltip)
    afx_msg void OnPaint();
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

MyToolTip.cpp

IMPLEMENT_DYNCREATE(CMyTooltip, CWnd)

BEGIN_MESSAGE_MAP(CMyTooltip, CWnd)
    //{{AFX_MSG_MAP(CMyTooltip)
    ON_WM_PAINT()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CMyTooltip::OnPaint()
{
    CPaintDC dc(this); // device context for painting
        CRect rect;
        GetClientRect(&rect);
        dc.SelectStockObject(BLACK_PEN);
        dc.SelectStockObject(WHITE_BRUSH);
        dc.Rectangle(&rect);
        dc.SetBkMode(TRANSPARENT);
        dc.DrawText("test", &rect, DT_LEFT);    
}

--------------------------

In your view class, declare a CMyTooltip object as member.

In the create function write this:
    tt.Create(NULL, "ToolTip", WS_OVERLAPPED|WS_VISIBLE, CRect(0,0,80,20), this, 100);
    tt.UpdateWindow();
    tt.ShowWindow(SW_HIDE);

In the mouse move event insert this:
    if (MustShowTip(point)) {   <---- your function here
        tt.MoveWindow(point.x, point.y+20, 100, 20, TRUE);
        tt.ShowWindow(SW_SHOWNOACTIVATE);
    } else
        tt.ShowWindow(SW_HIDE);

That's all, to enhance it, decorate in the OnPaint event of CMyToolTip
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11724231
migel: ToolTipNeedText function is never called. Maybe because window is placed on the toolbar. I will try this way with dialog.

jaime_olivares: I want to use standard tooltip, I beleive this is simple way. MustShowTip function in your answer may be complicated.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Author Comment

by:AlexFM
ID: 11724241
By the way, there is the sample program which shows tooltip in the form x = ...  y = ... for the view class. It may be in CodeGuru, CodeProject or somewhere else. I beleive it can be OK for me, but I cannot find it.
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11724350
I want to ask this question by other way:
I have CWnd-derived window placed on toolbar's parent on SDI application. In this window I want to show dynamic tooltip based on the mouse position. Window is created using this code:

void CMainFrame::CreateIconsWindow()
{
    m_pIconsWnd = new CIconsWnd();    // CIconsWnd is CWnd-derived class

    CRect rect;
    m_wndToolBar.GetParent()->GetClientRect(&rect);

    rect.top = rect.bottom - 300;   // toolbar is vertical

    m_pIconsWnd->Create(
        NULL,
        _T(""),
        WS_CHILD | WS_VISIBLE,
        rect,
        m_wndToolBar.GetParent(),
        ID_ICONWND);
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11724375
0
 
LVL 12

Expert Comment

by:migel
ID: 11724441
Okay
I have to write some test project :-)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11724895
re your comment - migel: ToolTipNeedText function is never called. Maybe because window is placed on the toolbar. I will try this way with dialog.

Alex - are you pumping the tooltip control in the OnMouseMove of your window?  Do you have a tooltip in your window?
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11724953
I don't have a tooltip, and I want to do it. Window is created as shown in my previous post. It is child of status bar parent and placed after status bar buttons. I don't use OnMouseMove. One of jaime_olivares' links shows how to display tooltip from OnMousMove function, but this is not what I need.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11725878
In .h for the window

      CToolTipCtrl      m_tooltip;


in .cpp


BOOL CSortDlg::PreTranslateMessage(MSG* pMsg)
{
      // CG: The following block was added by the ToolTips component.
      {
            // Let the ToolTip process this message.
            m_tooltip.RelayEvent(pMsg);
      }
    if(pMsg->message == WM_MOUSEMOVE)
      {
            m_tooltip.Update();  //DYNAMIC TIPS
      }


in OnCreate of the window
            m_tooltip.Create(this, TTS_NOPREFIX);
            m_tooltip.SetMaxTipWidth(600);      //allow it to be very wide
            m_tooltip.SetDelayTime(TTDT_AUTOPOP, 10000);      //display for 10 seconds (stop flashing)
            m_tooltip.Activate(TRUE);

            m_tooltip.AddTool(this, LPSTR_TEXTCALLBACK);


handle the TTN_NEEDTEXT as migel suggests - this should work.
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11741676
ToolTipNeedText function is not called.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11742023
The tool is with LPSTR_TEXTCALLBACK
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11742026
Is your window getting a mousemove event?
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11742094
Yes, it gets WM_MOUSEMOVE.

Learning DTTest sample from this article, it has functionality I need:

http://www.microsoft.com/msj/0497/tooltip/tooltip.aspx
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11745797
I found another solution using some tricks with WM_MOUSEMOVE, WM_MOUSELEAVE and showing prompt on the status bar. Tooltip doesn't want to work in this situation.
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11785477
Closed, 500 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 139
fix34  challenge 9 167
Annoying "thing" blocks my view 4 133
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 116
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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