显示禁用按钮的工具提示,该按钮位于 Windows 窗体中的组框内

本文关键字:按钮 窗体 Windows 工具提示 显示 | 更新日期: 2023-09-27 18:34:35

>我有一个组框设置为可见 true。里面有 3 个按钮 .btn1 可见真.btn 2 ,btn 3 可见假..想要在确切位置显示此 3 按钮的工具提示?

我现在在做什么

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private ToolTip toolTip = new ToolTip();
        private bool isShown = false;
        ToolTip toolTip1 = new ToolTip();
        bool IsShown = false;
        public Form1()
        {
            InitializeComponent();
            button1.Enabled = false;
            toolTip1.InitialDelay = 0;
        }
        private void Form1_MouseMove_1(object sender, MouseEventArgs e)
        {
            //if (button1 == this.GetChildAtPoint(e.Location))
            //{
            //    if (!isShown)
            //    {
            //        toolTip.Show("MyToolTip", this, e.Location);
            //        isShown = true;
            //    }
            //}
            //else
            //{
            //    toolTip.Hide(textBox1);
            //    isShown = false;
            //}
            Control ctrl = this.GetChildAtPoint(e.Location);
            if (ctrl != null)
            {
                if (ctrl == this.button1 && !IsShown)
                {
                    string tipstring = this.toolTip1.GetToolTip(this.button1);
                    this.toolTip1.Show(tipstring, this.button1, this.button1.Width / 2, this.button1.Height / 2);
                    IsShown = true;
                }
            }
            else
            {
                this.toolTip1.Hide(this.button1);
                IsShown = false;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
              // Set up the delays for the ToolTip.
            toolTip1.AutoPopDelay = 5000;
            toolTip1.InitialDelay = 1000;
            toolTip1.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            toolTip1.ShowAlways = true;
            // Set up the ToolTip text for the Button and Checkbox.
            toolTip1.SetToolTip(this.button1, "My button1");
        }
    }
}

我试过用 1 个按钮失败了...需要帮助

我什至尝试过

http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx

但它不适用于禁用的控件

显示禁用按钮的工具提示,该按钮位于 Windows 窗体中的组框内

由于禁用按钮上的鼠标悬停事件不会触发,因此您可以在窗体上设置 MouseMove 事件并检查按钮位置,如下所示:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.X >= button.Location.X && e.X < button.Location.X + button.Width
        && e.Y >= button.Location.Y && e.Y <= button.Location.Y + button.Height)
    {
        if (!isShown)
        {
            tt.Show("MyToolTip", button, button.Width / 2, button.Height / 2);
            isShown = true;
        }
    }
    else
    {
        tt.Hide(button);
        isShown = false;
    }
}

编辑(与 GroupBox 一起使用(:

在你的*中。设计器.cs类你应该添加这个委托(使用上面提到的相同回调(:

this.groupBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

试试这个我认为它会起作用。