Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
2.17/5 (18 votes)
17 Nov 2003 104.6K   14   5
Changing ToolTip's Fore color and Back color using Windows API classes

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.

ControlNameTextDescription
TextBoxtxtTextempty stringText box used for showing the ToolTip when we over the mouse
ButtonbtnForeFore ColorColor Dialog box is shown when you click the button
ButtonbtnBackBack ColorColor Dialog box is shown when you click the button
LabellblTooltipToolTip SettingUsed for display the Fore Color and Back Color which were selected

Source Code

C#
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
        //
        InitializeComponent();
        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) 
                {
                    components.Dispose();
                }
            }
            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();
        this.SuspendLayout();
        // 
        // 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.lblTooltip,
                                  this.btnBack,
                                  this.btnFore,
                                  this.txttext});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
        }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [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;
        }
    }

        

    //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
            System.Windows.Forms.CreateParams();

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

        // create the tooltip window
        toolTip.CreateHandle(cp);
            
        // make tooltip  the top level window
        API.SetWindowPos(toolTip.Handle, API.HWND_TOPMOST,300, 300, 25, 25,
            API.SWP_NOACTIVATE |  API.SWP_NOSIZE);

            // 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 | 
                            API.TTF_SUBCLASS;
            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
    {
        //Declaration 
        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
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
                public int left;
            public int top;
            public int right;
            public int bottom;
        }

            
        //Structure for ToolInfo
        [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(
                //handler
                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

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionabout tool tip Pin
manojsakthikumar30-Jul-08 0:32
manojsakthikumar30-Jul-08 0:32 
GeneralFor Amol Jaysingpure Pin
Mungo Mauro19-Jul-03 0:37
Mungo Mauro19-Jul-03 0:37 
Generalgood post Pin
iluvclubs12-Feb-03 9:14
iluvclubs12-Feb-03 9:14 
GeneralHi, Pin
Chris Richner1-Feb-03 8:30
Chris Richner1-Feb-03 8:30 
GeneralRe: Hi, Pin
Chopper6-Feb-03 3:16
Chopper6-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.