BmyGuest
BmyGuest

Reputation: 2949

How to update ToolTip text each time before display?

How can I make a toolTip text updating itself each time the tooltip is (about to be) displayed ?

I have a CDialog derived dialog which uses CToolTipCtrl tooltips in the usual way and it works just fine:

I also know how to update the toolTip text in various places of the code using CToolTipCtrl::UpdateTipText and CToolTipCtrl::Update

However, what I want and have not yet accomplished is this: I want that the text of the tooltip updated each time the mouse hoovers over the tool before the according tooltip is displayed, i.e. the displayed text is dependent on the situation the moment the tooltip-text is displayed.

My working code so far (truncated to relevant lines):

class CmyDialog : public CDialog
{
  virtual BOOL OnInitDialog();
  virtual BOOL PreTranslateMessage(MSG* pMsg);
  virtual void RefreshToolTipText();        // Want to call this prior each display

  CToolTipCtrl m_toolTip;
}

BOOL CmyDialog::OnInitDialog()
{
  CDialog::OnInitDialog();
  m_toolTip.Create(this);
  m_toolTip.AddTool( GetDlgItem(IDC_SOMECONTROLID), "Sometext" );
  m_toolTip.Activate( TRUE );
}

BOOL CmyDialog::PreTranslateMessage(MSG* pMsg)
{
  if(IsWindow(m_toolTip.m_hWnd)) 
     m_toolTip.RelayEvent(pMsg); 
}

void CmyDialog::RefreshToolTipText()
{
  m_toolTip.UpdateTipText( "updated runtime text",  GetDlgItem(IDC_SOMECONTROLID) );
  m_toolTip.Update();   
}

Upvotes: 5

Views: 6592

Answers (2)

BmyGuest
BmyGuest

Reputation: 2949

I seem to have figured it out myself. As I couldn't find the solution online, I'm going to post it here for references. I would still appreciate comments if there are any.

I've added the following line the message map of the CmyDialog class:

BEGIN_MESSAGE_MAP(CmyDialog, CDialog)
   ON_NOTIFY( TTN_SHOW, 0, OnToolTipTextAboutToShow )   
END_MESSAGE_MAP()

And I've added the following member function to CmyDialog:

void CmyDialog::OnToolTipTextAboutToShow(NMHDR * pNotifyStruct, LRESULT* result)
{
   if ( pNotifyStruct->hwndFrom == m_toolTip.m_hWnd )
       RefreshToolTipText();
}

Apparently, the TTN_SHOW notification code gets send via WM_NOTIFY each time a tooltip is about to be displayed. The if-check in my function checks that the toolTip is from the specific CToolTipCtrl.

Upvotes: 1

Nik Bougalis
Nik Bougalis

Reputation: 10613

When calling CToolTipCtrl::AddTool use the "special" value LPSTR_TEXTCALLBACK as the text to use for the tooltip. This will cause the tooltip to post a TTN_NEEDTEXT notification to the parent of the window you are adding a tooltip for. The parent can then set the text accordingly.

Upvotes: 3

Related Questions