还是原生ToolTip好

This commit is contained in:
蓝点lilac
2020-11-14 00:44:11 +08:00
parent 2361e5e13c
commit 05c09c1d40
2 changed files with 25 additions and 59 deletions

View File

@@ -71,17 +71,18 @@ namespace BulePointLilac.Controls
picImage.Location = new Point(16, 6).DpiZoom();
lblText.Top = 52.DpiZoom();
lblText.Resize += (sender, e) => lblText.Left = (Width - lblText.Width) / 2;
lblText.SetEnabled(false);
this.Image = image;
this.Text = text;
MyToolTip.SetToolTip(this, text);
ThroughControl(picImage);
ThroughControl(lblText);
}
readonly PictureBox picImage = new PictureBox
{
SizeMode = PictureBoxSizeMode.StretchImage,
Size = new Size(40, 40).DpiZoom(),
BackColor = Color.Transparent,
Enabled = false
BackColor = Color.Transparent
};
readonly Label lblText = new Label
@@ -107,5 +108,17 @@ namespace BulePointLilac.Controls
get => BackColor.A / 255;
set => BackColor = Color.FromArgb((int)(value * 255), Color.White);
}
private void ThroughControl(Control ctr)
{
MouseEventArgs getAbsArgs(MouseEventArgs e)
{
return new MouseEventArgs(e.Button, e.Clicks, ctr.Left + e.X, ctr.Top + e.Y, e.Delta);
}
ctr.MouseDown += (sender, e) => this.OnMouseDown(getAbsArgs(e));
ctr.MouseMove += (sender, e) => this.OnMouseMove(getAbsArgs(e));
ctr.MouseEnter += (sender, e) => this.OnMouseEnter(e);
MyToolTip.SetToolTip(ctr, this.Text);
}
}
}

View File

@@ -1,68 +1,21 @@
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms;
namespace BulePointLilac.Controls
{
public sealed class MyToolTip : Form
public sealed class MyToolTip
{
public static readonly MyToolTip Instance = new MyToolTip();
public static void SetToolTip(Control ctr, string tip)
{
if(string.IsNullOrWhiteSpace(tip)) return;
ctr.MouseEnter += (sender, e) =>
{
Instance.Text = tip;
Instance.Size = Instance.LblTip.Size;
Instance.Location = ctr.PointToScreen(new Point(ctr.Width + 10, (ctr.Height - Instance.Height) / 2));
Instance.Visible = true;
ctr.FindForm().Activate();
};
ctr.MouseLeave += (sender, e) => Instance.Visible = false;
ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(ctr, tip);
ctr.Disposed += (sender, e) => toolTip.Dispose();
}
static MyToolTip()
public static void SetToolTip(ToolStripMenuItem item, string tip)
{
Instance.Opacity = 0;
Instance.Show();
Instance.Visible = false;
Instance.Opacity = 1;
//必须先设置item的Owner
item.Owner.ShowItemToolTips = true;
item.ToolTipText = tip;
}
private MyToolTip()
{
this.TopMost = true;
this.BackColor = Color.FromArgb(235, 245, 255);
this.ForeColor = Color.FromArgb(80, 80, 80);
this.ShowIcon = this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
this.Controls.Add(LblTip);
}
public new string Text
{
get => LblTip.Text;
set => LblTip.Text = value;
}
public new Font Font
{
get => LblTip.Font;
set => LblTip.Font = value;
}
public new Color ForeColor
{
get => LblTip.ForeColor;
set => LblTip.ForeColor = value;
}
public BorderStyle BorderStyle
{
get => LblTip.BorderStyle;
set => LblTip.BorderStyle = value;
}
readonly Label LblTip = new Label
{
AutoSize = true,
Font = SystemFonts.MenuFont,
BorderStyle = BorderStyle.FixedSingle,
};
}
}