Tooltips without parent (on desktop)
I'm trying to display a balloon tooltip on the screen without specifying a parent window for it.
For this application purpose, my tooltip data and handle is "encapsulated" in my own handle definition (HBUBBLE):
Code:
struct __HBUBBLE {
HWND hwnd;
TOOLINFO ti;
};
typedef __HBUBBLE* HBUBBLE;
My creation code where I'm trying to create it and show is as follows.
Code:
DWORD BubbleWindow_Create(HBUBBLE* pHandle)
{
DWORD rv = 0;
HWND hwnd = NULL;
RECT rcDesktop;
GetClientRect(GetDesktopWindow(), &rcDesktop);
hwnd = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, TTS_ALWAYSTIP | TTS_BALLOON | WS_POPUP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, GetModuleHandle(0), NULL);
rv = (DWORD) hwnd;
if (hwnd)
{
*pHandle = new __HBUBBLE;
memset ( & ((*pHandle)->ti), 0, sizeof(TOOLINFO));
(*pHandle)->ti.cbSize = sizeof(TOOLINFO);
(*pHandle)->ti.hinst = 0;
(*pHandle)->ti.uFlags = TTF_TRACK | TTF_ABSOLUTE | TTF_IDISHWND;
(*pHandle)->ti.hwnd = GetDesktopWindow();
(*pHandle)->ti.uId = (UINT_PTR) GetDesktopWindow();
(*pHandle)->ti.lpszText = L"XXXXX";
CopyRect(& ((*pHandle)->ti.rect), &rcDesktop);
SendMessage(hwnd, TTM_ADDTOOL, 0, (LPARAM) &((*pHandle)->ti));
SendMessage(hwnd, TTM_SETTITLE, TTI_INFO, (LPARAM) L"xxxxxx TITLE xxxxx");
SendMessage(hwnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, -1);
SendMessage(hwnd, TTM_SETMAXTIPWIDTH, 0, 500);
SendMessage(hwnd, TTM_TRACKACTIVATE, TRUE, (LPARAM) &((*pHandle)->ti));
SendMessage(hwnd, TTM_TRACKPOSITION, 0, 0);
//SetWindowPos (hwnd, HWND_TOPMOST, 0, 0, 200, 200, SWP_NOSIZE);
(*pHandle)->hwnd = hwnd;
}
else
dprintf(L"BubbleWindow_Create:WindowCreation failed\n");
return rv;
}
It's possible to do this? Or I must create a parent window for it?
Thank you very much!
pd: (offtopic!) Has "code" tag on the forum C/C++ highlighting capabilities? :D
Re: Tooltips without parent (on desktop)
Usually, a tooltip is not a child window so, it has not a parent.
Please, first take a look at: Which are the differences between child and owned windows?
Of course you can create a tooltip with no owner, but I can not see any advantage.
However, here is an example of a function which can do that:
Code:
DWORD BubbleWindow_Create(HBUBBLE& hBubble, LPCTSTR pszText, LPCTSTR pszTitle,
WORD x, WORD y, WORD nMaxWidth)
{
DWORD dwRet = NO_ERROR;
hBubble = new __HBUBBLE;
hBubble->hwnd = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
NULL, TTS_ALWAYSTIP | TTS_BALLOON | WS_POPUP,
0, 0, 0, 0,
NULL, NULL, GetModuleHandle(0), hBubble);
if(NULL != hBubble->hwnd)
{
::ZeroMemory(&hBubble->ti, sizeof(TOOLINFO));
hBubble->ti.cbSize = sizeof(TOOLINFO);
#if (_WIN32_WINNT >= 0x0501)
hBubble->ti.cbSize -= sizeof(void*);
#endif
hBubble->ti.uFlags = TTF_TRACK | TTF_ABSOLUTE;
hBubble->ti.lpszText = const_cast<LPTSTR>(pszText);
SendMessage(hBubble->hwnd, TTM_ADDTOOL, 0, (LPARAM)&hBubble->ti);
SendMessage(hBubble->hwnd, TTM_SETMAXTIPWIDTH, 0, nMaxWidth);
SendMessage(hBubble->hwnd, TTM_SETTITLE, TTI_INFO, (LPARAM)pszTitle);
SendMessage(hBubble->hwnd, TTM_TRACKACTIVATE, TRUE, (LPARAM)&hBubble->ti);
SendMessage(hBubble->hwnd, TTM_TRACKPOSITION, 0, MAKELPARAM(x, y));
}
else
{
dwRet = ::GetLastError();
}
return dwRet;
}
And here is an example of call:
Code:
HBUBBLE hBubble = NULL;
LPCTSTR pszText = _T("Ala bala portocala, iesi Georghita la portita!");
LPCTSTR pszTitle = _T("TEST TOOLTIP");
DWORD dwRet = BubbleWindow_Create(hBubble, pszText, pszTitle, 150, 100, 200);
if(NO_ERROR != dwRet)
{
// handle error (see: http://www.codeguru.com/forum/showthread.php?t=318721).
return;
}
// ...
Do not forget to previously register tooltip common control as shown in this FAQ: How to register Windows Common Controls in a Win32 application?
Re: Tooltips without parent (on desktop)
Working. Awesome. Thank you very much for your help.
Maybe I forgot this snippet?
Code:
#if (_WIN32_WINNT >= 0x0501)
hBubble->ti.cbSize -= sizeof(void*);
#endif
Again, thanks.:thumb:
Re: Tooltips without parent (on desktop)
Quote:
Originally Posted by
indiocolifa
Working. Awesome. Thank you very much for your help.
Maybe I forgot this snippet?
Code:
#if (_WIN32_WINNT >= 0x0501)
hBubble->ti.cbSize -= sizeof(void*);
#endif
Again, thanks.:thumb:
That made the magic trick. :D
You are welcome!