Click here to Skip to main content
15,892,697 members
Articles / Desktop Programming / Windows Forms

Adding "Balloon" Style to ToolTip Provider

Rate me:
Please Sign up or sign in to vote.
4.59/5 (23 votes)
29 Apr 20051 min read 125.3K   2K   57  
How to add "Balloon" style to ToolTip provider
In this post, I present a solution about balloon Tooltip which I think is the easiest and does not influence existing System.Windows.Forms.ToolTip implementation.
namespace ToolTipBalloon
{
	using System;
	using System.Drawing;
	using System.Reflection;
	using System.Runtime.InteropServices;
	using System.Windows.Forms;
	/// <summary>
	/// Summary description for NativeMethods.
	/// </summary>
	public class NativeMethods
	{
		private const long WS_POPUP = 0x80000000;
		private const long TTS_BALLOON = 0x40;
		private const long TTS_NOFADE = 0x20;
		private const int GWL_STYLE = -16;
		private const int WM_USER = 0x0400;
		private const int TTM_SETTIPBKCOLOR = WM_USER + 19;

 		private NativeMethods() {}


		public static void SetBalloonStyle ( ToolTip toolTip )
		{
			NativeWindow window = GetNativeWindow ( toolTip );
 			NativeMethods.SetWindowLong ( window.Handle, GWL_STYLE , WS_POPUP | TTS_BALLOON | TTS_NOFADE );
			
		}
	
		public static void SetBackColor ( ToolTip toolTip, Color color )
		{
			int backColor =  ColorTranslator.ToWin32( color );
			NativeWindow window = GetNativeWindow ( toolTip );
			//setting back color
			SendMessage( window.Handle, TTM_SETTIPBKCOLOR, backColor, 0 );  
		}

		private static NativeWindow GetNativeWindow ( ToolTip toolTip )
		{
			FieldInfo windowField = toolTip.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance );
			NativeWindow window  = (NativeWindow)windowField.GetValue ( toolTip );
			if ( window.Handle == IntPtr.Zero ) throw new ArgumentNullException ( "window handle is not crated." );
			return window;
		}

		[DllImport("user32.dll")]
		private static extern long SetWindowLong(IntPtr hwnd,int index,long val);

		[DllImport("user32.dll")]
		private static extern int SendMessage( IntPtr hwnd, int msg, int wParam, int lParam);
  	}
 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
United States United States
http://www.mommosoft.com

Comments and Discussions