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:
CToolTipCtrl member variable in my CDialog class. CDialog::OnInitDialog
CToolTipCtrl in CDialog::PreTranslateMessage 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();
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With