* 重构DrawString函数
This commit is contained in:
parent
90c26a6b24
commit
bd9ec9d115
@ -187,7 +187,7 @@ namespace Sunny.UI
|
||||
/// 提供一个Graphics,常用于需要计算文字大小时
|
||||
/// </summary>
|
||||
/// <returns>大小</returns>
|
||||
public static Graphics Graphics()
|
||||
internal static Graphics Graphics()
|
||||
{
|
||||
if (TempGraphics == null)
|
||||
{
|
||||
@ -198,17 +198,6 @@ namespace Sunny.UI
|
||||
return TempGraphics;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算文字大小
|
||||
/// </summary>
|
||||
/// <param name="text">文字</param>
|
||||
/// <param name="font">字体</param>
|
||||
/// <returns>大小</returns>
|
||||
public static SizeF MeasureString(this string text, Font font)
|
||||
{
|
||||
return Graphics().MeasureString(text, font);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取起始颜色到终止颜色之间的渐变颜色
|
||||
/// </summary>
|
||||
|
@ -371,8 +371,6 @@ namespace Sunny.UI
|
||||
|
||||
if (Icon == UIIcon.Text)
|
||||
{
|
||||
//SizeF sf = e.Graphics.MeasureString(Text, Font);
|
||||
//e.Graphics.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2.0f + TextOffset.X, (Height - sf.Height) / 2.0f + 1 + TextOffset.Y);
|
||||
e.Graphics.DrawString(Text, Font, foreColor, ClientRectangle, ContentAlignment.MiddleCenter, TextOffset.X, TextOffset.Y);
|
||||
}
|
||||
|
||||
|
@ -187,7 +187,6 @@ namespace Sunny.UI
|
||||
int index = 0;
|
||||
foreach (var item in Items)
|
||||
{
|
||||
//SizeF sf = g.MeasureString(item.ToString(), Font);
|
||||
List<PointF> points = new List<PointF>();
|
||||
|
||||
if (index == 0 && AlignBothEnds)
|
||||
|
@ -64,9 +64,9 @@ namespace Sunny.UI
|
||||
base.OnPaint(e);
|
||||
if (AutoSize && Dock == DockStyle.None)
|
||||
{
|
||||
SizeF sf = Text.MeasureString(Font);
|
||||
int w = (int)sf.Width + ImageSize + 3;
|
||||
int h = Math.Max(ImageSize, (int)sf.Height) + 2;
|
||||
Size sf = TextRenderer.MeasureText(Text, Font);
|
||||
int w = sf.Width + ImageSize + 3;
|
||||
int h = Math.Max(ImageSize, sf.Height) + 2;
|
||||
if (Width != w) Width = w;
|
||||
if (Height != h) Height = h;
|
||||
}
|
||||
|
@ -635,7 +635,7 @@ namespace Sunny.UI
|
||||
{
|
||||
for (int i = 0; i < Items.Count; i++)
|
||||
{
|
||||
SizeF sf = GDI.MeasureString(Items[i].ToString(), Font);
|
||||
Size sf = TextRenderer.MeasureText(Items[i].ToString(), Font);
|
||||
dropWidth = Math.Max((int)sf.Width + ScrollBarInfo.VerticalScrollBarWidth() + 6, dropWidth);
|
||||
}
|
||||
}
|
||||
@ -643,7 +643,7 @@ namespace Sunny.UI
|
||||
{
|
||||
for (int i = 0; i < Items.Count; i++)
|
||||
{
|
||||
SizeF sf = GDI.MeasureString(dropForm.ListBox.GetItemText(Items[i]), Font);
|
||||
Size sf = TextRenderer.MeasureText(dropForm.ListBox.GetItemText(Items[i]), Font);
|
||||
dropWidth = Math.Max((int)sf.Width + ScrollBarInfo.VerticalScrollBarWidth() + 6, dropWidth);
|
||||
}
|
||||
}
|
||||
|
@ -289,8 +289,6 @@ namespace Sunny.UI
|
||||
|
||||
foreach (TextBox txt in Controls.OfType<TextBox>().Where(t => t != txt1))
|
||||
{
|
||||
//SizeF sf = e.Graphics.MeasureString(".", Font);
|
||||
//e.Graphics.DrawString(".", Font, ForeColor, txt.Left - 5 + 2.5f - sf.Width / 2.0f, txt.Top);
|
||||
e.Graphics.DrawString(".", Font, ForeColor, new Rectangle(txt.Left - 5, 0, 5, Height), ContentAlignment.MiddleCenter, 2);
|
||||
}
|
||||
}
|
||||
|
@ -90,8 +90,6 @@ namespace Sunny.UI
|
||||
/// <param name="path">绘图路径</param>
|
||||
protected override void OnPaintFore(Graphics g, GraphicsPath path)
|
||||
{
|
||||
//SizeF sf = g.MeasureString(Text, Font);
|
||||
//g.DrawString(Text, Font, ForeColor, Width / 2 - sf.Width / 2, Height / 2 - sf.Height / 2);
|
||||
g.DrawString(Text, Font, ForeColor, ClientRectangle, TextAlign);
|
||||
}
|
||||
|
||||
|
@ -675,7 +675,6 @@ namespace Sunny.UI
|
||||
}
|
||||
}
|
||||
|
||||
//SizeF sf = e.Graphics.MeasureString(e.Node.Text, Font);
|
||||
if (e.Node == SelectedNode)
|
||||
{
|
||||
if (SelectedColorGradient)
|
||||
@ -690,14 +689,12 @@ namespace Sunny.UI
|
||||
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 - drawLeft, ItemHeight), ContentAlignment.MiddleLeft);
|
||||
e.Graphics.FillRectangle(SelectedHighColor, new Rectangle(0, e.Bounds.Y, 4, e.Bounds.Height));
|
||||
}
|
||||
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 - drawLeft, ItemHeight), ContentAlignment.MiddleLeft);
|
||||
}
|
||||
else
|
||||
@ -709,7 +706,6 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
e.Graphics.FillRectangle(color, 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 - drawLeft, ItemHeight), ContentAlignment.MiddleLeft);
|
||||
}
|
||||
|
||||
|
@ -65,9 +65,9 @@ namespace Sunny.UI
|
||||
base.OnPaint(e);
|
||||
if (AutoSize && Dock == DockStyle.None)
|
||||
{
|
||||
SizeF sf = Text.MeasureString(Font);
|
||||
int w = (int)sf.Width + ImageSize + 3;
|
||||
int h = Math.Max(ImageSize, (int)sf.Height) + 2;
|
||||
Size sf = TextRenderer.MeasureText(Text, Font);
|
||||
int w = sf.Width + ImageSize + 3;
|
||||
int h = Math.Max(ImageSize, sf.Height) + 2;
|
||||
if (Width != w) Width = w;
|
||||
if (Height != h) Height = h;
|
||||
}
|
||||
|
@ -271,15 +271,11 @@ namespace Sunny.UI
|
||||
if (!Active)
|
||||
{
|
||||
g.FillEllipse(fillColor.IsValid() ? fillColor : Color.White, 3, 3, rect.Height - 6, rect.Height - 6);
|
||||
//SizeF sf = g.MeasureString(InActiveText, Font);
|
||||
//g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + rect.Height - 6 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
|
||||
g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3 + rect.Height - 6, 0, width, rect.Height), ContentAlignment.MiddleCenter);
|
||||
}
|
||||
else
|
||||
{
|
||||
g.FillEllipse(fillColor.IsValid() ? fillColor : Color.White, Width - 3 - 1 - (rect.Height - 6), 3, rect.Height - 6, rect.Height - 6);
|
||||
//SizeF sf = g.MeasureString(ActiveText, Font);
|
||||
//g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
|
||||
g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3, 0, width, rect.Height), ContentAlignment.MiddleCenter);
|
||||
}
|
||||
}
|
||||
@ -293,15 +289,11 @@ namespace Sunny.UI
|
||||
if (!Active)
|
||||
{
|
||||
g.FillRoundRectangle(fillColor.IsValid() ? fillColor : Color.White, 3, 3, rect.Height - 6, rect.Height - 6, Radius);
|
||||
//SizeF sf = g.MeasureString(InActiveText, Font);
|
||||
//g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + rect.Height - 6 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
|
||||
g.DrawString(InActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3 + rect.Height - 6, 0, width, rect.Height), ContentAlignment.MiddleCenter);
|
||||
}
|
||||
else
|
||||
{
|
||||
g.FillRoundRectangle(fillColor.IsValid() ? fillColor : Color.White, Width - 3 - 1 - (rect.Height - 6), 3, rect.Height - 6, rect.Height - 6, Radius);
|
||||
//SizeF sf = g.MeasureString(ActiveText, Font);
|
||||
//g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, 3 + (width - sf.Width) / 2, 3 + (rect.Height - 6 - sf.Height) / 2);
|
||||
g.DrawString(ActiveText, Font, fillColor.IsValid() ? fillColor : Color.White, new Rectangle(3, 0, width, rect.Height), ContentAlignment.MiddleCenter);
|
||||
}
|
||||
}
|
||||
|
@ -166,24 +166,8 @@ namespace Sunny.UI
|
||||
g.DrawLine(RectColor, 0, TitleHeight, Width, TitleHeight);
|
||||
|
||||
color = Enabled ? TitleForeColor : UIFontColor.Regular;
|
||||
//SizeF sf = g.MeasureString(Text, Font);
|
||||
g.DrawString(Text, Font, color, new Rectangle(_titleInterval, 0, Width - _titleInterval * 2 - (ShowCollapse || ShowClose ? 24 : 0), TitleHeight), TextAlignment);
|
||||
|
||||
//switch (TextAlign)
|
||||
//{
|
||||
// case HorizontalAlignment.Left:
|
||||
// g.DrawString(Text, Font, color, _titleInterval, (TitleHeight - sf.Height) / 2.0f);
|
||||
// break;
|
||||
//
|
||||
// case HorizontalAlignment.Center:
|
||||
// g.DrawString(Text, Font, color, (Width - sf.Width) / 2.0f, (TitleHeight - sf.Height) / 2.0f);
|
||||
// break;
|
||||
//
|
||||
// case HorizontalAlignment.Right:
|
||||
// g.DrawString(Text, Font, color, Width - _titleInterval - sf.Width, (TitleHeight - sf.Height) / 2.0f);
|
||||
// break;
|
||||
//}
|
||||
|
||||
if (ShowCollapse)
|
||||
{
|
||||
if (InControlBox)
|
||||
|
@ -185,26 +185,24 @@ namespace Sunny.UI
|
||||
else
|
||||
{
|
||||
int symbolWidth = tooltip.Symbol > 0 ? tooltip.SymbolSize : 0;
|
||||
SizeF titleSize = new SizeF(0, 0);
|
||||
Size titleSize = new Size(0, 0);
|
||||
if (tooltip.Title.IsValid())
|
||||
{
|
||||
titleSize = GDI.MeasureString(tooltip.Title, TempTitleFont);
|
||||
titleSize = TextRenderer.MeasureText(tooltip.Title, TempTitleFont);
|
||||
}
|
||||
|
||||
SizeF textSize = GDI.MeasureString(tooltip.ToolTipText, TempFont);
|
||||
int allWidth = (int)Math.Max(textSize.Width, titleSize.Width) + 10;
|
||||
Size textSize = TextRenderer.MeasureText(tooltip.ToolTipText, TempFont);
|
||||
int allWidth = Math.Max(textSize.Width, titleSize.Width) + 10;
|
||||
if (symbolWidth > 0) allWidth = allWidth + symbolWidth + 5;
|
||||
int allHeight = titleSize.Height > 0 ?
|
||||
(int)titleSize.Height + (int)textSize.Height + 15 :
|
||||
(int)textSize.Height + 10;
|
||||
int allHeight = titleSize.Height > 0 ? titleSize.Height + textSize.Height + 15 : textSize.Height + 10;
|
||||
e.ToolTipSize = new Size(allWidth, allHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SizeF sf = GDI.MeasureString(GetToolTip(e.AssociatedControl), TempFont);
|
||||
e.ToolTipSize = sf.Size().Add(10, 10);
|
||||
Size sf = TextRenderer.MeasureText(GetToolTip(e.AssociatedControl), TempFont);
|
||||
e.ToolTipSize = sf.Add(10, 10);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,14 +56,14 @@ namespace Sunny.UI
|
||||
|
||||
if (Option.AutoLabelWidth)
|
||||
{
|
||||
float size = 0;
|
||||
int size = 0;
|
||||
foreach (var info in Option.Infos)
|
||||
{
|
||||
SizeF sf = info.Text.MeasureString(Font);
|
||||
Size sf = TextRenderer.MeasureText(info.Text, Font);
|
||||
size = Math.Max(sf.Width, size);
|
||||
}
|
||||
|
||||
Option.LabelWidth = (int)size + 1 + 50;
|
||||
Option.LabelWidth = size + 1 + 50;
|
||||
}
|
||||
|
||||
Width = Option.LabelWidth + Option.ValueWidth + 28;
|
||||
@ -145,9 +145,9 @@ namespace Sunny.UI
|
||||
string[] items = (string[])info.DataSource;
|
||||
edit.ActiveText = items[0];
|
||||
edit.InActiveText = items[1];
|
||||
SizeF sf1 = GDI.MeasureString(items[0], edit.Font);
|
||||
SizeF sf2 = GDI.MeasureString(items[0], edit.Font);
|
||||
edit.Width = (int)Math.Max(sf1.Width, sf2.Width) + edit.Height + 16;
|
||||
Size sf1 = TextRenderer.MeasureText(items[0], edit.Font);
|
||||
Size sf2 = TextRenderer.MeasureText(items[0], edit.Font);
|
||||
edit.Width = Math.Max(sf1.Width, sf2.Width) + edit.Height + 16;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1243,15 +1243,12 @@ namespace Sunny.UI
|
||||
}
|
||||
}
|
||||
|
||||
//SizeF sf = e.Graphics.MeasureString(Text, TitleFont);
|
||||
if (TextAlignment == StringAlignment.Center)
|
||||
{
|
||||
//e.Graphics.DrawString(Text, TitleFont, titleForeColor, (Width - sf.Width) / 2, (TitleHeight - sf.Height) / 2);
|
||||
e.Graphics.DrawString(Text, TitleFont, titleForeColor, new Rectangle(0, 0, Width, TitleHeight), ContentAlignment.MiddleCenter);
|
||||
}
|
||||
else
|
||||
{
|
||||
//e.Graphics.DrawString(Text, TitleFont, titleForeColor, 6 + (ShowTitleIcon && Icon != null ? 26 : 0), (TitleHeight - sf.Height) / 2);
|
||||
e.Graphics.DrawString(Text, TitleFont, titleForeColor, new Rectangle(6 + (ShowTitleIcon && Icon != null ? 26 : 0), 0, Width, TitleHeight), ContentAlignment.MiddleLeft);
|
||||
}
|
||||
}
|
||||
|
@ -629,8 +629,6 @@ namespace Sunny.UI
|
||||
e.Graphics.DrawFontImage(Symbol, SymbolSize, TitleForeColor, new Rectangle(ImageInterval, 0, SymbolSize, TitleHeight), SymbolOffset.X, SymbolOffset.Y);
|
||||
}
|
||||
|
||||
//SizeF sf = e.Graphics.MeasureString(Text, TitleFont);
|
||||
//e.Graphics.DrawString(Text, TitleFont, TitleForeColor, Symbol > 0 ? ImageInterval * 2 + SymbolSize : ImageInterval, (TitleHeight - sf.Height) / 2);
|
||||
e.Graphics.DrawString(Text, TitleFont, TitleForeColor, new Rectangle(Symbol > 0 ? ImageInterval * 2 + SymbolSize : ImageInterval, 0, Width, TitleHeight), ContentAlignment.MiddleLeft);
|
||||
|
||||
e.Graphics.SetHighQuality();
|
||||
|
Loading…
x
Reference in New Issue
Block a user