Click here to Skip to main content
12,556,876 members (47,626 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked

Changing ToolTip's Fore color and Back color using Windows API classes

, 17 Nov 2003
Rate this:
Please Sign up or sign in to vote.
Changing ToolTip's Fore color and Back color using Windows API classes


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


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
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    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;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        // Required for Windows Form Designer support
        this.txttext.MouseHover+= new System.EventHandler(OnMouseEnter);
        // TODO: Add any constructor code after InitializeComponent call

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
            if( disposing )
                if (components != null) 
            base.Dispose( disposing );

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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();
        // txttext
        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 = "";
        // txtFore
        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 = "";
        // txtBack
        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 = "";
        // lblTooltip
    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";
    // Form1
    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.Name = "Form1";
    this.Text = "Form1";

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main() 
        Application.Run(new Form1());

    private void btnFore_Click(object sender, System.EventArgs e)
        ColorDialog cd1=new ColorDialog();

    private void btnBack_Click(object sender, System.EventArgs e)
        ColorDialog cd2=new ColorDialog();


    //Event Handler for Displaying ToolTip when the mouse is over the control
    public void  OnMouseEnter(object sender,EventArgs e ) 
        //Check wheather ToolTip option is Enabeleb or Not
        NativeWindow toolTip = new NativeWindow();
        System.Windows.Forms.CreateParams cp = new

        // fill  created param
        cp.ClassName = API.TOOLTIPS_CLASS;
        cp.Style = API.WS_POPUP | API.TTS_NOPREFIX |
        cp.Parent = this.Handle;

        // create the tooltip window
        // make tooltip  the top level window
        API.SetWindowPos(toolTip.Handle, API.HWND_TOPMOST,300, 300, 25, 25,

            // create and fill in the tool tip info
            API.TOOLINFO ti = new API.TOOLINFO();
            ti.cbSize = Marshal.SizeOf(ti);
            ti.uFlags = API.TTF_CENTERTIP | API.TTF_TRANSPARENT | 
            ti.hwnd = this.Handle;
            ti.lpszText ="Demo ToolTip with ForeColor and BackColor";
            //Convert the color 
            Color Fore=lblTooltip.ForeColor;
             Color Back=lblTooltip.BackColor;
            int bk=System.Drawing.ColorTranslator.ToWin32(Fore);
            int fk=System.Drawing.ColorTranslator.ToWin32(Back);
            //get the Client Rectangle
             API.GetClientRect(this.Handle, ref ti.rect);

            // add the tooltip,Send the message
            API.SendMessage(toolTip.Handle, API.TTM_ADDTOOL, 0, ref ti);
        //send message for setting backColor
        API.SendMessage(toolTip.Handle,API.TTM_SETTIPBKCOLOR,bk,ref ti);  
        //send message for setting ForeColor
        API.SendMessage(toolTip.Handle,API.TTM_SETTIPTEXTCOLOR,fk,ref ti);
    ///Windows API for the ToolTip
    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);

        //Structure for rectangle
        public struct RECT
                public int left;
            public int top;
            public int right;
            public int bottom;

        //Structure for ToolInfo
        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);

        //For message sending
        [DllImport("User32", SetLastError=true)]
        public static extern int SendMessage(
            IntPtr hWnd,
            int Msg,
            int wParam,
            ref TOOLINFO lParam);
        //for setting windows Position
        [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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Amol Jaysingpure
Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Questionabout tool tip Pin
manojsakthikumar30-Jul-08 0:32
membermanojsakthikumar30-Jul-08 0:32 
GeneralFor Amol Jaysingpure Pin
Mungo Mauro19-Jul-03 0:37
memberMungo Mauro19-Jul-03 0:37 
Generalgood post Pin
iluvclubs12-Feb-03 9:14
memberiluvclubs12-Feb-03 9:14 
GeneralHi, Pin
Jerry Maguire1-Feb-03 8:30
memberJerry Maguire1-Feb-03 8:30 
GeneralRe: Hi, Pin
Chopper6-Feb-03 3:16
memberChopper6-Feb-03 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161026.1 | Last Updated 18 Nov 2003
Article Copyright 2003 by Amol Jaysingpure
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid