Introduction
This article describes how to use the windows API classes in the C# for changing the ToolTip's Fore Color and Back Color. This article assumes that you are familiar with the API classes
Description
In this example I used API classes using DLL User32.DLL and a internal sealed API class to change the Fore Color and Back Color of the ToolTip. Open the C# windows application project. I used the following controls.
Control |
Name |
Text |
Description |
TextBox |
txtText |
empty string |
Text box used for showing the ToolTip when we over the mouse |
Button |
btnFore |
Fore Color |
Color Dialog box is shown when you click the button |
Button |
btnBack |
Back Color |
Color Dialog box is shown when you click the button |
Label |
lblTooltip |
ToolTip Setting |
Used for display the Fore Color and Back Color which were selected |
Source Code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ToolTip_Upload
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txttext;
private System.Windows.Forms.Button btnFore;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.Label lblTooltip;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
this.txttext.MouseHover+= new System.EventHandler(OnMouseEnter);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.txttext = new System.Windows.Forms.TextBox();
this.btnFore = new System.Windows.Forms.Button();
this.btnBack = new System.Windows.Forms.Button();
this.lblTooltip = new System.Windows.Forms.Label();
this.SuspendLayout();
this.txttext.Location = new System.Drawing.Point(64, 56);
this.txttext.Name = "txttext";
this.txttext.Size = new System.Drawing.Size(216, 20);
this.txttext.TabIndex = 0;
this.txttext.Text = "";
this.txtFore.Location = new System.Drawing.Point(128, 88);
this.txtFore.Name = "txtFore";
this.txtFore.Size = new System.Drawing.Size(72, 20);
this.txtFore.TabIndex = 7;
this.txtFore.Text = "";
this.txtBack.Location = new System.Drawing.Point(128, 136);
this.txtBack.Name = "txtBack";
this.txtBack.Size = new System.Drawing.Size(72, 20);
this.txtBack.TabIndex = 8;
this.txtBack.Text = "";
this.lblTooltip.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.lblTooltip.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblTooltip.Location = new System.Drawing.Point(88, 176);
this.lblTooltip.Name = "lblTooltip";
this.lblTooltip.Size = new System.Drawing.Size(128, 16);
this.lblTooltip.TabIndex = 9;
this.lblTooltip.Text = " ToolTip Setting";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.Gainsboro;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblTooltip,
this.btnBack,
this.btnFore,
this.txttext});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnFore_Click(object sender, System.EventArgs e)
{
ColorDialog cd1=new ColorDialog();
if(cd1.ShowDialog()==DialogResult.OK)
{
txtFore.BackColor=cd1.Color;
}
}
private void btnBack_Click(object sender, System.EventArgs e)
{
ColorDialog cd2=new ColorDialog();
if(cd2.ShowDialog()==DialogResult.OK)
{
txtBack.BackColor=cd2.Color;
}
}
public void OnMouseEnter(object sender,EventArgs e )
{
NativeWindow toolTip = new NativeWindow();
System.Windows.Forms.CreateParams cp = new
System.Windows.Forms.CreateParams();
cp.ClassName = API.TOOLTIPS_CLASS;
cp.Style = API.WS_POPUP | API.TTS_NOPREFIX |
API.TTS_ALWAYSTIP;
cp.Parent = this.Handle;
toolTip.CreateHandle(cp);
API.SetWindowPos(toolTip.Handle, API.HWND_TOPMOST,300, 300, 25, 25,
API.SWP_NOACTIVATE | API.SWP_NOSIZE);
API.TOOLINFO ti = new API.TOOLINFO();
ti.cbSize = Marshal.SizeOf(ti);
ti.uFlags = API.TTF_CENTERTIP | API.TTF_TRANSPARENT |
API.TTF_SUBCLASS;
ti.hwnd = this.Handle;
ti.lpszText ="Demo ToolTip with ForeColor and BackColor";
Color Fore=lblTooltip.ForeColor;
Color Back=lblTooltip.BackColor;
int bk=System.Drawing.ColorTranslator.ToWin32(Fore);
int fk=System.Drawing.ColorTranslator.ToWin32(Back);
API.GetClientRect(this.Handle, ref ti.rect);
API.SendMessage(toolTip.Handle, API.TTM_ADDTOOL, 0, ref ti);
API.SendMessage(toolTip.Handle,API.TTM_SETTIPBKCOLOR,bk,ref ti);
API.SendMessage(toolTip.Handle,API.TTM_SETTIPTEXTCOLOR,fk,ref ti);
}
internal sealed class API
{
public const string TOOLTIPS_CLASS = "tooltips_class32";
public const int TTS_ALWAYSTIP = 0x01;
public const int TTS_NOPREFIX = 0x02;
public const int TTS_BALLOON = 0x40;
public const int WS_POPUP = unchecked((int)0x80000000);
public const int TTF_SUBCLASS = 0x0010;
public const int TTF_TRANSPARENT = 0x0100;
public const int TTF_CENTERTIP = 0x0002;
public const int TTF_LEFTTIP=0x0002;
public const int TTM_ADDTOOL = 0x0400 + 50;
public const int TTM_SETTIPBKCOLOR= 0x0400 + 19;
public const int TTM_SETTIPTEXTCOLOR= 0x0400 + 20;
public const int TTM_WINDOWFROMPOINT = 0x0400 + 21;
public const int ICC_WIN95_CLASSES = 0x000000FF;
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0000;
public const int SWP_NOACTIVATE = 0x0010;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOOLINFO
{
public int cbSize;
public int uFlags;
public IntPtr hwnd;
public IntPtr uId;
public RECT rect;
public IntPtr hinst;
[MarshalAs(UnmanagedType.LPTStr)] public string lpszText;
public uint lParam;
}
[DllImport("User32", SetLastError=true)]
public static extern int GetClientRect(
IntPtr hWnd,
ref RECT lpRect);
[DllImport("User32", SetLastError=true)]
public static extern int SendMessage(
IntPtr hWnd,
int Msg,
int wParam,
ref TOOLINFO lParam);
[DllImport("User32", SetLastError=true)]
public static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
int uFlags);
private API(){}
}
}
}
That's it. Use this all and play with the Windows API classes