* 重构MeasureString

This commit is contained in:
Sunny 2023-05-16 15:01:46 +08:00
parent 169b079f32
commit f6f12c51c9
18 changed files with 48 additions and 54 deletions

View File

@ -175,7 +175,7 @@ namespace Sunny.UI
{ {
foreach (var item in Items) foreach (var item in Items)
{ {
SizeF sf = g.MeasureString(item.ToString(), Font); Size sf = TextRenderer.MeasureText(item.ToString(), Font);
width = Math.Max(width, sf.Width); width = Math.Max(width, sf.Width);
} }

View File

@ -319,8 +319,8 @@ namespace Sunny.UI
if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText)) if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText))
{ {
e.Graphics.SetHighQuality(); e.Graphics.SetHighQuality();
SizeF sf = e.Graphics.MeasureString(TipsText, TempFont); Size sf = TextRenderer.MeasureText(TipsText, TempFont);
int sfMax = (int)Math.Max(sf.Width, sf.Height) + 1; int sfMax = Math.Max(sf.Width, sf.Height);
int x = Width - 1 - 2 - sfMax; int x = Width - 1 - 2 - sfMax;
int y = 1 + 1; int y = 1 + 1;
e.Graphics.FillEllipse(TipsColor, x - 1, y, sfMax, sfMax); e.Graphics.FillEllipse(TipsColor, x - 1, y, sfMax, sfMax);

View File

@ -24,6 +24,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
@ -741,8 +742,8 @@ namespace Sunny.UI
if (isDate) if (isDate)
{ {
SizeF sf = e.Graphics.MeasureString("00", Font); SizeF sf = TextRenderer.MeasureText("00", Font);
e.Graphics.DrawRectangle(PrimaryColor, new RectangleF(left + (width - sf.Width) / 2 - 2, top + 30 + (height - sf.Height) / 2 - 1, sf.Width + 3, sf.Height)); e.Graphics.DrawRectangle(PrimaryColor, new RectangleF(left + (width - sf.Width) / 2 - 2, top + 30 + (height - sf.Height) / 2 - 1, sf.Width + 3, sf.Height + 2));
} }
} }
} }

View File

@ -619,8 +619,6 @@ namespace Sunny.UI
ImageSize = Image.Size; ImageSize = Image.Size;
Color color = GetForeColor(); Color color = GetForeColor();
SizeF sf = e.Graphics.MeasureString(Text, Font);
switch (textImageRelation) switch (textImageRelation)
{ {
case TextImageRelation.TextAboveImage: case TextImageRelation.TextAboveImage:
@ -748,8 +746,8 @@ namespace Sunny.UI
if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText)) if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText))
{ {
e.Graphics.SetHighQuality(); e.Graphics.SetHighQuality();
sf = e.Graphics.MeasureString(TipsText, TempFont); Size sf = TextRenderer.MeasureText(TipsText, TempFont);
int sfMax = (int)Math.Max(sf.Width, sf.Height) + 1; int sfMax = Math.Max(sf.Width, sf.Height);
int x = Width - 1 - 2 - sfMax; int x = Width - 1 - 2 - sfMax;
int y = 1 + 1; int y = 1 + 1;
e.Graphics.FillEllipse(TipsColor, x - 1, y, sfMax, sfMax); e.Graphics.FillEllipse(TipsColor, x - 1, y, sfMax, sfMax);

View File

@ -776,7 +776,7 @@ namespace Sunny.UI
newTransform.Translate(e.Bounds.X, e.Bounds.Y); newTransform.Translate(e.Bounds.X, e.Bounds.Y);
g.Transform = newTransform; g.Transform = newTransform;
ImageListItem item = (ImageListItem)Items[e.Index]; ImageListItem item = (ImageListItem)Items[e.Index];
SizeF sf = g.MeasureString("ImageListBox", Font); Size sf = TextRenderer.MeasureText("ImageListBox", Font);
int thumbnailSize = ShowDescription ? ((int)(ItemHeight - ImageInterval - sf.Height)) : (ItemHeight - ImageInterval * 2); int thumbnailSize = ShowDescription ? ((int)(ItemHeight - ImageInterval - sf.Height)) : (ItemHeight - ImageInterval * 2);
if (item.Image != null) if (item.Image != null)
@ -794,7 +794,7 @@ namespace Sunny.UI
if (ShowDescription && !string.IsNullOrEmpty(item.Description)) if (ShowDescription && !string.IsNullOrEmpty(item.Description))
{ {
g.DrawString(item.Description, e.Font, foreColor, new Rectangle(ImageInterval, e.Bounds.Y + thumbnailSize + ImageInterval, e.Bounds.Width, e.Bounds.Height), ContentAlignment.TopLeft); g.DrawString(item.Description, e.Font, foreColor, new Rectangle(ImageInterval, e.Bounds.Y + thumbnailSize, e.Bounds.Width, e.Bounds.Height), ContentAlignment.TopLeft);
} }
g.Transform = oldTransform; g.Transform = oldTransform;

View File

@ -857,15 +857,14 @@ namespace Sunny.UI
{ {
// Use font family = _scaleFont, but size = automatic // Use font family = _scaleFont, but size = automatic
fSize = (float)(6F * drawRatio); fSize = (float)(6F * drawRatio);
if (fSize < 6) if (fSize < 6) fSize = 6;
fSize = 6; strsize = TextRenderer.MeasureText(str, new Font(_scaleFont.FontFamily, fSize));
strsize = Gr.MeasureString(str, new Font(_scaleFont.FontFamily, fSize));
} }
else else
{ {
// Use font family = _scaleFont, but size = fixed // Use font family = _scaleFont, but size = fixed
fSize = _scaleFont.Size; fSize = _scaleFont.Size;
strsize = Gr.MeasureString(str, _scaleFont); strsize = TextRenderer.MeasureText(str, _scaleFont);
} }
int strw = (int)strsize.Width; int strw = (int)strsize.Width;
@ -954,7 +953,7 @@ namespace Sunny.UI
} }
font = new Font(_scaleFont.FontFamily, fSize); font = new Font(_scaleFont.FontFamily, fSize);
strsize = Gr.MeasureString(str, font); strsize = TextRenderer.MeasureText(str, font);
int strw = (int)strsize.Width; int strw = (int)strsize.Width;
int strh = (int)strsize.Height; int strh = (int)strsize.Height;
@ -980,9 +979,9 @@ namespace Sunny.UI
// If autosize // If autosize
if (_scaleFontAutoSize) if (_scaleFontAutoSize)
strsize = Gr.MeasureString(str, new Font(_scaleFont.FontFamily, fSize)); strsize = TextRenderer.MeasureText(str, new Font(_scaleFont.FontFamily, fSize));
else else
strsize = Gr.MeasureString(str, new Font(_scaleFont.FontFamily, _scaleFont.Size)); strsize = TextRenderer.MeasureText(str, new Font(_scaleFont.FontFamily, _scaleFont.Size));
if (_drawDivInside) if (_drawDivInside)
{ {
@ -1094,7 +1093,7 @@ namespace Sunny.UI
font = new Font(_scaleFont.FontFamily, _scaleFont.Size); font = new Font(_scaleFont.FontFamily, _scaleFont.Size);
} }
SizeF strsize = Gr.MeasureString(str, font); SizeF strsize = TextRenderer.MeasureText(str, font);
// Graduations outside // Graduations outside
gradLength = 4 * drawRatio; gradLength = 4 * drawRatio;
@ -1233,7 +1232,7 @@ namespace Sunny.UI
if (!_scaleFontAutoSize) if (!_scaleFontAutoSize)
{ {
fSize = _scaleFont.Size; fSize = _scaleFont.Size;
strsize = Gr.MeasureString(str, _scaleFont); strsize = TextRenderer.MeasureText(str, _scaleFont);
} }
else else
{ {
@ -1242,7 +1241,7 @@ namespace Sunny.UI
fSize = 6; fSize = 6;
knobFont = new Font(_scaleFont.FontFamily, fSize); knobFont = new Font(_scaleFont.FontFamily, fSize);
strsize = Gr.MeasureString(str, knobFont); strsize = TextRenderer.MeasureText(str, knobFont);
} }
int strw = (int)strsize.Width; int strw = (int)strsize.Width;

View File

@ -126,16 +126,16 @@ namespace Sunny.UI
{ {
base.OnPaint(e); base.OnPaint(e);
SizeF TextSize = e.Graphics.MeasureString(Text, Font); Size TextSize = TextRenderer.MeasureText(Text, Font);
if (autoSize && Dock == DockStyle.None) if (autoSize && Dock == DockStyle.None)
{ {
float width = (MarkPos == UIMarkPos.Left || MarkPos == UIMarkPos.Right) ? int width = (MarkPos == UIMarkPos.Left || MarkPos == UIMarkPos.Right) ?
TextSize.Width + MarkSize + 2 : TextSize.Width; TextSize.Width + MarkSize + 2 : TextSize.Width;
float height = (MarkPos == UIMarkPos.Top || MarkPos == UIMarkPos.Bottom) ? int height = (MarkPos == UIMarkPos.Top || MarkPos == UIMarkPos.Bottom) ?
TextSize.Height + MarkSize + 2 : TextSize.Height; TextSize.Height + MarkSize + 2 : TextSize.Height;
if (Width != (int)width) Width = (int)width; if (Width != width) Width = width;
if (Height != (int)height) Height = (int)height; if (Height != height) Height = height;
} }
switch (markPos) switch (markPos)

View File

@ -474,7 +474,7 @@ namespace Sunny.UI
if (symbol > 0) if (symbol > 0)
symbolSize = MenuHelper.GetSymbolSize(node); symbolSize = MenuHelper.GetSymbolSize(node);
SizeF sf = TextRenderer.MeasureText(node.Text, Font);// e.Graphics.MeasureString(node.Text, Font); SizeF sf = TextRenderer.MeasureText(node.Text, Font);
Color textColor = ForeColor; Color textColor = ForeColor;
if (i == ActiveIndex) if (i == ActiveIndex)

View File

@ -756,8 +756,8 @@ namespace Sunny.UI
//显示Tips圆圈 //显示Tips圆圈
if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid()) if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid())
{ {
SizeF tipsSize = e.Graphics.MeasureString(MenuHelper.GetTipsText(e.Node), TempFont); Size tipsSize = TextRenderer.MeasureText(MenuHelper.GetTipsText(e.Node), TempFont);
int sfMax = (int)Math.Max(tipsSize.Width, tipsSize.Height) + 1; int sfMax = Math.Max(tipsSize.Width, tipsSize.Height);
int tipsLeft = Width - sfMax - 16; int tipsLeft = Width - sfMax - 16;
if (e.Node.Nodes.Count > 0) tipsLeft -= 24; if (e.Node.Nodes.Count > 0) tipsLeft -= 24;
if (Bar.Visible) tipsLeft -= Bar.Width; if (Bar.Visible) tipsLeft -= Bar.Width;

View File

@ -168,13 +168,13 @@ namespace Sunny.UI
b16.Text = UILocalize.Next; b16.Text = UILocalize.Next;
btnSelect.Text = UILocalize.SelectTitle; btnSelect.Text = UILocalize.SelectTitle;
SizeF sf = b0.CreateGraphics().MeasureString(b0.Text, b0.Font); Size sf = TextRenderer.MeasureText(b0.Text, b0.Font);
b0.Width = b0.SymbolSize + (int)sf.Width + 10; b0.Width = b0.SymbolSize + sf.Width + 10;
sf = b16.CreateGraphics().MeasureString(b16.Text, b0.Font); sf = TextRenderer.MeasureText(b16.Text, b0.Font);
b16.Width = b16.SymbolSize + (int)sf.Width + 10; b16.Width = b16.SymbolSize + sf.Width + 10;
btnSelect.Width = (int)btnSelect.CreateGraphics().MeasureString(btnSelect.Text, btnSelect.Font).Width + 16; btnSelect.Width = TextRenderer.MeasureText(btnSelect.Text, btnSelect.Font).Width + 16;
uiLabel1.Text = UILocalize.SelectPageLeft; uiLabel1.Text = UILocalize.SelectPageLeft;
uiLabel2.Text = UILocalize.SelectPageRight; uiLabel2.Text = UILocalize.SelectPageRight;

View File

@ -150,7 +150,7 @@ namespace Sunny.UI
else else
processText = posValue.ToString(); processText = posValue.ToString();
SizeF sf = e.Graphics.MeasureString(processText, Font); Size sf = TextRenderer.MeasureText(processText, Font);
bool canShow = Height > sf.Height + 4; bool canShow = Height > sf.Height + 4;
if (ShowValue && canShow) if (ShowValue && canShow)
@ -198,7 +198,7 @@ namespace Sunny.UI
private void DrawString(Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align, int offsetX = 0, int offsetY = 0) private void DrawString(Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align, int offsetX = 0, int offsetY = 0)
{ {
if (str.IsNullOrEmpty()) return; if (str.IsNullOrEmpty()) return;
SizeF sf = g.MeasureString(str, font); Size sf = TextRenderer.MeasureText(str, font);
using Brush br = color.Brush(); using Brush br = color.Brush();
switch (align) switch (align)
{ {

View File

@ -150,13 +150,11 @@ namespace Sunny.UI
/// <param name="path">绘图路径</param> /// <param name="path">绘图路径</param>
protected override void OnPaintFore(Graphics g, GraphicsPath path) protected override void OnPaintFore(Graphics g, GraphicsPath path)
{ {
SizeF sf = g.MeasureString(Text, Font); Size sf = TextRenderer.MeasureText(Text, Font);
int y = (int)((Height - sf.Height) / 2); if (TextWidth != sf.Width)
if (TextWidth != (int)sf.Width)
{ {
XPos = 0; XPos = 0;
TextWidth = (int)sf.Width; TextWidth = sf.Width;
} }
if (ScrollingType == UIScrollingType.LeftToRight) if (ScrollingType == UIScrollingType.LeftToRight)

View File

@ -258,8 +258,7 @@ namespace Sunny.UI
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
drawSize = e.Graphics.MeasureString(Text, Font, new PointF(), StringFormat.GenericTypographic); drawSize = TextRenderer.MeasureText(Text, Font);
if (AutoSize) if (AutoSize)
{ {
point.X = Padding.Left; point.X = Padding.Left;
@ -305,8 +304,7 @@ namespace Sunny.UI
float fontSize = e.Graphics.DpiY * Font.SizeInPoints / 72; float fontSize = e.Graphics.DpiY * Font.SizeInPoints / 72;
drawPath.Reset(); drawPath.Reset();
drawPath.AddString(Text, Font.FontFamily, (int)Font.Style, fontSize, drawPath.AddString(Text, Font.FontFamily, (int)Font.Style, fontSize, point, StringFormat.GenericTypographic);
point, StringFormat.GenericTypographic);
e.Graphics.FillPath(forecolorBrush, drawPath); e.Graphics.FillPath(forecolorBrush, drawPath);
e.Graphics.DrawPath(drawPen, drawPath); e.Graphics.DrawPath(drawPen, drawPath);

View File

@ -364,7 +364,7 @@ namespace Sunny.UI
//字体图标 //字体图标
Color color = GetForeColor(); Color color = GetForeColor();
SizeF TextSize = e.Graphics.MeasureString(Text, Font); Size TextSize = TextRenderer.MeasureText(Text, Font);
if (ImageAlign == ContentAlignment.MiddleCenter && TextAlign == ContentAlignment.MiddleCenter) if (ImageAlign == ContentAlignment.MiddleCenter && TextAlign == ContentAlignment.MiddleCenter)
{ {
@ -372,7 +372,7 @@ namespace Sunny.UI
{ {
e.Graphics.DrawString(Text, Font, color, ClientRectangle, ContentAlignment.MiddleCenter); e.Graphics.DrawString(Text, Font, color, ClientRectangle, ContentAlignment.MiddleCenter);
} }
else if (TextSize.Width.Equals(0)) else if (Text.IsNullOrEmpty())
{ {
if (ImageSize.Width > 0) if (ImageSize.Width > 0)
{ {

View File

@ -644,7 +644,7 @@ namespace Sunny.UI
TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y + 2, ItemSize.Width, ItemSize.Height); TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y + 2, ItemSize.Width, ItemSize.Height);
} }
SizeF sf = e.Graphics.MeasureString(TabPages[index].Text, Font); Size sf = TextRenderer.MeasureText(TabPages[index].Text, Font);
int textLeft = ImageList?.ImageSize.Width ?? 0; int textLeft = ImageList?.ImageSize.Width ?? 0;
if (ImageList != null) textLeft += 4 + 4 + 6; if (ImageList != null) textLeft += 4 + 4 + 6;
if (TextAlignment == HorizontalAlignment.Right) if (TextAlignment == HorizontalAlignment.Right)
@ -696,8 +696,8 @@ namespace Sunny.UI
string TipsText = GetTipsText(TabPages[index]); string TipsText = GetTipsText(TabPages[index]);
if (Enabled && TipsText.IsValid()) if (Enabled && TipsText.IsValid())
{ {
sf = e.Graphics.MeasureString(TipsText, TempFont); sf = TextRenderer.MeasureText(TipsText, TempFont);
int sfMax = (int)Math.Max(sf.Width, sf.Height) + 1; int sfMax = Math.Max(sf.Width, sf.Height);
int x = TabRect.Width - 1 - 2 - sfMax; int x = TabRect.Width - 1 - 2 - sfMax;
if (showActiveCloseButton || ShowCloseButton) x -= 24; if (showActiveCloseButton || ShowCloseButton) x -= 24;
int y = 1 + 1; int y = 1 + 1;

View File

@ -360,10 +360,10 @@ namespace Sunny.UI
for (int index = 0; index <= TabCount - 1; index++) for (int index = 0; index <= TabCount - 1; index++)
{ {
Rectangle TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y - 2, base.ItemSize.Height + 4, base.ItemSize.Width); Rectangle TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y - 2, base.ItemSize.Height + 4, base.ItemSize.Width);
SizeF sf = e.Graphics.MeasureString(TabPages[index].Text, Font); Size sf = TextRenderer.MeasureText(TabPages[index].Text, Font);
int textLeft = 4 + 6 + 4 + (ImageList?.ImageSize.Width ?? 0); int textLeft = 4 + 6 + 4 + (ImageList?.ImageSize.Width ?? 0);
if (TextAlignment == HorizontalAlignment.Right) if (TextAlignment == HorizontalAlignment.Right)
textLeft = (int)(TabRect.Width - 4 - sf.Width); textLeft = TabRect.Width - 4 - sf.Width;
if (TextAlignment == HorizontalAlignment.Center) if (TextAlignment == HorizontalAlignment.Center)
textLeft = textLeft + (int)((TabRect.Width - textLeft - sf.Width) / 2.0f); textLeft = textLeft + (int)((TabRect.Width - textLeft - sf.Width) / 2.0f);

View File

@ -258,7 +258,7 @@ namespace Sunny.UI
{ {
if (tooltip.Title.IsValid()) if (tooltip.Title.IsValid())
{ {
titleSize = e.Graphics.MeasureString(tooltip.Title, TempTitleFont); titleSize = TextRenderer.MeasureText(tooltip.Title, TempTitleFont);
} }
e.Graphics.DrawString(tooltip.Title, TempTitleFont, ForeColor, new Rectangle(tooltip.Symbol > 0 ? tooltip.SymbolSize + 5 : 5, 5, bounds.Width, bounds.Height), ContentAlignment.TopLeft); e.Graphics.DrawString(tooltip.Title, TempTitleFont, ForeColor, new Rectangle(tooltip.Symbol > 0 ? tooltip.SymbolSize + 5 : 5, 5, bounds.Width, bounds.Height), ContentAlignment.TopLeft);

View File

@ -1027,7 +1027,7 @@ namespace Sunny.UI
var checkBoxLeft = drawLeft - 2; var checkBoxLeft = drawLeft - 2;
var imageLeft = drawLeft; var imageLeft = drawLeft;
var haveImage = false; var haveImage = false;
var sf = e.Graphics.MeasureString(e.Node.Text, Font); var sf = TextRenderer.MeasureText(e.Node.Text, Font);
if (CheckBoxes) if (CheckBoxes)
{ {