65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Windows API 类更改 ToolTip 的前景色和背景色

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2003 年 1 月 30 日

viewsIcon

105421

使用 Windows API 类更改 ToolTip 的前景色和背景色

引言

本文档描述了如何在 C# 中使用 Windows API 类来更改 ToolTip 的前景色和背景色。本文档假定您熟悉 API 类。

描述

在此示例中,我使用了 DLL User32.DLL 和一个内部密封的 API 类来更改 ToolTip 的前景色和背景色。打开 C# Windows 应用程序项目。我使用了以下控件:

Control 名称 文本 描述
文本框 txtText 空字符串 当鼠标悬停时用于显示 ToolTip 的文本框
Button btnFore 前景色 单击按钮时显示颜色对话框
Button btnBack 背景色 单击按钮时显示颜色对话框
Label lblTooltip ToolTip 设置 用于显示所选的前景色和背景色

源代码

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(){}
        
       }
   }    
}

就是这样。使用这些并尝试使用 Windows API 类。

© . All rights reserved.