Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use tooltips in mfc??

89 views
Skip to first unread message

Alexander Griesser

unread,
Jun 19, 2000, 3:00:00 AM6/19/00
to
hi!

is there any easy possibility, to use tooltips with mfc-dialogs??

i think, i've heared about a possibility, to enter the tooltip text directly
in the ressource editor, after a special escape sequence... is that right??
and if not, do you know any samples or something else, that would help me??

regards, alexx

Michiel de Boer

unread,
Jun 19, 2000, 3:00:00 AM6/19/00
to

Alexander Griesser <tu...@aon.at> wrote in message
news:394e2bf7$0$16...@SSP1NO25.highway.telekom.at...

Put a function like this in the public part of the dialog header file :

BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);

Then put this message in the message map of the cpp file :

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)

Then handle the function like this :

BOOL CSumbo2Dlg::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
{
TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pNMHDR;
UINT nID =pNMHDR->idFrom;
if (pTTT->uFlags & TTF_IDISHWND)
{
nID = ::GetDlgCtrlID((HWND)nID);
if(nID==2)
{
pTTT->lpszText = "This is the Cancel-button.";
return(TRUE);
}
if(nID==1003)
{
pTTT->lpszText = "This is ListControl 1.";
return(TRUE);
}
}
return(FALSE);
}

You can know the nID values from the (random?) ID-numbers that are given to
the controls when you create them.

Alexander Griesser

unread,
Jun 20, 2000, 3:00:00 AM6/20/00
to
> Put a function like this in the public part of the dialog header file :
> BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);

class MyDlg: public CDialog
{
// Konstruktion
public:
MyDlg(CWnd* pParent = NULL); // Standard-Konstruktor


BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
protected:
HICON m_hIcon;
...
...
DECLARE_MESSAGE_MAP()
};

ok, here it is.

> Then put this message in the message map of the cpp file :
> ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)

BEGIN_MESSAGE_MAP(MyDlg, CDialog)
//{{AFX_MSG_MAP(MyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
...
...
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

ok, should be right...

> Then handle the function like this :
> BOOL CSumbo2Dlg::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
> {
> TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pNMHDR;
> UINT nID =pNMHDR->idFrom;
> if (pTTT->uFlags & TTF_IDISHWND)
> {
> nID = ::GetDlgCtrlID((HWND)nID);
> if(nID==2)
> {
> pTTT->lpszText = "This is the Cancel-button.";
> return(TRUE);
> }
> if(nID==1003)
> {
> pTTT->lpszText = "This is ListControl 1.";
> return(TRUE);
> }
> }
> return(FALSE);
> }

but i don't get inside this function :(
what did i do wrong??

i started the debugger, and set a brekpoint at the top of this function, but
i didn't get into it :-(

do i have to create my buttons and so on with a special style??
i also tried to activate the "notification" checkbox in the ressource
editor, but the result was the same...

regards, tuxx

Michael Pitt

unread,
Jun 21, 2000, 3:00:00 AM6/21/00
to
Hi,

Actually if you modify the lines


if(nID==2)
{
pTTT->lpszText = "This is the Cancel-button.";
return(TRUE);
}
if(nID==1003)
{
pTTT->lpszText = "This is ListControl 1.";
return(TRUE);
}

and put instead

pTTT->lpszText = MAKEINTRESOURCE(nID);

you can put the string into a string resource using the control's ID rather
than embedding strings in the code.

*** BUT ***

My problem is that I can't seem to get it to work with ActiveX controls.
Even though they are assigned a unique ID in the resource editor and
hovering above the control causes OnToolTipNotify to be called, GetDlgCtrlID
returns an ID of zero so I cannot retrieve the text. ???

Any ideas?

Thanks
Michael Pitt

P.S. If you want a static text item to have a ToolTip you have to tick the
Notify checkbox on the property dialog.

"Michiel de Boer" <auve...@hotmail.com> wrote in message
news:8ilefc$26lf$1...@beast.euro.net...


>
> Alexander Griesser <tu...@aon.at> wrote in message
> news:394e2bf7$0$16...@SSP1NO25.highway.telekom.at...
> > hi!
> >
> > is there any easy possibility, to use tooltips with mfc-dialogs??
> >
> > i think, i've heared about a possibility, to enter the tooltip text
> directly
> > in the ressource editor, after a special escape sequence... is that
> right??
> > and if not, do you know any samples or something else, that would help
> me??
> >
> > regards, alexx
> >
> >
>

> Put a function like this in the public part of the dialog header file :
>
> BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);
>

> Then put this message in the message map of the cpp file :
>
> ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
>

> Then handle the function like this :
>
> BOOL CSumbo2Dlg::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
> {
> TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pNMHDR;
> UINT nID =pNMHDR->idFrom;
> if (pTTT->uFlags & TTF_IDISHWND)
> {
> nID = ::GetDlgCtrlID((HWND)nID);
> if(nID==2)
> {
> pTTT->lpszText = "This is the Cancel-button.";
> return(TRUE);
> }
> if(nID==1003)
> {
> pTTT->lpszText = "This is ListControl 1.";
> return(TRUE);
> }
> }
> return(FALSE);
> }
>

Michael Pitt

unread,
Jun 21, 2000, 3:00:00 AM6/21/00
to
Did you put a call to EnableToolTips() in your dialog code (say in
OnInitDialog) ??

By the way this is all documented in the MSDN/VC help. Search for the string
"Tool Tip Topics" and select the link "Not derived from CFrameWnd".

Regards
Michael

"Alexander Griesser" <tu...@aon.at> wrote in message

news:394fcac7$0$19...@SSP1NO25.highway.telekom.at...


> > Put a function like this in the public part of the dialog header file :
> > BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);
>

> class MyDlg: public CDialog
> {
> // Konstruktion
> public:
> MyDlg(CWnd* pParent = NULL); // Standard-Konstruktor

> BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> ...
> ...
> protected:
> HICON m_hIcon;
> ...
> ...
> DECLARE_MESSAGE_MAP()
> };
>
> ok, here it is.
>

> > Then put this message in the message map of the cpp file :
> > ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
>

> BEGIN_MESSAGE_MAP(MyDlg, CDialog)
> file://{{AFX_MSG_MAP(MyDlg)


> ON_WM_SYSCOMMAND()
> ON_WM_PAINT()
> ON_WM_QUERYDRAGICON()
> ...
> ...
> ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> file://}}AFX_MSG_MAP


> END_MESSAGE_MAP()
>
> ok, should be right...
>

> > Then handle the function like this :
> > BOOL CSumbo2Dlg::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT*
pResult)
> > {
> > TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pNMHDR;
> > UINT nID =pNMHDR->idFrom;
> > if (pTTT->uFlags & TTF_IDISHWND)
> > {
> > nID = ::GetDlgCtrlID((HWND)nID);
> > if(nID==2)
> > {
> > pTTT->lpszText = "This is the Cancel-button.";
> > return(TRUE);
> > }
> > if(nID==1003)
> > {
> > pTTT->lpszText = "This is ListControl 1.";
> > return(TRUE);
> > }
> > }
> > return(FALSE);
> > }
>

Michiel de Boer

unread,
Jun 21, 2000, 3:00:00 AM6/21/00
to

Michael Pitt <mich...@almitek.com.au> wrote in message
news:8inver$r5r$1...@bugstomper.ihug.com.au...

> Hi,
>
> Actually if you modify the lines
> if(nID==2)
> {
> pTTT->lpszText = "This is the Cancel-button.";
> return(TRUE);
> }
> if(nID==1003)
> {
> pTTT->lpszText = "This is ListControl 1.";
> return(TRUE);
> }
>
> and put instead
>
> pTTT->lpszText = MAKEINTRESOURCE(nID);
>
> you can put the string into a string resource using the control's ID
rather
> than embedding strings in the code.
>
> *** BUT ***
>
> My problem is that I can't seem to get it to work with ActiveX controls.
> Even though they are assigned a unique ID in the resource editor and
> hovering above the control causes OnToolTipNotify to be called,
GetDlgCtrlID
> returns an ID of zero so I cannot retrieve the text. ???
>
> Any ideas?
>
> Thanks
> Michael Pitt
>

I think I cannot help you here, all I can do is give you the original MSDN
example code :

if(nID==2)
{
pTTT->lpszText = MAKEINTRESOURCE(nID);
pTTT->hinst = AfxGetResourceHandle();
return(TRUE);
}

But I couldn't get this to work, so I modified it a little.

(Actually I don't really see the point of using the resources when it works
:-))


Michiel de Boer

unread,
Jun 21, 2000, 3:00:00 AM6/21/00
to
> > if(nID==2)
> > {
> > pTTT->lpszText = "This is the Cancel-button.";
> > return(TRUE);
> > }
> > if(nID==1003)
> > {
> > pTTT->lpszText = "This is ListControl 1.";
> > return(TRUE);
> > }
> > }
> > return(FALSE);
> > }
>
> but i don't get inside this function :(
> what did i do wrong??
>
> i started the debugger, and set a brekpoint at the top of this function,
but
> i didn't get into it :-(
>
> do i have to create my buttons and so on with a special style??
> i also tried to activate the "notification" checkbox in the ressource
> editor, but the result was the same...
>
> regards, tuxx
>
>

Yes, I am sorry, I forgot something.

Just like Michael said, I put EnableToolTips(TRUE); inside the OnInitDialog
member.


Alexander Griesser

unread,
Jun 21, 2000, 3:00:00 AM6/21/00
to

> Yes, I am sorry, I forgot something.
> Just like Michael said, I put EnableToolTips(TRUE); inside the
OnInitDialog
> member.

No problem ;)
Now it works *happy*

btw: i didn't use the numerical id, instead i wrote something like this:

...
if(nID==IDCANCEL)
...

it works and you know exactly, which button you call...
because IDCANCEL is just a definiton for its value...

regards, alexx

0 new messages