* UIToolTip:修复默认显示

This commit is contained in:
Sunny 2021-12-09 11:13:39 +08:00
parent 70d662b557
commit 560a6f00f6
4 changed files with 75 additions and 67 deletions

Binary file not shown.

Binary file not shown.

View File

@ -44,6 +44,7 @@ namespace Sunny.UI.Demo
// //
this.uiLabel3.AutoSize = true; this.uiLabel3.AutoSize = true;
this.uiLabel3.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiLabel3.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLabel3.IsScaled = false;
this.uiLabel3.Location = new System.Drawing.Point(31, 136); this.uiLabel3.Location = new System.Drawing.Point(31, 136);
this.uiLabel3.Name = "uiLabel3"; this.uiLabel3.Name = "uiLabel3";
this.uiLabel3.Size = new System.Drawing.Size(138, 21); this.uiLabel3.Size = new System.Drawing.Size(138, 21);
@ -55,6 +56,7 @@ namespace Sunny.UI.Demo
// //
this.uiLabel2.AutoSize = true; this.uiLabel2.AutoSize = true;
this.uiLabel2.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiLabel2.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLabel2.IsScaled = false;
this.uiLabel2.Location = new System.Drawing.Point(211, 95); this.uiLabel2.Location = new System.Drawing.Point(211, 95);
this.uiLabel2.Name = "uiLabel2"; this.uiLabel2.Name = "uiLabel2";
this.uiLabel2.Size = new System.Drawing.Size(138, 21); this.uiLabel2.Size = new System.Drawing.Size(138, 21);
@ -66,16 +68,19 @@ namespace Sunny.UI.Demo
// //
this.uiLabel1.AutoSize = true; this.uiLabel1.AutoSize = true;
this.uiLabel1.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiLabel1.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLabel1.IsScaled = false;
this.uiLabel1.Location = new System.Drawing.Point(31, 95); this.uiLabel1.Location = new System.Drawing.Point(31, 95);
this.uiLabel1.Name = "uiLabel1"; this.uiLabel1.Name = "uiLabel1";
this.uiLabel1.Size = new System.Drawing.Size(138, 21); this.uiLabel1.Size = new System.Drawing.Size(138, 21);
this.uiLabel1.TabIndex = 77; this.uiLabel1.TabIndex = 77;
this.uiLabel1.Text = "鼠标移过来看提示"; this.uiLabel1.Text = "鼠标移过来看提示";
this.uiLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.uiLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.uiToolTip1.SetToolTip(this.uiLabel1, "赠人玫瑰手有余香");
// //
// uiLine7 // uiLine7
// //
this.uiLine7.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiLine7.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine7.IsScaled = false;
this.uiLine7.Location = new System.Drawing.Point(30, 55); this.uiLine7.Location = new System.Drawing.Point(30, 55);
this.uiLine7.MinimumSize = new System.Drawing.Size(16, 16); this.uiLine7.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine7.Name = "uiLine7"; this.uiLine7.Name = "uiLine7";
@ -90,6 +95,7 @@ namespace Sunny.UI.Demo
this.uiScrollingText2.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255))))); this.uiScrollingText2.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
this.uiScrollingText2.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.uiScrollingText2.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiScrollingText2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(160)))), ((int)(((byte)(255))))); this.uiScrollingText2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(160)))), ((int)(((byte)(255)))));
this.uiScrollingText2.IsScaled = false;
this.uiScrollingText2.Location = new System.Drawing.Point(381, 129); this.uiScrollingText2.Location = new System.Drawing.Point(381, 129);
this.uiScrollingText2.MinimumSize = new System.Drawing.Size(1, 1); this.uiScrollingText2.MinimumSize = new System.Drawing.Size(1, 1);
this.uiScrollingText2.Name = "uiScrollingText2"; this.uiScrollingText2.Name = "uiScrollingText2";
@ -103,6 +109,7 @@ namespace Sunny.UI.Demo
this.uiScrollingText1.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255))))); this.uiScrollingText1.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
this.uiScrollingText1.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiScrollingText1.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiScrollingText1.ForeColor = System.Drawing.Color.Red; this.uiScrollingText1.ForeColor = System.Drawing.Color.Red;
this.uiScrollingText1.IsScaled = false;
this.uiScrollingText1.Location = new System.Drawing.Point(381, 88); this.uiScrollingText1.Location = new System.Drawing.Point(381, 88);
this.uiScrollingText1.MinimumSize = new System.Drawing.Size(1, 1); this.uiScrollingText1.MinimumSize = new System.Drawing.Size(1, 1);
this.uiScrollingText1.Name = "uiScrollingText1"; this.uiScrollingText1.Name = "uiScrollingText1";
@ -116,6 +123,7 @@ namespace Sunny.UI.Demo
// uiLine6 // uiLine6
// //
this.uiLine6.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiLine6.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine6.IsScaled = false;
this.uiLine6.Location = new System.Drawing.Point(381, 55); this.uiLine6.Location = new System.Drawing.Point(381, 55);
this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16); this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine6.Name = "uiLine6"; this.uiLine6.Name = "uiLine6";
@ -130,6 +138,7 @@ namespace Sunny.UI.Demo
this.uiToolTip1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.uiToolTip1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiToolTip1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(239)))), ((int)(((byte)(239)))), ((int)(((byte)(239))))); this.uiToolTip1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(239)))), ((int)(((byte)(239)))), ((int)(((byte)(239)))));
this.uiToolTip1.OwnerDraw = true; this.uiToolTip1.OwnerDraw = true;
this.uiToolTip1.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(160)))), ((int)(((byte)(255)))));
// //
// FOther // FOther
// //

View File

@ -19,6 +19,7 @@
* 2020-07-21: V2.2.6 * 2020-07-21: V2.2.6
* 2020-07-25: V2.2.6 * 2020-07-25: V2.2.6
* 2021-08-16: V3.0.6 ToolTip接口UITextBox这类的组合控件无法显示ToolTip的问题 * 2021-08-16: V3.0.6 ToolTip接口UITextBox这类的组合控件无法显示ToolTip的问题
* 2021-12-09: V3.0.9
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -48,8 +49,12 @@ namespace Sunny.UI
InitOwnerDraw(); InitOwnerDraw();
} }
[DefaultValue("ToolTip title"), Category("SunnyUI"), Description("标题")] protected override void Dispose(bool disposing)
public new string ToolTipTitle { get; set; } = "ToolTip title"; {
base.Dispose(disposing);
TempTitleFont?.Dispose();
TempFont?.Dispose();
}
[DefaultValue(typeof(Font), "微软雅黑, 9pt"), Description("字体"), Category("SunnyUI")] [DefaultValue(typeof(Font), "微软雅黑, 9pt"), Description("字体"), Category("SunnyUI")]
public Font Font { get; set; } = new Font("微软雅黑", 9); public Font Font { get; set; } = new Font("微软雅黑", 9);
@ -66,7 +71,7 @@ namespace Sunny.UI
[DefaultValue(typeof(Size), "100, 70"), Description("不自动缩放时大小"), Category("SunnyUI")] [DefaultValue(typeof(Size), "100, 70"), Description("不自动缩放时大小"), Category("SunnyUI")]
public Size Size { get; set; } = new Size(100, 70); public Size Size { get; set; } = new Size(100, 70);
public void SetToolTip(Control control, string description, string title, int symbol, int symbolSize, public void SetToolTip(Control control, string caption, string title, int symbol, int symbolSize,
Color symbolColor) Color symbolColor)
{ {
if (title == null) title = string.Empty; if (title == null) title = string.Empty;
@ -74,7 +79,7 @@ namespace Sunny.UI
if (ToolTipControls.ContainsKey(control)) if (ToolTipControls.ContainsKey(control))
{ {
ToolTipControls[control].Title = title; ToolTipControls[control].Title = title;
ToolTipControls[control].Description = description; ToolTipControls[control].ToolTipText = caption;
ToolTipControls[control].Symbol = symbol; ToolTipControls[control].Symbol = symbol;
ToolTipControls[control].SymbolSize = symbolSize; ToolTipControls[control].SymbolSize = symbolSize;
ToolTipControls[control].SymbolColor = symbolColor; ToolTipControls[control].SymbolColor = symbolColor;
@ -85,7 +90,7 @@ namespace Sunny.UI
{ {
Control = control, Control = control,
Title = title, Title = title,
Description = description, ToolTipText = caption,
Symbol = symbol, Symbol = symbol,
SymbolSize = symbolSize, SymbolSize = symbolSize,
SymbolColor = symbolColor SymbolColor = symbolColor
@ -96,20 +101,20 @@ namespace Sunny.UI
if (control is IToolTip toolTip) if (control is IToolTip toolTip)
{ {
SetToolTip(toolTip.ExToolTipControl(), description, title, symbol, symbolSize, symbolColor); SetToolTip(toolTip.ExToolTipControl(), caption, title, symbol, symbolSize, symbolColor);
} }
base.SetToolTip(control, description); base.SetToolTip(control, caption);
} }
public void SetToolTip(Control control, string description, string title) public void SetToolTip(Control control, string caption, string title)
{ {
if (title == null) title = string.Empty; if (title == null) title = string.Empty;
if (ToolTipControls.ContainsKey(control)) if (ToolTipControls.ContainsKey(control))
{ {
ToolTipControls[control].Title = title; ToolTipControls[control].Title = title;
ToolTipControls[control].Description = description; ToolTipControls[control].ToolTipText = caption;
} }
else else
{ {
@ -117,7 +122,7 @@ namespace Sunny.UI
{ {
Control = control, Control = control,
Title = title, Title = title,
Description = description ToolTipText = caption
}; };
ToolTipControls.TryAdd(control, ctrl); ToolTipControls.TryAdd(control, ctrl);
@ -125,37 +130,10 @@ namespace Sunny.UI
if (control is IToolTip toolTip) if (control is IToolTip toolTip)
{ {
SetToolTip(toolTip.ExToolTipControl(), description, title); SetToolTip(toolTip.ExToolTipControl(), caption, title);
} }
base.SetToolTip(control, description); base.SetToolTip(control, caption);
}
public new void SetToolTip(Control control, string description)
{
if (ToolTipControls.ContainsKey(control))
{
ToolTipControls[control].Title = string.Empty;
ToolTipControls[control].Description = description;
}
else
{
var ctrl = new ToolTipControl
{
Control = control,
Title = string.Empty,
Description = description
};
ToolTipControls.TryAdd(control, ctrl);
}
if (control is IToolTip toolTip)
{
SetToolTip(toolTip.ExToolTipControl(), description);
}
base.SetToolTip(control, description);
} }
public void RemoveToolTip(Control control) public void RemoveToolTip(Control control)
@ -171,11 +149,6 @@ namespace Sunny.UI
} }
} }
public new string GetToolTip(Control control)
{
return ToolTipControls.ContainsKey(control) ? ToolTipControls[control].Description : "";
}
private void InitOwnerDraw() private void InitOwnerDraw()
{ {
OwnerDraw = true; OwnerDraw = true;
@ -193,7 +166,7 @@ namespace Sunny.UI
{ {
var tooltip = ToolTipControls[e.AssociatedControl]; var tooltip = ToolTipControls[e.AssociatedControl];
if (tooltip.Description.IsValid()) if (tooltip.ToolTipText.IsValid())
{ {
if (!AutoSize) if (!AutoSize)
{ {
@ -204,16 +177,15 @@ namespace Sunny.UI
var bmp = new Bitmap(e.ToolTipSize.Width, e.ToolTipSize.Height); var bmp = new Bitmap(e.ToolTipSize.Width, e.ToolTipSize.Height);
var g = Graphics.FromImage(bmp); var g = Graphics.FromImage(bmp);
int symbolWidth = tooltip.Symbol > 0 ? tooltip.SymbolSize : 0; int symbolWidth = tooltip.Symbol > 0 ? tooltip.SymbolSize : 0;
Font tmpTitleFont = TitleFont.DPIScaleFont();
Font tmpFont = Font.DPIScaleFont();
SizeF titleSize = new SizeF(0, 0); SizeF titleSize = new SizeF(0, 0);
if (tooltip.Title.IsValid()) if (tooltip.Title.IsValid())
{ {
titleSize = g.MeasureString(tooltip.Title, tmpTitleFont); titleSize = g.MeasureString(tooltip.Title, TempTitleFont);
} }
SizeF textSize = g.MeasureString(tooltip.Description, tmpFont); SizeF textSize = g.MeasureString(tooltip.ToolTipText, TempTitleFont);
int allWidth = (int)Math.Max(textSize.Width, titleSize.Width) + 10; int allWidth = (int)Math.Max(textSize.Width, titleSize.Width) + 10;
if (symbolWidth > 0) allWidth = allWidth + symbolWidth + 5; if (symbolWidth > 0) allWidth = allWidth + symbolWidth + 5;
int allHeight = titleSize.Height > 0 ? int allHeight = titleSize.Height > 0 ?
@ -221,27 +193,57 @@ namespace Sunny.UI
(int)textSize.Height + 10; (int)textSize.Height + 10;
e.ToolTipSize = new Size(allWidth, allHeight); e.ToolTipSize = new Size(allWidth, allHeight);
bmp.Dispose(); bmp.Dispose();
tmpTitleFont.Dispose();
tmpFont.Dispose();
} }
} }
} }
else
{
SizeF sf = GDI.MeasureString(GetToolTip(e.AssociatedControl), TempTitleFont);
e.ToolTipSize = new Size((int)sf.Width + 10, (int)sf.Height + 10);
}
}
Font tmpFont;
private Font TempFont
{
get
{
if (tmpFont == null || !tmpFont.Size.EqualsFloat(Font.DPIScaleFontSize()))
{
tmpFont?.Dispose();
tmpFont = Font.DPIScaleFont();
}
return tmpFont;
}
}
Font tmpTitleFont;
private Font TempTitleFont
{
get
{
if (tmpTitleFont == null || !tmpTitleFont.Size.EqualsFloat(TitleFont.DPIScaleFontSize()))
{
tmpTitleFont?.Dispose();
tmpTitleFont = TitleFont.DPIScaleFont();
}
return tmpTitleFont;
}
} }
private void ToolTipExDraw(object sender, DrawToolTipEventArgs e) private void ToolTipExDraw(object sender, DrawToolTipEventArgs e)
{ {
Font tmpTitleFont = TitleFont.DPIScaleFont(); var bounds = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width - 1, e.Bounds.Height - 1);
Font tmpFont = Font.DPIScaleFont(); e.Graphics.FillRectangle(BackColor, bounds);
e.Graphics.DrawRectangle(RectColor, bounds);
if (ToolTipControls.ContainsKey(e.AssociatedControl)) if (ToolTipControls.ContainsKey(e.AssociatedControl))
{ {
var tooltip = ToolTipControls[e.AssociatedControl]; var tooltip = ToolTipControls[e.AssociatedControl];
var bounds = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width - 1, e.Bounds.Height - 1);
e.Graphics.FillRectangle(BackColor, bounds);
e.Graphics.DrawRectangle(RectColor, bounds);
if (tooltip.Symbol > 0) if (tooltip.Symbol > 0)
{ {
e.Graphics.DrawFontImage(tooltip.Symbol, tooltip.SymbolSize, tooltip.SymbolColor, new Rectangle(5, 5, tooltip.SymbolSize, tooltip.SymbolSize)); e.Graphics.DrawFontImage(tooltip.Symbol, tooltip.SymbolSize, tooltip.SymbolColor, new Rectangle(5, 5, tooltip.SymbolSize, tooltip.SymbolSize));
@ -253,10 +255,10 @@ namespace Sunny.UI
{ {
if (tooltip.Title.IsValid()) if (tooltip.Title.IsValid())
{ {
titleSize = e.Graphics.MeasureString(tooltip.Title, tmpTitleFont); titleSize = e.Graphics.MeasureString(tooltip.Title, TempTitleFont);
} }
e.Graphics.DrawString(tooltip.Title, tmpTitleFont, ForeColor, e.Graphics.DrawString(tooltip.Title, TempTitleFont, ForeColor,
tooltip.Symbol > 0 ? tooltip.SymbolSize + 5 : 5, 5); tooltip.Symbol > 0 ? tooltip.SymbolSize + 5 : 5, 5);
} }
@ -267,24 +269,21 @@ namespace Sunny.UI
e.Bounds.Width - 5, 5 + titleSize.Height + 3); e.Bounds.Width - 5, 5 + titleSize.Height + 3);
} }
e.Graphics.DrawString(e.ToolTipText, tmpFont, ForeColor, e.Graphics.DrawString(e.ToolTipText, TempTitleFont, ForeColor,
tooltip.Symbol > 0 ? tooltip.SymbolSize + 5 : 5, tooltip.Symbol > 0 ? tooltip.SymbolSize + 5 : 5,
titleSize.Height > 0 ? 10 + titleSize.Height : 5); titleSize.Height > 0 ? 10 + titleSize.Height : 5);
} }
else else
{ {
e.Graphics.DrawString(e.ToolTipText, tmpFont, ForeColor, 0, 0); e.Graphics.DrawString(e.ToolTipText, TempTitleFont, ForeColor, 5, 5);
} }
tmpTitleFont.Dispose();
tmpFont.Dispose();
} }
public class ToolTipControl : ISymbol public class ToolTipControl : ISymbol
{ {
public Control Control { get; set; } public Control Control { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Description { get; set; } public string ToolTipText { get; set; }
public int Symbol { get; set; } public int Symbol { get; set; }