* 重构DrawFontImage函数

This commit is contained in:
Sunny 2023-05-16 22:55:33 +08:00
parent f6f12c51c9
commit a6c3de2ffb
7 changed files with 82 additions and 119 deletions

View File

@ -24,6 +24,7 @@
* 2023-04-08: V3.3.4 DropDownList时
* 2023-05-08: V3.3.6
* 2022-05-12: V3.3.6 DrawString函数
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -410,17 +411,12 @@ namespace Sunny.UI
int symbol = dropSymbol;
if (NeedDrawClearButton)
{
symbol = 261527;
SizeF sf = g.GetFontImageSize(symbol, 24);
g.DrawFontImage(symbol, 24, color, Width - 28 + (12 - sf.Width / 2.0f), (Height - sf.Height) / 2.0f, 1, 1);
g.DrawFontImage(261527, 24, color, new Rectangle(Width - 28, 0, 28, Height), -1, 1);
}
else
{
SizeF sf = g.GetFontImageSize(symbol, 24);
g.DrawFontImage(symbol, 24, color, Width - 28 + (12 - sf.Width / 2.0f), (Height - sf.Height) / 2.0f);
g.DrawFontImage(symbol, 24, color, new Rectangle(Width - 28, 0, 28, Height), 1, 0);
}
//g.DrawLine(RectColor, Width - 1, Radius / 2, Width - 1, Height - Radius);
}
protected bool NeedDrawClearButton;

View File

@ -25,6 +25,7 @@
* 2022-01-02: V3.0.9
* 2022-03-19: V3.1.1
* 2022-05-13: V3.3.6 DrawString函数
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -612,9 +613,9 @@ namespace Sunny.UI
{
//重绘父类
base.OnPaint(e);
SizeF ImageSize = new SizeF(0, 0);
Size ImageSize = new Size(0, 0);
if (Symbol > 0)
ImageSize = e.Graphics.GetFontImageSize(Symbol, SymbolSize);
ImageSize = new Size(SymbolSize, SymbolSize);
if (Image != null)
ImageSize = Image.Size;
@ -636,13 +637,8 @@ namespace Sunny.UI
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f, Height - Padding.Bottom - CircleSize, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new Rectangle(
symbolOffset.X + (int)(Width - CircleSize) / 2,
symbolOffset.Y + Height - Padding.Bottom - CircleSize,
CircleSize,
CircleSize));
e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f - 1, Height - Padding.Bottom - CircleSize - 1, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, new Rectangle(0, Height - Padding.Bottom - CircleSize, Width, CircleSize), symbolOffset.X, symbolOffset.Y);
}
else if (Image != null)
{
@ -664,13 +660,8 @@ namespace Sunny.UI
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, Padding.Left, (Height - CircleSize) / 2.0f, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new Rectangle(
symbolOffset.X + Padding.Left,
symbolOffset.Y + (int)(Height - CircleSize) / 2,
CircleSize,
CircleSize));
e.Graphics.FillEllipse(bcColor, Padding.Left - 1, (Height - CircleSize) / 2.0f - 1, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, new Rectangle(Padding.Left, 0, CircleSize, Height), symbolOffset.X, symbolOffset.Y);
}
else if (Image != null)
{
@ -696,13 +687,8 @@ namespace Sunny.UI
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, Width - Padding.Right - CircleSize, (Height - CircleSize) / 2.0f, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new Rectangle(
symbolOffset.X + Width - Padding.Right - CircleSize,
symbolOffset.Y + (int)(Height - CircleSize) / 2,
CircleSize,
CircleSize));
e.Graphics.FillEllipse(bcColor, Width - Padding.Right - CircleSize - 1, (Height - CircleSize) / 2.0f - 1, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, new Rectangle(Width - Padding.Right - CircleSize, 0, CircleSize, Height), symbolOffset.X, symbolOffset.Y);
}
else if (Image != null)
{
@ -724,13 +710,8 @@ namespace Sunny.UI
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f, Padding.Top, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new Rectangle(
symbolOffset.X + (int)(Width - CircleSize) / 2,
symbolOffset.Y + Padding.Top,
CircleSize,
CircleSize));
e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f - 1, Padding.Top - 1, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, new Rectangle(0, Padding.Top, Width, CircleSize), symbolOffset.X, symbolOffset.Y);
}
else if (Image != null)
{

View File

@ -26,6 +26,7 @@
* 2022-07-28: V3.2.2
* 2023-02-22: V3.3.2
* 2022-05-12: V3.3.6 DrawString函数
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -536,14 +537,13 @@ namespace Sunny.UI
if (ShowItemsArrow && node.Nodes.Count > 0)
{
SizeF imageSize = e.Graphics.GetFontImageSize(61703, 24);
if (i != SelectedIndex)
{
e.Graphics.DrawFontImage(61703, 24, textColor, NodeX + i * NodeSize.Width + rect.Width - 24, rect.Top + (rect.Height - imageSize.Height) / 2);
e.Graphics.DrawFontImage(61703, 24, textColor, new Rectangle(NodeX + i * NodeSize.Width + rect.Width - 24, rect.Top, 24, rect.Height));
}
else
{
e.Graphics.DrawFontImage(NavBarMenu.Visible ? 61702 : 61703, 24, textColor, NodeX + i * NodeSize.Width + rect.Width - 24, rect.Top + (rect.Height - imageSize.Height) / 2);
e.Graphics.DrawFontImage(NavBarMenu.Visible ? 61702 : 61703, 24, textColor, new Rectangle(NodeX + i * NodeSize.Width + rect.Width - 24, rect.Top, 24, rect.Height));
}
}
}

View File

@ -34,6 +34,7 @@
* 2023-02-02: V3.3.1
* 2023-02-10: V3.3.2 /
* 2022-05-12: V3.3.6 DrawString函数
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -730,10 +731,9 @@ namespace Sunny.UI
{
if (MenuHelper.GetSymbol(e.Node) > 0)
{
SizeF fiSize = e.Graphics.GetFontImageSize(MenuHelper.GetSymbol(e.Node), MenuHelper.GetSymbolSize(e.Node));
Color color = e.Node == SelectedNode ? SelectedForeColor : ForeColor;
Point offset = MenuHelper.GetSymbolOffset(e.Node);
e.Graphics.DrawFontImage(MenuHelper.GetSymbol(e.Node), MenuHelper.GetSymbolSize(e.Node), color, imageLeft + (MenuHelper.GetSymbolSize(e.Node) - fiSize.Width) / 2.0f + offset.X, e.Bounds.Y + (e.Bounds.Height - fiSize.Height) / 2 + offset.Y);
e.Graphics.DrawFontImage(MenuHelper.GetSymbol(e.Node), MenuHelper.GetSymbolSize(e.Node), color, new Rectangle(imageLeft, e.Bounds.Y, MenuHelper.GetSymbolSize(e.Node), e.Bounds.Height), offset.X, offset.Y);
}
else
{

View File

@ -21,6 +21,7 @@
* 2022-01-05: V3.0.9
* 2022-03-19: V3.1.1
* 2022-05-15: V3.3.6 DrawString函数
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -37,7 +38,7 @@ namespace Sunny.UI
public class UISymbolButton : UIButton, ISymbol
{
private int _symbolSize = 24;
private int _imageInterval = 2;
private int _imageInterval = 0;
public UISymbolButton()
{
@ -200,7 +201,7 @@ namespace Sunny.UI
}
}
[DefaultValue(2)]
[DefaultValue(0)]
[Description("图片文字间间隔"), Category("SunnyUI")]
public int ImageInterval
{
@ -279,7 +280,7 @@ namespace Sunny.UI
if (IsCircle)
{
int size = Math.Min(Width, Height) - 2 - CircleRectWidth;
g.FillEllipse(GetFillColor(), (Width - size) / 2.0f, (Height - size) / 2.0f, size, size);
g.FillEllipse(GetFillColor(), (Width - size) / 2.0f - 1, (Height - size) / 2.0f - 1, size, size);
}
else
{
@ -313,7 +314,7 @@ namespace Sunny.UI
int size = Math.Min(Width, Height) - 2 - CircleRectWidth;
using var pn = new Pen(GetRectColor(), CircleRectWidth);
g.SetHighQuality();
g.DrawEllipse(pn, (Width - size) / 2.0f, (Height - size) / 2.0f, size, size);
g.DrawEllipse(pn, (Width - size) / 2.0f - 1, (Height - size) / 2.0f - 1, size, size);
g.SetDefaultQuality();
}
else
@ -356,9 +357,9 @@ namespace Sunny.UI
//重绘父类
base.OnPaint(e);
SizeF ImageSize = new SizeF(0, 0);
Size ImageSize = new Size(0, 0);
if (Symbol > 0)
ImageSize = e.Graphics.GetFontImageSize(Symbol, SymbolSize);
ImageSize = new Size(SymbolSize, SymbolSize);
if (Image != null)
ImageSize = Image.Size;
@ -368,7 +369,7 @@ namespace Sunny.UI
if (ImageAlign == ContentAlignment.MiddleCenter && TextAlign == ContentAlignment.MiddleCenter)
{
if (ImageSize.Width.Equals(0))
if (ImageSize.Width == 0)
{
e.Graphics.DrawString(Text, Font, color, ClientRectangle, ContentAlignment.MiddleCenter);
}
@ -378,30 +379,24 @@ namespace Sunny.UI
{
if (Symbol > 0 && Image == null)
{
e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(),
new Rectangle(
(int)(Width - ImageSize.Width) / 2,
Padding.Top + (int)(Height - ImageSize.Height - Padding.Top - Padding.Bottom) / 2,
(int)ImageSize.Width, (int)ImageSize.Height), SymbolOffset.X, SymbolOffset.Y);
e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(), ClientRectangle, SymbolOffset.X, SymbolOffset.Y);
}
if (Image != null)
{
e.Graphics.DrawImage(Image,
(Width - ImageSize.Width) / 2.0f,
Padding.Top + (Height - ImageSize.Height - Padding.Top - Padding.Bottom) / 2.0f,
ImageSize.Width, ImageSize.Height);
e.Graphics.DrawImage(Image, (Width - ImageSize.Width) / 2.0f,
Padding.Top + (Height - ImageSize.Height - Padding.Top - Padding.Bottom) / 2.0f, ImageSize.Width, ImageSize.Height);
}
}
}
else
{
float allWidth = ImageSize.Width + ImageInterval + TextSize.Width;
int allWidth = ImageSize.Width + ImageInterval + TextSize.Width;
if (Symbol > 0 && Image == null)
{
e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(),
new Rectangle((int)(Width - allWidth) / 2, (int)(Height - ImageSize.Height) / 2, (int)ImageSize.Width, (int)ImageSize.Height), SymbolOffset.X, SymbolOffset.Y);
new RectangleF((Width - allWidth) / 2.0f, 0, ImageSize.Width, Height), SymbolOffset.X, SymbolOffset.Y);
}
if (Image != null)
@ -410,7 +405,7 @@ namespace Sunny.UI
ImageSize.Width, ImageSize.Height);
}
e.Graphics.DrawString(Text, Font, color, new Rectangle((int)((Width - allWidth) / 2 + ImageSize.Width + ImageInterval), 0, Width, Height), ContentAlignment.MiddleLeft);
e.Graphics.DrawString(Text, Font, color, new Rectangle((int)((Width - allWidth) / 2.0f + ImageSize.Width + ImageInterval), 0, Width, Height), ContentAlignment.MiddleLeft);
}
}
else

View File

@ -20,6 +20,7 @@
* 2021-12-24: V3.0.9 Dock和AutoSize同时设置的Bug
* 2022-03-19: V3.1.1
* 2022-05-12: V3.3.6 DrawString函数
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -37,7 +38,7 @@ namespace Sunny.UI
public sealed class UISymbolLabel : UIControl, ISymbol
{
private int _symbolSize = 24;
private int _imageInterval = 2;
private int _imageInterval = 0;
private Color symbolColor;
@ -107,7 +108,7 @@ namespace Sunny.UI
}
}
[DefaultValue(2)]
[DefaultValue(0)]
[Description("图标和文字间间隔"), Category("SunnyUI")]
public int ImageInterval
{
@ -240,58 +241,59 @@ namespace Sunny.UI
{
//重绘父类
base.OnPaint(e);
float left = 0;
float top = 0;
SizeF ImageSize = e.Graphics.GetFontImageSize(Symbol, SymbolSize);
Size TextSize = TextRenderer.MeasureText(Text, Font);
int height = Math.Max(SymbolSize, TextSize.Height);
int width = SymbolSize + ImageInterval + TextSize.Width;
if (Dock == DockStyle.None && autoSize)
{
int width = (int)(SymbolSize + ImageInterval * 3 + TextSize.Width);
int height = (int)Math.Max(SymbolSize, TextSize.Height);
if (Width != width) Width = width;
if (Height != height) Height = height;
TextAlign = ContentAlignment.MiddleCenter;
if (Width != width + 4) Width = width + 4;
if (Height != height + 4) Height = height + 4;
}
if (TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.TopRight)
Rectangle rect;
switch (TextAlign)
{
top = Padding.Top;
}
if (TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.MiddleRight)
{
top = Padding.Top + (Height - Padding.Top - Padding.Bottom - ImageSize.Height) / 2.0f;
}
if (TextAlign == ContentAlignment.BottomCenter || TextAlign == ContentAlignment.BottomLeft || TextAlign == ContentAlignment.BottomRight)
{
top = Height - Padding.Bottom - ImageSize.Height;
}
if (TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.BottomCenter)
{
left = Padding.Left + (Width - TextSize.Width - Padding.Left - Padding.Right) / 2.0f;
left = left - ImageInterval - ImageSize.Width;
}
if (TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.BottomLeft)
{
left = ImageInterval;
}
if (TextAlign == ContentAlignment.TopRight || TextAlign == ContentAlignment.MiddleRight || TextAlign == ContentAlignment.BottomRight)
{
left = Width - Padding.Right - TextSize.Width - ImageInterval - ImageSize.Width;
case ContentAlignment.TopLeft:
rect = new Rectangle(Padding.Left, Padding.Top, width, height);
break;
case ContentAlignment.TopCenter:
rect = new Rectangle((Width - width) / 2, Padding.Top, width, height);
break;
case ContentAlignment.TopRight:
rect = new Rectangle(Width - width - Padding.Right, Padding.Top, width, height);
break;
case ContentAlignment.MiddleLeft:
rect = new Rectangle(Padding.Left, (Height - height) / 2, width, height);
break;
case ContentAlignment.MiddleCenter:
rect = new Rectangle((Width - width) / 2, (Height - height) / 2, width, height);
break;
case ContentAlignment.MiddleRight:
rect = new Rectangle(Width - width - Padding.Right, (Height - height) / 2, width, height);
break;
case ContentAlignment.BottomLeft:
rect = new Rectangle(Padding.Left, Height - Padding.Bottom - height, width, height);
break;
case ContentAlignment.BottomCenter:
rect = new Rectangle((Width - width) / 2, Height - Padding.Bottom - height, width, height);
break;
case ContentAlignment.BottomRight:
rect = new Rectangle(Width - width - Padding.Right, Height - Padding.Bottom - height, width, height);
break;
default:
rect = new Rectangle((Width - width) / 2, (Height - height) / 2, width, height);
break;
}
if (Text.IsNullOrEmpty())
e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, ImageInterval + (Width - ImageSize.Width) / 2.0f, (Height - ImageSize.Height) / 2.0f, SymbolOffset.X, SymbolOffset.Y);
e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, (Width - SymbolSize) / 2.0f, (Height - SymbolSize) / 2.0f, SymbolOffset.X, SymbolOffset.Y);
else
e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, left, top, SymbolOffset.X, SymbolOffset.Y);
e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, new Rectangle(rect.Left, rect.Top, SymbolSize, rect.Height), SymbolOffset.X, SymbolOffset.Y);
Rectangle rect = new Rectangle((int)left + SymbolSize, (int)top, Width, SymbolSize);
e.Graphics.DrawString(Text, Font, ForeColor, rect, ContentAlignment.MiddleLeft);
e.Graphics.DrawString(Text, Font, ForeColor, rect, ContentAlignment.MiddleRight);
}
protected override void OnPaintFore(Graphics g, GraphicsPath path)

View File

@ -21,6 +21,7 @@
* https://gitee.com/maikebing
* 2021-06-15: V3.0.4 FontAwesomeV5的字体图标
* 2021-06-15: V3.3.5 FontAwesomeV6的字体图标
* 2022-05-16: V3.3.6 DrawFontImage函数
******************************************************************************/
using System;
@ -106,7 +107,7 @@ namespace Sunny.UI
/// <param name="symbol">字符</param>
/// <param name="symbolSize">大小</param>
/// <returns>字体大小</returns>
public static SizeF GetFontImageSize(this Graphics graphics, int symbol, int symbolSize)
private static SizeF GetFontImageSize(this Graphics graphics, int symbol, int symbolSize)
{
Font font = GetFont(symbol, symbolSize);
if (font == null)
@ -161,10 +162,7 @@ namespace Sunny.UI
{
//字体
Font font = GetFont(symbol, symbolSize);
if (font == null)
{
return;
}
if (font == null) return;
var symbolValue = GetSymbolValue(symbol);
string text = char.ConvertFromUtf32(symbolValue);
@ -308,12 +306,7 @@ namespace Sunny.UI
public Font GetFont(int iconText, int imageSize)
{
int item = GetFontSize(iconText, imageSize);
if (Fonts.ContainsKey(item))
{
return Fonts[GetFontSize(iconText, imageSize)];
}
return null;
return Fonts.ContainsKey(item) ? Fonts[item] : null;
}
/// <summary>
@ -324,11 +317,7 @@ namespace Sunny.UI
/// <returns>字体大小</returns>
public int GetFontSize(int symbol, int imageSize)
{
using (Bitmap bitmap = new Bitmap(48, 48))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
return BinarySearch(graphics, MinFontSize, MaxFontSize, symbol, imageSize);
}
return BinarySearch(GDI.Graphics(), MinFontSize, MaxFontSize, symbol, imageSize);
}
public int BinarySearch(Graphics graphics, int low, int high, int symbol, int imageSize)