|
General Programming Help others and get yourself helped here!
|
|
Roi de France
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
|
[Windows API] Tooltips w/o MFC -
10-06-2004
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 ?
RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
|
|
|
|
|
Super Moderator
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
|
Re: [Windows API] Tooltips w/o MFC -
10-06-2004
In your CreateWindow() call you have...
Code:
0,0, // left/top positions
Replace 0,0 with the client window's X,Y position.
botman
Last edited by botman; 10-06-2004 at 23:22..
|
|
|
|
|
Roi de France
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
|
Re: [Windows API] Tooltips w/o MFC -
11-06-2004
That doesn't work, botman... I already tried many combinations with left/top and width/height coordinates. The tooltip always show in the top left corner of the screen.
RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
|
|
|
|
|
Member
Status: Offline
Posts: 270
Join Date: Jun 2004
Location: Nottingham, UK
|
Re: [Windows API] Tooltips w/o MFC -
11-06-2004
just a little idea, have you tried moving the window after you've created it?
- using code, rather than the mouse or any such thing
|
|
|
|
|
Roi de France
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
|
Re: [Windows API] Tooltips w/o MFC -
11-06-2004
Thanks for the tip, that worked
Code:
// these #define's apply to balloon tooltips and are not present in the MSVC 6 headers
#define TTS_BALLOON 0x40
#define TTM_SETTITLE (WM_USER + 32)
#define TTI_NONE 0
#define TTI_INFO 1
#define TTI_WARNING 2
#define TTI_ERROR 3
HWND CreateToolTip (HWND hWndParent, long icon_type, const char *title, const char *text, char is_balloon)
{
// this function creates a tooltip
static HWND hWndToolTip = NULL;
INITCOMMONCONTROLSEX iccex;
TOOLINFO ti;
long window_style;
int screen_width;
int screen_height;
// 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
memset (&iccex, 0, sizeof (iccex));
iccex.dwSize = sizeof (iccex);
iccex.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx (&iccex);
// prepare the tooltip window style
window_style = WS_POPUP // popup window
| TTS_NOPREFIX // prevents ampersand stripping
| TTS_ALWAYSTIP; // allows tip from inactive window
if (is_balloon)
window_style |= TTS_BALLOON; // add the balloon flag if we want a balloon tooltip
// create a tooltip window
hWndToolTip = CreateWindow (TOOLTIPS_CLASS, // common dialog style tooltip
NULL, // title bar
window_style, // window style flags
CW_USEDEFAULT, CW_USEDEFAULT, // left/top positions
CW_USEDEFAULT, CW_USEDEFAULT, // width/height of window
NULL, // the parent window
NULL, // no menu
NULL, // window handle
NULL); // no extra parameters
if (hWndToolTip != NULL)
{
memset (&ti, 0, sizeof (ti)); // mama says: reset your structure before touching it.
ti.cbSize = sizeof (ti);
ti.hwnd = hWndParent;
ti.lpszText = (char *) text;
GetClientRect (hWndParent, &ti.rect);
// send an addtool message to the tooltip control window
SendMessage (hWndToolTip, TTM_ADDTOOL, 0, (long) &ti);
// set the icon and the title for the tooltip window
SendMessage (hWndToolTip, TTM_SETTITLE, icon_type, (long) title);
// delay the tooltip with the default delay values
SendMessage (hWndToolTip, TTM_SETDELAYTIME, TTDT_AUTOMATIC, -1);
// sets a maximum width for the tool tip window (else it won't wrap lines at all)
SendMessage (hWndToolTip, TTM_SETMAXTIPWIDTH, 0, 500);
// I dunno why, but I must call this to display the window (it wont work without)
SendMessage (hWndToolTip, TTM_TRACKACTIVATE, TRUE, (long) &ti);
// is this tooltip NOT associated with another window ?
if (hWndParent == NULL)
{
// then move it somewhere instead of letting it into that top left corner
// get the screen size
GetClientRect (GetDesktopWindow (), &ti.rect);
screen_width = ti.rect.right;
screen_height = ti.rect.bottom;
// now get the popup size and place the popup at the center of the screen
GetClientRect (hWndToolTip, &ti.rect);
SetWindowPos (hWndToolTip, HWND_TOPMOST,
screen_width / 2 - ti.rect.right / 2,
screen_height / 2 - ti.rect.bottom / 2,
0, 0, SWP_NOSIZE);
}
}
return (hWndToolTip); // finished, return the tooltip handle
}
I'm still however wondering why I can only set the window position AFTER it has shown up... and also why on earth does it need that TTM_TRACKACTIVATE message to show up ? ???
RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
|
|
|
|
|
Member
Status: Offline
Posts: 270
Join Date: Jun 2004
Location: Nottingham, UK
|
Re: [Windows API] Tooltips w/o MFC -
11-06-2004
ahh, have to pass that one. I'm a complete n00b when it comes to anything more advanced than QBASIC...
|
|
|
|
|
Super Moderator
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
|
Re: [Windows API] Tooltips w/o MFC -
12-06-2004
TTM_TRACKACTIVATE allows the tooltip to track the mouse position and disply the tooltip window when the mouse is over the item.
See the MSDN webpage about it...
http://msdn.microsoft.com/library/de...ckactivate.asp
botman
|
|
|
|
|
Roi de France
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
|
Re: [Windows API] Tooltips w/o MFC -
12-06-2004
I know that botman.. looking it up in the MSDN was the first thing I did, but still it doesn't tell me WHY I need this message... since all I want is a topmost-level tooltip not child of any other window (but the desktop window) that I could pop up at an arbitrary location.
I prolly *hate* when I use a piece of code that I don't understand.
Thanks for taking the time to search it anyway
RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Powered by vBulletin® Version 3.8.2 Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
|
|