View Single Post
Re: [Windows API] Tooltips w/o MFC
Old
  (#5)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default 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."
  
Reply With Quote