【问题标题】:How to create a Tooltip anywhere on the screen?如何在屏幕上的任意位置创建工具提示?
【发布时间】:2012-11-12 05:43:02
【问题描述】:

如何在屏幕上的任何位置创建一个像这样的工具提示(在 AutoIt 中创建)? 我找了一个小时,真的什么也没找到。 它是一个普通的tooltip,就像trayicons的tooltips一样,可以放在任何地方。

问候

【问题讨论】:

  • MessageBox.Show 阻止了我的脚本,像这样的工具提示非常适合调试和/或记录内容。
  • 您已将其标记为 C# - 但它是 Windows 应用程序吗? / ASP .NET / WPF 还是什么?
  • 有什么区别?我只需要在屏幕上的任意位置创建一个工具提示,无需 GUI/窗口/用户交互。

标签: c# tooltip balloon


【解决方案1】:

为什么它是 Windows 窗体、ASP .NET 等很重要?因为它可能会影响你的选择。

如果它是一个 Windows 窗体应用程序,您可以创建自己的继承自 Windows.Forms.Form 的类,设置一些属性然后使用它。

public class MyTooltip : Form
{
    public int Duration { get; set; }

    public MyTooltip(int x, int y, int width, int height, string message, int duration)
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.ShowInTaskbar = false;
        this.Width = width;
        this.Height = height;
        this.Duration = duration;
        this.Location = new Point(x, y);
        this.StartPosition = FormStartPosition.Manual;
        this.BackColor = Color.LightYellow;

        Label label = new Label();
        label.Text = message;
        label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        label.Dock = DockStyle.Fill;

        this.Padding = new Padding(5);
        this.Controls.Add(label);
    }

    protected override void OnShown(System.EventArgs e)
    {
        base.OnShown(e);

        TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();

        Task.Factory.StartNew(() => CloseAfter(this.Duration, ui));
    }

    private void CloseAfter(int duration, TaskScheduler ui)
    {
        Thread.Sleep(duration * 1000);

        Form form = this;

        Task.Factory.StartNew(
            () => form.Close(),
            CancellationToken.None,
            TaskCreationOptions.None,
            ui);
    }
}

你可以这样使用它:

    private void showButton_Click(object sender, EventArgs e)
    {
        var tooltip = new MyTooltip(
            (int)this.xBox.Value,
            (int)this.yBox.Value,
            50,
            50,
            "This is my custom tooltip message.",
            (int)durationBox.Value);

        tooltip.Show();
    }

如果您想要一个持续时间,您可以减少表单的不透明度,直到它消失,然后关闭它以获得更好的效果,而不是关闭它。

您还可以使用透明度颜色并使用背景图像等来设置工具提示。

编辑:

这里是 CloseAfter 方法如何淡出工具提示表单的快速演示。

private void CloseAfter(int duration, TaskScheduler ui)
{
    Thread.Sleep(duration * 1000);

    Form form = this;

    for (double i = 0.95; i > 0; i -= 0.05)
    {
        Task.Factory.StartNew(
            () => form.Opacity = i,
            CancellationToken.None,
            TaskCreationOptions.None,
            ui);

        Thread.Sleep(50);
    }

    Task.Factory.StartNew(
        () => form.Close(),
        CancellationToken.None,
        TaskCreationOptions.None,
        ui);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多