Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

Building a BallonToolTip provider in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
3 Mar 2004CPOL4 min read 117.2K   2.1K   46  
Shows how to create a ToolTip provider that supports Balloon Tooltips, including issues related to creating extender properties and using the NativeWindow class.
using System;
using System.Runtime.InteropServices;

namespace ToolTipLibrary
{
	/// <summary>
	/// A static class that exposes some Win32 P/Invoke methods and structures.
	/// </summary>
	public sealed class NativeMethods
	{
		public static readonly IntPtr HWND_TOPMOST;
		
		public const int WS_POPUP = -2147483648;

		/// <summary>
		/// Initializes static read-only members
		/// </summary>
		static NativeMethods()
		{
			HWND_TOPMOST = new IntPtr(-1);
		}

		/// <summary>
		/// Private constructor prevents this class from being created.
		/// </summary>
		private NativeMethods()
		{
		}

		/// <summary>
		/// Win32 TOOLINFO structure.
		/// </summary>
		public struct TOOLINFO
		{
			public TOOLINFO(int flags)
			{
				this.cbSize = Marshal.SizeOf(typeof(TOOLINFO));
				this.flags = flags;
				hwnd = IntPtr.Zero;
				uId = IntPtr.Zero;
				rect = new RECT(0, 0, 0, 0);
				hinst = IntPtr.Zero;
				text = "";
				lparam = IntPtr.Zero;
			}

			public int cbSize;
			public int flags;
			public IntPtr hwnd;
			public IntPtr uId;
			public NativeMethods.RECT rect;
			public IntPtr hinst;

			[MarshalAs(UnmanagedType.LPTStr)]
			public string text;

			public IntPtr lparam;
		}

		/// <summary>
		/// Win32 RECT structure for use with the TOOLINFO struct above.
		/// </summary>
		public struct RECT
		{
			public RECT(int left, int top, int right, int bottom)
			{
				this.left = left;
				this.top = top;
				this.right = right;
				this.bottom = bottom;
			}

			public RECT(System.Drawing.Point topLeft, System.Drawing.Size size)
			{
				this.left = topLeft.X;
				this.top = topLeft.Y;
				this.right = topLeft.X + size.Width;
				this.bottom = topLeft.Y + size.Height;
			}

			public int left;
			public int top; 
			public int right;
			public int bottom;
		}

		[DllImport("user32")]
		public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, 
			int x, int y, int cx, int cy, int flags);

		[DllImport("user32")]
		public static extern IntPtr SendMessage(
			IntPtr hWnd,
			int Msg,
			int wParam,
			ref TOOLINFO lParam
			);

		[DllImport("user32")]
		public static extern IntPtr 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I've been a software engineer since 1999. I tend to focus on C# and .NET technologies when possible.

Comments and Discussions