1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2024-10-17 09:42:45 +02:00
TweetDuck/windows/TweetDuck.Video/Controls/LabelTooltip.cs
2022-11-20 05:25:29 +01:00

50 lines
1.2 KiB
C#

#nullable enable
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace TweetDuck.Video.Controls {
sealed class LabelTooltip : Label {
public LabelTooltip() {
Visible = false;
}
public void AttachTooltip(Control control, bool followCursor, string tooltip) {
AttachTooltip(control, followCursor, _ => tooltip);
}
public void AttachTooltip(Control control, bool followCursor, Func<MouseEventArgs, string?> tooltipFunc) {
control.MouseMove += (_, args) => {
SuspendLayout();
Form? form = control.FindForm();
Debug.Assert(form != null);
string? text = tooltipFunc(args);
if (text == null || control.Parent == null) {
Visible = false;
return;
}
Text = text;
Point loc = form.PointToClient(control.Parent.PointToScreen(new Point(control.Location.X + (followCursor ? args.X : control.Width / 2), 0)));
loc.X = Math.Max(0, Math.Min(form.Width - Width, loc.X - Width / 2));
loc.Y -= Height - Margin.Top + Margin.Bottom;
Location = loc;
ResumeLayout();
Visible = true;
};
control.MouseLeave += control_MouseLeave;
}
private void control_MouseLeave(object? sender, EventArgs e) {
Visible = false;
}
}
}