Question

How can I create a tooltip like this one (created in AutoIt) anywhere on the screen? I am looking for an hour and found nothing really. It's a normal tooltip like trayicons' tooltips, just can be placed anywhere.

Regards

AutoIt Tooltip()

Was it helpful?

Solution

Why does it matter whether it is Windows Forms, ASP .NET, etc? Because it probably affects your choices.

If it is a Windows Forms application you could create your own class that inherits from Windows.Forms.Form, set some properties and then use that.

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);
    }
}

You can use it like this:

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

Instead of closing you could reduce the form's opacity until it disappears then close it for a nicer effect, assuming you want a duration at all.

You could also play around with the transparency colour and use a background image, etc to have shaped tooltips.

EDIT:

Here is a quick demonstration of how the CloseAfter method could fade out the tooltip form.

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top