using System;
using System.Windows.Forms;
namespace TooltipExample
{
public class TooltipForm : Form
{
private ToolTip toolTip1;
private Button button1;
public TooltipForm()
{
// 初始化按钮
button1 = new Button();
button1.Text = "Hover over me!";
button1.Location = new System.Drawing.Point(50, 50);
this.Controls.Add(button1);
// 初始化 ToolTip
toolTip1 = new ToolTip();
toolTip1.SetToolTip(button1, "This is a tooltip message.");
// 设置 ToolTip 的显示时间
toolTip1.AutoPopDelay = 5000; // 显示5秒后自动消失
toolTip1.InitialDelay = 1000; // 鼠标悬停1秒后显示
toolTip1.ReshowDelay = 500; // 再次显示的延迟时间
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TooltipForm());
}
}
}
TooltipForm 类,继承自 Form。在构造函数中,初始化了一个按钮 button1 并将其添加到窗体上。ToolTip 类创建了一个工具提示控件 toolTip1,并调用 SetToolTip 方法为按钮设置了提示文本。AutoPopDelay、InitialDelay 和 ReshowDelay 属性来控制工具提示的显示时间和行为。Main 方法中启动应用程序并显示窗体。这个示例展示了如何在 C# WinForms 应用程序中使用 ToolTip 控件为按钮添加提示信息。
上一篇:c# serializable
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站