* 重构MeasureString

This commit is contained in:
Sunny 2023-05-16 13:27:58 +08:00
parent bd9ec9d115
commit 169b079f32
16 changed files with 19 additions and 46 deletions

View File

@ -351,7 +351,7 @@ namespace Sunny.UI
if (tip.Text != Bars[0][selectIndex].Tips)
{
tip.Text = Bars[0][selectIndex].Tips;
tip.Size = new Size((int)Bars[0][selectIndex].Size.Width + 4, (int)Bars[0][selectIndex].Size.Height + 4);
tip.Size = new Size(Bars[0][selectIndex].Size.Width + 4, Bars[0][selectIndex].Size.Height + 4);
}
int x = e.Location.X + 15;
@ -619,7 +619,7 @@ namespace Sunny.UI
for (int i = 0; i < Option.XAxis.Data.Count; i++)
{
Bars[0][i].Size = g.MeasureString(Bars[0][i].Tips, TempFont);
Bars[0][i].Size = TextRenderer.MeasureText(Bars[0][i].Tips, TempFont);
}
}
@ -630,7 +630,7 @@ namespace Sunny.UI
public string Tips { get; set; }
public SizeF Size { get; set; }
public Size Size { get; set; }
public Color Color { get; set; }

View File

@ -285,7 +285,7 @@ namespace Sunny.UI
foreach (var data in legend.Data)
{
SizeF sf = g.MeasureString(data, TempLegendFont);
Size sf = TextRenderer.MeasureText(data, TempLegendFont);
totalHeight += sf.Height;
totalWidth += sf.Width;
totalWidth += 20;
@ -324,7 +324,7 @@ namespace Sunny.UI
for (int i = 0; i < legend.DataCount; i++)
{
var data = legend.Data[i];
SizeF sf = g.MeasureString(data, TempLegendFont);
Size sf = TextRenderer.MeasureText(data, TempLegendFont);
Color color = ChartStyle.GetColor(i);
if (legend.Colors.Count > 0 && i >= 0 && i < legend.Colors.Count)

View File

@ -175,7 +175,7 @@ namespace Sunny.UI
else
g.FillFan(color, angle.Center, angle.Inner, angle.Outer, angle.Start - 90, angle.Sweep);
Angles[pieIndex][azIndex].TextSize = g.MeasureString(Angles[pieIndex][azIndex].Text, TempFont);
Angles[pieIndex][azIndex].TextSize = TextRenderer.MeasureText(Angles[pieIndex][azIndex].Text, TempFont);
if (pie.Label.Show && ActiveAzIndex == azIndex)
{
@ -322,7 +322,7 @@ namespace Sunny.UI
public string Text { get; set; }
public SizeF TextSize { get; set; }
public Size TextSize { get; set; }
}
}
}

View File

@ -412,7 +412,7 @@ namespace Sunny.UI
}
}
SizeF sfName = g.MeasureString(Option.YAxis.Name, TempFont);
Size sfName = TextRenderer.MeasureText(Option.YAxis.Name, TempFont);
float xx = DrawOrigin.X - Option.YAxis.AxisTick.Length - widthMax - sfName.Height / 2.0f;
float yy = Option.Grid.Top + DrawSize.Height / 2.0f;
g.DrawRotateString(Option.YAxis.Name, TempFont, ForeColor, new PointF(xx, yy), 270);
@ -458,7 +458,7 @@ namespace Sunny.UI
}
}
SizeF sfName = g.MeasureString(Option.Y2Axis.Name, TempFont);
Size sfName = TextRenderer.MeasureText(Option.Y2Axis.Name, TempFont);
float xx = Width - Option.Grid.Right + Option.Y2Axis.AxisTick.Length + widthMax + sfName.Height / 2.0f;
float yy = Option.Grid.Top + DrawSize.Height / 2.0f;
g.DrawRotateString(Option.Y2Axis.Name, TempFont, ForeColor, new PointF(xx, yy), 90);
@ -892,8 +892,8 @@ namespace Sunny.UI
{
using (Graphics g = this.CreateGraphics())
{
SizeF sf = g.MeasureString(sb.ToString(), TempFont);
tip.Size = new Size((int)sf.Width + 4, (int)sf.Height + 4);
Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont);
tip.Size = new Size(sf.Width + 4, sf.Height + 4);
}
int x = e.Location.X + 15;

View File

@ -182,7 +182,7 @@ namespace Sunny.UI
if (data.StyleCustomMode) color = data.Color;
RectangleF rectx = new RectangleF(rect.X - 10, rect.Y - 10, rect.Width + 20, rect.Width + 20);
g.FillPie(color, (ActivePieIndex == pieIndex && ActiveAzIndex == azIndex) ? rectx : rect, Angles[pieIndex][azIndex].Start - 90, Angles[pieIndex][azIndex].Sweep);
Angles[pieIndex][azIndex].TextSize = g.MeasureString(Angles[pieIndex][azIndex].Text, TempFont);
Angles[pieIndex][azIndex].TextSize = TextRenderer.MeasureText(Angles[pieIndex][azIndex].Text, TempFont);
if (pie.Label.Show)
{
@ -369,7 +369,7 @@ namespace Sunny.UI
public string Text { get; set; }
public SizeF TextSize { get; set; }
public Size TextSize { get; set; }
}
}
}

View File

@ -264,7 +264,7 @@ namespace Sunny.UI
private static void DrawRotateString(this Graphics g, string text, Font font, Brush brush, PointF centerPoint, float angle)
{
if (text.IsNullOrEmpty()) return;
SizeF sf = g.MeasureString(text, font);
SizeF sf = TextRenderer.MeasureText(text, font);
float x1 = centerPoint.X - sf.Width / 2.0f;
float y1 = centerPoint.Y - sf.Height / 2.0f;

View File

@ -256,13 +256,7 @@ namespace Sunny.UI
float ty = (float)(cy + (radius - (20 * drawRatio)) * Math.Sin(currentAngle));
double val = Math.Round(rulerValue);
String str = String.Format("{0,0:D}", (int)val);
SizeF size = Gr.MeasureString(str, font);
Gr.DrawString(str,
font,
br,
tx - (float)(size.Width * 0.5),
ty - (float)(size.Height * 0.5));
Gr.DrawString(str, font, scaleColor, new Rectangle((int)(tx - w), (int)(ty - h), (int)(w * 2), (int)(h * 2)), ContentAlignment.MiddleCenter);
rulerValue += (float)((maxValue - minValue) / (scaleDivisions - 1));

View File

@ -152,7 +152,7 @@ namespace Sunny.UI
float width = 0;
if (Items.Count == 0)
{
SizeF sf = g.MeasureString(Text, Font);
Size sf = TextRenderer.MeasureText(Text, Font);
width = sf.Width + Height + 6;
if (itemWidth < width) itemWidth = (int)width;
List<PointF> points = new List<PointF>();
@ -169,7 +169,6 @@ namespace Sunny.UI
g.FillPolygon(br, points.ToArray());
}
//g.DrawString(Text, Font, ForeColor, (Width - sf.Width) / 2.0f, (Height - sf.Height) / 2.0f);
g.DrawString(Text, Font, ForeColor, ClientRectangle, ContentAlignment.MiddleCenter);
}
else
@ -238,7 +237,6 @@ namespace Sunny.UI
g.FillPolygon(br, points.ToArray());
}
//g.DrawString(item.ToString(), Font, index <= ItemIndex ? ForeColor : UnSelectedForeColor, begin + (itemWidth - sf.Width) / 2.0f, (Height - sf.Height) / 2.0f);
g.DrawString(item.ToString(), Font, index <= ItemIndex ? ForeColor : UnSelectedForeColor,
new Rectangle(begin, 0, itemWidth, Height), ContentAlignment.MiddleCenter);
begin = begin + itemWidth - 3 - Height / 2 + Interval;

View File

@ -311,9 +311,9 @@ namespace Sunny.UI
if (autoSize && Dock == DockStyle.None)
{
SizeF sf = e.Graphics.MeasureString(Text, Font);
if (Width != (int)(sf.Width) + 6) Width = (int)(sf.Width) + 6;
if (Height != (int)(sf.Height) + 6) Height = (int)(sf.Height) + 6;
Size sf = TextRenderer.MeasureText(Text, Font);
if (Width != sf.Width + 6) Width = sf.Width + 6;
if (Height != sf.Height + 6) Height = sf.Height + 6;
}
if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText))
@ -324,7 +324,6 @@ namespace Sunny.UI
int x = Width - 1 - 2 - sfMax;
int y = 1 + 1;
e.Graphics.FillEllipse(TipsColor, x - 1, y, sfMax, sfMax);
//e.Graphics.DrawString(TipsText, TempFont, TipsForeColor, x + sfMax / 2.0f - sf.Width / 2.0f, y + sfMax / 2.0f - sf.Height / 2.0f);
e.Graphics.DrawString(TipsText, TempFont, TipsForeColor, new Rectangle(x, y, sfMax, sfMax), ContentAlignment.MiddleCenter);
}

View File

@ -626,7 +626,6 @@ namespace Sunny.UI
case TextImageRelation.TextAboveImage:
{
#region
//e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Padding.Top);
e.Graphics.DrawString(Text, Font, color, new Rectangle(0, Padding.Top, Width, Height), ContentAlignment.TopCenter);
//字体图标
@ -680,7 +679,6 @@ namespace Sunny.UI
e.Graphics.DrawImage(Image, ImageTop, (Height - ImageSize.Height) / 2.0f, ImageSize.Width, ImageSize.Height);
}
//e.Graphics.DrawString(Text, Font, color, Width - Padding.Right - sf.Width, (Height - sf.Height) / 2);
e.Graphics.DrawString(Text, Font, color, new Rectangle(0, 0, Width - Padding.Right, Height), ContentAlignment.MiddleRight);
#endregion
}
@ -688,7 +686,6 @@ namespace Sunny.UI
case TextImageRelation.TextBeforeImage:
{
#region
//e.Graphics.DrawString(Text, Font, color, Padding.Left, (Height - sf.Height) / 2);
e.Graphics.DrawString(Text, Font, color, new Rectangle(Padding.Left, 0, Width, Height), ContentAlignment.MiddleLeft);
//字体图标
@ -742,7 +739,6 @@ namespace Sunny.UI
e.Graphics.DrawImage(Image, (Width - ImageSize.Width) / 2.0f, ImageTop, ImageSize.Width, ImageSize.Height);
}
//e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height);
e.Graphics.DrawString(Text, Font, color, new Rectangle(0, 0, Width, Height - Padding.Bottom), ContentAlignment.BottomCenter);
#endregion
}

View File

@ -794,7 +794,6 @@ namespace Sunny.UI
if (ShowDescription && !string.IsNullOrEmpty(item.Description))
{
//g.DrawString(item.Description, e.Font, foreColor, ImageInterval, thumbnailSize + ImageInterval);
g.DrawString(item.Description, e.Font, foreColor, new Rectangle(ImageInterval, e.Bounds.Y + thumbnailSize + ImageInterval, e.Bounds.Width, e.Bounds.Height), ContentAlignment.TopLeft);
}

View File

@ -527,12 +527,10 @@ namespace Sunny.UI
e.Graphics.DrawImage((Bitmap)ImageList.Images[node.ImageIndex], NodeX + i * NodeSize.Width + (NodeSize.Width - sf.Width - symbolSize) / 2.0f, NodeY + (NodeSize.Height - ImageList.ImageSize.Height) / 2);
}
//e.Graphics.DrawString(node.Text, Font, textColor, NodeX + i * NodeSize.Width + (NodeSize.Width - sf.Width + symbolSize) / 2.0f, NodeY + (NodeSize.Height - sf.Height) / 2);
e.Graphics.DrawString(node.Text, Font, textColor, new Rectangle(NodeX + i * NodeSize.Width + symbolSize / 2, NodeY, NodeSize.Width, NodeSize.Height), ContentAlignment.MiddleCenter);
}
else
{
//e.Graphics.DrawString(node.Text, Font, textColor, NodeX + i * NodeSize.Width + (NodeSize.Width - sf.Width) / 2.0f, NodeY + (NodeSize.Height - sf.Height) / 2);
e.Graphics.DrawString(node.Text, Font, textColor, new Rectangle(NodeX + i * NodeSize.Width, NodeY, NodeSize.Width, NodeSize.Height), ContentAlignment.MiddleCenter);
}

View File

@ -164,7 +164,6 @@ namespace Sunny.UI
if (XPos + TextWidth > Width && TextWidth < Width - offset)
{
XPos1 = XPos - Width + offset;
//g.DrawString(Text, Font, ForeColor, XPos1, y);
g.DrawString(Text, Font, ForeColor, new Rectangle(XPos1, 0, Width, Height), ContentAlignment.MiddleLeft);
}
else
@ -172,7 +171,6 @@ namespace Sunny.UI
XPos1 = -TextWidth + offset;
}
//g.DrawString(Text, Font, ForeColor, XPos, y);
g.DrawString(Text, Font, ForeColor, new Rectangle(XPos, 0, Width, Height), ContentAlignment.MiddleLeft);
}
@ -181,7 +179,6 @@ namespace Sunny.UI
if (XPos < 0 && TextWidth < Width - offset)
{
XPos1 = Width + XPos - offset;
//g.DrawString(Text, Font, ForeColor, XPos1, y);
g.DrawString(Text, Font, ForeColor, new Rectangle(XPos1, 0, Width, Height), ContentAlignment.MiddleLeft);
}
else
@ -189,7 +186,6 @@ namespace Sunny.UI
XPos1 = Width - offset;
}
//g.DrawString(Text, Font, ForeColor, XPos, y);
g.DrawString(Text, Font, ForeColor, new Rectangle(XPos, 0, Width, Height), ContentAlignment.MiddleLeft);
}
}

View File

@ -370,7 +370,6 @@ namespace Sunny.UI
{
if (ImageSize.Width.Equals(0))
{
//e.Graphics.DrawString(Text, Font, color, (Width - TextSize.Width) / 2.0f, (Height - TextSize.Height) / 2.0f);
e.Graphics.DrawString(Text, Font, color, ClientRectangle, ContentAlignment.MiddleCenter);
}
else if (TextSize.Width.Equals(0))
@ -411,7 +410,6 @@ namespace Sunny.UI
ImageSize.Width, ImageSize.Height);
}
//e.Graphics.DrawString(Text, Font, color, (Width - allWidth) / 2.0f + ImageSize.Width + ImageInterval, (Height - TextSize.Height) / 2.0f);
e.Graphics.DrawString(Text, Font, color, new Rectangle((int)((Width - allWidth) / 2 + ImageSize.Width + ImageInterval), 0, Width, Height), ContentAlignment.MiddleLeft);
}
}

View File

@ -378,7 +378,6 @@ namespace Sunny.UI
// 绘制标题
Color textColor = index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor;
//e.Graphics.DrawString(TabPages[index].Text, Font, textColor, textLeft, TabRect.Top + 2 + (TabRect.Height - sf.Height) / 2.0f);
e.Graphics.DrawString(TabPages[index].Text, Font, textColor, new Rectangle(textLeft, TabRect.Top, TabRect.Width, TabRect.Height), ContentAlignment.MiddleLeft);
// 绘制图标

View File

@ -1053,7 +1053,6 @@ namespace Sunny.UI
if (Painter.ContainsKey(e.Node))
{
e.Graphics.FillRectangle(Painter[e.Node].BackColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
//e.Graphics.DrawString(e.Node.Text, Font, Painter[e.Node].ForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, Painter[e.Node].ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
}
else
@ -1061,20 +1060,17 @@ namespace Sunny.UI
if (e.Node == SelectedNode)
{
e.Graphics.FillRectangle(SelectedColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
//e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
checkboxColor = SelectedForeColor;
}
else if (e.Node == CurrentNode && (e.State & TreeNodeStates.Hot) != 0)
{
e.Graphics.FillRectangle(HoverColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
//e.Graphics.DrawString(e.Node.Text, Font, ForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
}
else
{
e.Graphics.FillRectangle(FillColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
//e.Graphics.DrawString(e.Node.Text, Font, ForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), ContentAlignment.MiddleLeft);
}
}