Hey all
I have been trying the whole night long to write a program that would display a tooltip control at an arbitrary position on screen (without making it a child of an existing window), WITHOUT using these terrible MFC classes... I've scoured the net for code samples and stuff, but all that I've succeeded in coming up was something like this :
Code:
// don't forget to link with comctl32.lib when you compile this app
#include <windows.h>
#include <commctrl.h>
HWND CreateToolTip (HINSTANCE hInst, HWND hWndParent, const char *string)
{
static HWND hWndToolTip = NULL;
INITCOMMONCONTROLSEX iccex;
TOOLINFO ti;
if ((string == NULL) || (string[0] == 0))
return (NULL); // reliability check
// is a tooltip already displayed ?
if (hWndToolTip != NULL)
{
SendMessage (hWndToolTip, WM_CLOSE, 0, 0); // then destroy it first
hWndToolTip = NULL;
}
// initialize the Windows Common Controls library
iccex.dwICC = ICC_WIN95_CLASSES;
iccex.dwSize = sizeof (iccex);
InitCommonControlsEx (&iccex);
// create a tooltip window
hWndToolTip = CreateWindow (TOOLTIPS_CLASS, // common dialog style tooltip
NULL, // title bar
WS_POPUP // popup window
| TTS_NOPREFIX // prevents ampersand stripping
| TTS_BALLOON // defined as 0x40 (but not featured in MSVC6 headers)
| TTS_ALWAYSTIP, // allows tip from inactive window
0, 0, // left/top positions
0, 0, // width/height of window
NULL, // the parent window
NULL, // no menu
hInst, // window handle
NULL); // no extra parameters
if (hWndToolTip != NULL)
{
ti.cbSize = sizeof (ti);
ti.uFlags = TTF_TRANSPARENT | TTF_CENTERTIP;
ti.hwnd = hWndParent;
ti.uId = 0;
ti.hinst = NULL;
ti.lpszText = (char *) string;
GetClientRect (hWndParent, &ti.rect);
// send an addtool message to the tooltip control window
SendMessage (hWndToolTip, TTM_ADDTOOL, 0, (long) &ti);
// the tooltip is delayed at default values. Hopefully this is enough
SendMessage (hWndToolTip, TTM_SETDELAYTIME, TTDT_AUTOMATIC, -1);
// sets a maximum width for the tool tip window (else it won't wrap lines at all, even with \n)
SendMessage (hWndToolTip, TTM_SETMAXTIPWIDTH, 0, 500);
// I don't understand what it does, but without this line the tooltip doesn't show
SendMessage (hWndToolTip, TTM_TRACKACTIVATE, TRUE, (long) &ti);
}
return (hWndToolTip); // finished, return the tooltip handle
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow)
{
CreateToolTip (NULL, NULL, "testing 123\ntesting 456");
_sleep (10000);
return (0);
}
the problem with this one is that it displays my Tooltip in the top left corner and I don't know how to make it display elsewhere ! :'( I would want a tooltip that looks like the ones Windows displays above the systray icons when there's an alert. And another problem is that this tooltip doesn't seem to want to go away until the program exits...
What am I doing wrong ?