* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-14 15:59:16 +08:00
parent decb112c21
commit cf5727b374
4 changed files with 87 additions and 60 deletions

View File

@ -18,6 +18,7 @@
* *
* 2020-06-06: V2.2.5 * 2020-06-06: V2.2.5
* 2020-09-10: V2.2.7 线 * 2020-09-10: V2.2.7 线
* 2022-05-14: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -267,40 +268,10 @@ namespace Sunny.UI
protected void DrawTitle(Graphics g, UITitle title) protected void DrawTitle(Graphics g, UITitle title)
{ {
if (title == null) return; if (title == null) return;
SizeF sf = g.MeasureString(title.Text, Font); Size sf = TextRenderer.MeasureText(title.Text, Font);
float left = 0; g.DrawString(title.Text, Font, ChartStyle.ForeColor, new Rectangle(TextInterval, TextInterval, Width - TextInterval * 2, Height - TextInterval * 2), (StringAlignment)((int)title.Left), (StringAlignment)((int)title.Top));
switch (title.Left) g.DrawString(title.SubText, TempFont, ChartStyle.ForeColor, new Rectangle(TextInterval, TextInterval, Width - TextInterval * 2, Height - TextInterval * 2),
{ (StringAlignment)((int)title.Left), (StringAlignment)((int)title.Top), 0, title.Top == UITopAlignment.Bottom ? -sf.Height : sf.Height);
case UILeftAlignment.Left: left = TextInterval; break;
case UILeftAlignment.Center: left = (Width - sf.Width) / 2.0f; break;
case UILeftAlignment.Right: left = Width - TextInterval - sf.Width; break;
}
float top = 0;
switch (title.Top)
{
case UITopAlignment.Top: top = TextInterval; break;
case UITopAlignment.Center: top = (Height - sf.Height) / 2.0f; break;
case UITopAlignment.Bottom: top = Height - TextInterval - sf.Height; break;
}
g.DrawString(title.Text, Font, ChartStyle.ForeColor, left, top);
SizeF sfs = g.MeasureString(title.SubText, TempFont);
switch (title.Left)
{
case UILeftAlignment.Left: left = TextInterval; break;
case UILeftAlignment.Center: left = (Width - sfs.Width) / 2.0f; break;
case UILeftAlignment.Right: left = Width - TextInterval - sf.Width; break;
}
switch (title.Top)
{
case UITopAlignment.Top: top = top + sf.Height; break;
case UITopAlignment.Center: top = top + sf.Height; break;
case UITopAlignment.Bottom: top = top - sf.Height; break;
}
g.DrawString(title.SubText, TempFont, ChartStyle.ForeColor, left, top);
} }
protected void DrawLegend(Graphics g, UILegend legend) protected void DrawLegend(Graphics g, UILegend legend)
@ -362,7 +333,7 @@ namespace Sunny.UI
if (legend.Orient == UIOrient.Horizontal) if (legend.Orient == UIOrient.Horizontal)
{ {
g.FillRoundRectangle(color, (int)startLeft, (int)top + 1, 18, (int)oneHeight - 2, 5); g.FillRoundRectangle(color, (int)startLeft, (int)top + 1, 18, (int)oneHeight - 2, 5);
g.DrawString(data, TempLegendFont, color, startLeft + 20, top); g.DrawString(data, TempLegendFont, color, new Rectangle((int)startLeft + 18, (int)top, Width, Height), ContentAlignment.TopLeft);
startLeft += 22; startLeft += 22;
startLeft += sf.Width; startLeft += sf.Width;
} }
@ -370,7 +341,7 @@ namespace Sunny.UI
if (legend.Orient == UIOrient.Vertical) if (legend.Orient == UIOrient.Vertical)
{ {
g.FillRoundRectangle(color, (int)left, (int)startTop + 1, 18, (int)oneHeight - 2, 5); g.FillRoundRectangle(color, (int)left, (int)startTop + 1, 18, (int)oneHeight - 2, 5);
g.DrawString(data, TempLegendFont, color, left + 20, startTop); g.DrawString(data, TempLegendFont, color, new Rectangle((int)left + 18, (int)startTop, Width, Height), ContentAlignment.TopLeft);
startTop += oneHeight; startTop += oneHeight;
} }
} }

View File

@ -19,6 +19,7 @@
* 2020-06-06: V2.2.5 * 2020-06-06: V2.2.5
* 2021-07-22: V3.0.5 * 2021-07-22: V3.0.5
* 2022-07-29: V3.2.2 Option.DecimalPlaces * 2022-07-29: V3.2.2 Option.DecimalPlaces
* 2022-05-14: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -180,8 +181,7 @@ namespace Sunny.UI
{ {
if (pie.Label.Position == UIPieSeriesLabelPosition.Center) if (pie.Label.Position == UIPieSeriesLabelPosition.Center)
{ {
SizeF sf = g.MeasureString(pie.Data[azIndex].Name, Font); g.DrawString(pie.Data[azIndex].Name, Font, color, new Rectangle((int)angle.Center.X - Width, (int)angle.Center.Y - Height, Width * 2, Height * 2), ContentAlignment.MiddleCenter);
g.DrawString(pie.Data[azIndex].Name, Font, color, angle.Center.X - sf.Width / 2.0f, angle.Center.Y - sf.Height / 2.0f);
} }
} }
} }

View File

@ -19,6 +19,7 @@
* 2020-06-06: V2.2.5 * 2020-06-06: V2.2.5
* 2021-07-22: V3.0.5 * 2021-07-22: V3.0.5
* 2022-07-29: V3.2.2 Option.DecimalPlaces * 2022-07-29: V3.2.2 Option.DecimalPlaces
* 2022-05-14: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -193,20 +194,32 @@ namespace Sunny.UI
if (pie.Data[azIndex].Value > 0) if (pie.Data[azIndex].Value > 0)
{ {
string text = name + pie.Data[azIndex].Value.ToString("F" + Option.DecimalPlaces); string text = name + pie.Data[azIndex].Value.ToString("F" + Option.DecimalPlaces);
SizeF sf = g.MeasureString(text, TempFont); Size sf = TextRenderer.MeasureText(text, TempFont);
PointF pf;
int added = 9; int added = 9;
float left = 0, top = 0;
if (az >= 0 && az < 90) if (az >= 0 && az < 90)
pf = new PointF((float)(DrawCenter(pie).X + RadiusSize(pie) * x + added), (float)(DrawCenter(pie).Y - RadiusSize(pie) * y - sf.Height - added)); {
left = (float)(DrawCenter(pie).X + RadiusSize(pie) * x + added);
top = (float)(DrawCenter(pie).Y - RadiusSize(pie) * y - sf.Height - added);
}
else if (az >= 90 && az < 180) else if (az >= 90 && az < 180)
pf = new PointF((float)(DrawCenter(pie).X + RadiusSize(pie) * x + added), (float)(DrawCenter(pie).Y + RadiusSize(pie) * y + added)); {
left = (float)(DrawCenter(pie).X + RadiusSize(pie) * x + added);
top = (float)(DrawCenter(pie).Y + RadiusSize(pie) * y + added);
}
else if (az >= 180 && az < 270) else if (az >= 180 && az < 270)
pf = new PointF((float)(DrawCenter(pie).X - RadiusSize(pie) * x - added) - sf.Width, (float)(DrawCenter(pie).Y + RadiusSize(pie) * y + added)); {
left = (float)(DrawCenter(pie).X - RadiusSize(pie) * x - added) - sf.Width;
top = (float)(DrawCenter(pie).Y + RadiusSize(pie) * y + added);
}
else else
pf = new PointF((float)(DrawCenter(pie).X - RadiusSize(pie) * x - added) - sf.Width, (float)(DrawCenter(pie).Y - RadiusSize(pie) * y) - sf.Height - added); {
left = (float)(DrawCenter(pie).X - RadiusSize(pie) * x - added) - sf.Width;
top = (float)(DrawCenter(pie).Y - RadiusSize(pie) * y) - sf.Height - added;
}
if (pie.Data[azIndex].Value > 0) if (pie.Data[azIndex].Value > 0)
g.DrawString(text, TempFont, color, pf.X, pf.Y); g.DrawString(text, TempFont, color, new Rectangle((int)left, (int)top, Width, Height), ContentAlignment.TopLeft);
} }
} }
} }

View File

@ -191,18 +191,7 @@ namespace Sunny.UI
set set
{ {
showType = value; showType = value;
switch (value) Invalidate();
{
case UIDateType.YearMonthDay:
DateFormat = "yyyy-MM-dd";
break;
case UIDateType.YearMonth:
DateFormat = "yyyy-MM";
break;
case UIDateType.Year:
DateFormat = "yyyy";
break;
}
} }
} }
@ -284,7 +273,19 @@ namespace Sunny.UI
{ {
if (value < new DateTime(1900, 1, 1)) if (value < new DateTime(1900, 1, 1))
value = new DateTime(1900, 1, 1); value = new DateTime(1900, 1, 1);
switch (ShowType)
{
case UIDateType.YearMonthDay:
Text = value.ToString(dateFormat); Text = value.ToString(dateFormat);
break;
case UIDateType.YearMonth:
Text = value.ToString(dateYearMonthFormat);
break;
case UIDateType.Year:
Text = value.ToString(dateYearFormat);
break;
}
if (item.Date != value) if (item.Date != value)
{ {
@ -319,9 +320,51 @@ namespace Sunny.UI
set set
{ {
dateFormat = value; dateFormat = value;
if (ShowType == UIDateType.YearMonthDay)
{
Text = Value.ToString(dateFormat); Text = Value.ToString(dateFormat);
MaxLength = dateFormat.Length; MaxLength = dateFormat.Length;
} }
} }
} }
private string dateYearMonthFormat = "yyyy-MM";
[Description("日期格式化掩码"), Category("SunnyUI")]
[DefaultValue("yyyy-MM")]
public string DateYearMonthFormat
{
get => dateYearMonthFormat;
set
{
dateYearMonthFormat = value;
if (ShowType == UIDateType.YearMonth)
{
Text = Value.ToString(dateYearMonthFormat);
MaxLength = dateYearMonthFormat.Length;
}
}
}
private string dateYearFormat = "yyyy";
[Description("日期格式化掩码"), Category("SunnyUI")]
[DefaultValue("yyyy")]
public string DateYearFormat
{
get => dateYearFormat;
set
{
dateYearFormat = value;
if (ShowType == UIDateType.Year)
{
Text = Value.ToString(dateYearFormat);
MaxLength = dateYearFormat.Length;
}
}
}
}
} }