+ UIToolTip:新增控件,可修改字体

This commit is contained in:
Sunny 2020-07-21 23:38:29 +08:00
parent 667ab5e688
commit c64eb884c1
9 changed files with 241 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.uiButton1 = new Sunny.UI.UIButton();
this.uiButton2 = new Sunny.UI.UIButton();
this.uiButton3 = new Sunny.UI.UIButton();
@ -81,6 +82,7 @@
this.uiImageButton2 = new Sunny.UI.UIImageButton();
this.uiImageButton3 = new Sunny.UI.UIImageButton();
this.uiImageButton4 = new Sunny.UI.UIImageButton();
this.uiToolTip1 = new Sunny.UI.UIToolTip(this.components);
this.PagePanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.uiImageButton1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.uiImageButton2)).BeginInit();
@ -144,7 +146,6 @@
this.PagePanel.Controls.Add(this.uiButton2);
this.PagePanel.Controls.Add(this.uiButton1);
this.PagePanel.Size = new System.Drawing.Size(800, 499);
this.PagePanel.Style = Sunny.UI.UIStyle.Blue;
this.PagePanel.Text = "";
//
// uiButton1
@ -167,6 +168,7 @@
this.uiButton1.StyleCustomMode = true;
this.uiButton1.TabIndex = 0;
this.uiButton1.Text = "White";
this.uiButton1.TipsText = "White";
//
// uiButton2
//
@ -179,6 +181,7 @@
this.uiButton2.StyleCustomMode = true;
this.uiButton2.TabIndex = 1;
this.uiButton2.Text = "Blue";
this.uiButton2.TipsText = "Blue";
//
// uiButton3
//
@ -983,6 +986,13 @@
this.uiImageButton4.Text = "Home";
this.uiImageButton4.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// uiToolTip1
//
this.uiToolTip1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(54)))), ((int)(((byte)(54)))), ((int)(((byte)(54)))));
this.uiToolTip1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(239)))), ((int)(((byte)(239)))), ((int)(((byte)(239)))));
this.uiToolTip1.OwnerDraw = true;
this.uiToolTip1.ToolTipTitle = "ToolTip title";
//
// FButton
//
this.AlwaysOpen = true;
@ -1056,5 +1066,6 @@
private UIImageButton uiImageButton2;
private UIImageButton uiImageButton1;
private UIImageButton uiImageButton4;
private UIToolTip uiToolTip1;
}
}

View File

@ -8,6 +8,10 @@ namespace Sunny.UI.Demo
public FButton()
{
InitializeComponent();
uiToolTip1.SetToolTip(uiButton1,uiButton1.Text);
uiToolTip1.SetToolTip(uiSymbolButton1,uiSymbolButton1.Text,"SunnyUI");
uiToolTip1.SetToolTip(uiSymbolButton2, uiSymbolButton2.Text, "SunnyUI",
uiSymbolButton2.Symbol, 32,UIColor.Red);
}
}
}

View File

@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="uiToolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,219 @@
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
public class UIToolTip : ToolTip
{
private readonly ConcurrentDictionary<Control, ToolTipControl> ToolTipControls =
new ConcurrentDictionary<Control, ToolTipControl>();
public UIToolTip()
{
InitOwnerDraw();
}
public UIToolTip(IContainer cont)
: base(cont)
{
InitOwnerDraw();
}
public new string ToolTipTitle { get; set; } = "ToolTip title";
[DefaultValue(typeof(Font), "微软雅黑, 9pt")]
public Font Font { get; set; } = new Font("微软雅黑", 9);
[DefaultValue(typeof(Font), "微软雅黑, 12pt")]
public Font TitleFont { get; set; } = new Font("微软雅黑", 12);
[DefaultValue(typeof(Color), "239, 239, 239")]
public Color RectColor { get; set; } = UIChartStyles.Dark.ForeColor;
[DefaultValue(true)] public bool AutoSize { get; set; } = true;
[DefaultValue(typeof(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,
Color symbolColor)
{
if (title == null) title = string.Empty;
if (ToolTipControls.ContainsKey(control))
{
ToolTipControls[control].Title = title;
ToolTipControls[control].Description = description;
ToolTipControls[control].Symbol = symbol;
ToolTipControls[control].SymbolSize = symbolSize;
ToolTipControls[control].SymbolColor = symbolColor;
}
else
{
var ctrl = new ToolTipControl()
{
Control = control,
Title = title,
Description = description,
Symbol = symbol,
SymbolSize = symbolSize,
SymbolColor = symbolColor
};
ToolTipControls.TryAdd(control, ctrl);
}
base.SetToolTip(control, description);
}
public void SetToolTip(Control control, string description, string title)
{
if (title == null) title = string.Empty;
if (ToolTipControls.ContainsKey(control))
{
ToolTipControls[control].Title = title;
ToolTipControls[control].Description = description;
}
else
{
var ctrl = new ToolTipControl()
{
Control = control,
Title = title,
Description = description
};
ToolTipControls.TryAdd(control, ctrl);
}
base.SetToolTip(control, description);
}
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);
}
base.SetToolTip(control, description);
}
public void RemoveToolTip(Control control)
{
if (ToolTipControls.ContainsKey(control))
ToolTipControls.TryRemove(control, out _);
}
public new ToolTipControl GetToolTip(Control control)
{
return ToolTipControls.ContainsKey(control) ? ToolTipControls[control] : new ToolTipControl();
}
private void InitOwnerDraw()
{
OwnerDraw = true;
Draw += ToolTipExDraw;
Popup += UIToolTip_Popup;
BackColor = UIChartStyles.Dark.BackColor;
ForeColor = UIChartStyles.Dark.ForeColor;
RectColor = UIChartStyles.Dark.ForeColor;
}
private void UIToolTip_Popup(object sender, PopupEventArgs e)
{
if (ToolTipControls.ContainsKey(e.AssociatedControl))
{
var tooltip = ToolTipControls[e.AssociatedControl];
if (tooltip.Description.IsValid())
{
if (!AutoSize)
{
e.ToolTipSize = Size;
}
else
{
var bmp = new Bitmap(e.ToolTipSize.Width, e.ToolTipSize.Height);
var g = Graphics.FromImage(bmp);
int symbolWidth = tooltip.Symbol > 0 ? tooltip.SymbolSize : 0;
int symbolHeight = tooltip.Symbol > 0 ? tooltip.SymbolSize : 0;
SizeF titleSize = new SizeF(0, 0);
if (tooltip.Title.IsValid())
{
titleSize = g.MeasureString(tooltip.Title, TitleFont);
}
SizeF textSize = g.MeasureString(tooltip.Description, Font);
TitleHeight = (int)Math.Max(symbolHeight, titleSize.Height);
e.ToolTipSize = new Size((int)Math.Max(textSize.Width, symbolWidth + titleSize.Width) + 10, (int)textSize.Height + TitleHeight + 10);
bmp.Dispose();
}
}
}
}
private int TitleHeight;
private void ToolTipExDraw(object sender, DrawToolTipEventArgs e)
{
if (ToolTipControls.ContainsKey(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)
e.Graphics.DrawFontImage(tooltip.Symbol, tooltip.SymbolSize, tooltip.SymbolColor, new Rectangle(5, 5, tooltip.SymbolSize, tooltip.SymbolSize));
if (tooltip.Title.IsValid())
{
SizeF sf = e.Graphics.MeasureString(tooltip.Title, TitleFont);
e.Graphics.DrawString(tooltip.Title,TitleFont,ForeColor, tooltip.Symbol>0?tooltip.SymbolSize+5:5, (TitleHeight-sf.Height)/2);
}
e.Graphics.DrawString(e.ToolTipText, Font, ForeColor, 6, TitleHeight + 6);
}
else
{
e.Graphics.DrawString(e.ToolTipText, e.Font, ForeColor, 0, 0);
}
}
public class ToolTipControl
{
public Control Control { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Symbol { get; set; } = 0;
public int SymbolSize { get; set; } = 32;
public Color SymbolColor { get; set; } = UIChartStyles.Dark.ForeColor;
}
}
}

View File

@ -396,6 +396,9 @@
<Compile Include="Controls\UIColorPicker.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\UIToolTip.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Units\UThunder.cs" />
<Compile Include="Controls\UILine.cs">
<SubType>Component</SubType>