From cf5727b3749c7f9bf98761af4828f2028fe4cb6e Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 14 May 2023 15:59:16 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E9=87=8D=E6=9E=84DrawString=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Charts/UIChart.cs | 43 +++--------------- SunnyUI/Charts/UIDoughnutChart.cs | 4 +- SunnyUI/Charts/UIPieChart.cs | 27 +++++++++--- SunnyUI/Controls/UIDatePicker.cs | 73 ++++++++++++++++++++++++------- 4 files changed, 87 insertions(+), 60 deletions(-) diff --git a/SunnyUI/Charts/UIChart.cs b/SunnyUI/Charts/UIChart.cs index 176b3fde..8003b93a 100644 --- a/SunnyUI/Charts/UIChart.cs +++ b/SunnyUI/Charts/UIChart.cs @@ -18,6 +18,7 @@ * * 2020-06-06: V2.2.5 增加文件说明 * 2020-09-10: V2.2.7 增加图表的边框线颜色设置 + * 2022-05-14: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -267,40 +268,10 @@ namespace Sunny.UI protected void DrawTitle(Graphics g, UITitle title) { if (title == null) return; - SizeF sf = g.MeasureString(title.Text, Font); - float left = 0; - switch (title.Left) - { - 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); + Size sf = TextRenderer.MeasureText(title.Text, Font); + 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)); + 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); } protected void DrawLegend(Graphics g, UILegend legend) @@ -362,7 +333,7 @@ namespace Sunny.UI if (legend.Orient == UIOrient.Horizontal) { 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 += sf.Width; } @@ -370,7 +341,7 @@ namespace Sunny.UI if (legend.Orient == UIOrient.Vertical) { 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; } } diff --git a/SunnyUI/Charts/UIDoughnutChart.cs b/SunnyUI/Charts/UIDoughnutChart.cs index 87699144..a95b9008 100644 --- a/SunnyUI/Charts/UIDoughnutChart.cs +++ b/SunnyUI/Charts/UIDoughnutChart.cs @@ -19,6 +19,7 @@ * 2020-06-06: V2.2.5 增加文件说明 * 2021-07-22: V3.0.5 增加更新数据的方法 * 2022-07-29: V3.2.2 数据显示的小数位数重构调整至Option.DecimalPlaces + * 2022-05-14: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -180,8 +181,7 @@ namespace Sunny.UI { if (pie.Label.Position == UIPieSeriesLabelPosition.Center) { - SizeF sf = g.MeasureString(pie.Data[azIndex].Name, Font); - g.DrawString(pie.Data[azIndex].Name, Font, color, angle.Center.X - sf.Width / 2.0f, angle.Center.Y - sf.Height / 2.0f); + 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); } } } diff --git a/SunnyUI/Charts/UIPieChart.cs b/SunnyUI/Charts/UIPieChart.cs index 370c3e92..fd49738c 100644 --- a/SunnyUI/Charts/UIPieChart.cs +++ b/SunnyUI/Charts/UIPieChart.cs @@ -19,6 +19,7 @@ * 2020-06-06: V2.2.5 增加文件说明 * 2021-07-22: V3.0.5 增加更新数据的方法 * 2022-07-29: V3.2.2 数据显示的小数位数重构调整至Option.DecimalPlaces + * 2022-05-14: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -193,20 +194,32 @@ namespace Sunny.UI if (pie.Data[azIndex].Value > 0) { string text = name + pie.Data[azIndex].Value.ToString("F" + Option.DecimalPlaces); - SizeF sf = g.MeasureString(text, TempFont); - PointF pf; + Size sf = TextRenderer.MeasureText(text, TempFont); int added = 9; + float left = 0, top = 0; 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) - 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) - 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 - 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) - g.DrawString(text, TempFont, color, pf.X, pf.Y); + g.DrawString(text, TempFont, color, new Rectangle((int)left, (int)top, Width, Height), ContentAlignment.TopLeft); } } } diff --git a/SunnyUI/Controls/UIDatePicker.cs b/SunnyUI/Controls/UIDatePicker.cs index 0bb30d6b..4ac400a5 100644 --- a/SunnyUI/Controls/UIDatePicker.cs +++ b/SunnyUI/Controls/UIDatePicker.cs @@ -191,18 +191,7 @@ namespace Sunny.UI set { showType = value; - switch (value) - { - case UIDateType.YearMonthDay: - DateFormat = "yyyy-MM-dd"; - break; - case UIDateType.YearMonth: - DateFormat = "yyyy-MM"; - break; - case UIDateType.Year: - DateFormat = "yyyy"; - break; - } + Invalidate(); } } @@ -284,7 +273,19 @@ namespace Sunny.UI { if (value < new DateTime(1900, 1, 1)) value = new DateTime(1900, 1, 1); - Text = value.ToString(dateFormat); + + switch (ShowType) + { + case UIDateType.YearMonthDay: + 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) { @@ -319,8 +320,50 @@ namespace Sunny.UI set { dateFormat = value; - Text = Value.ToString(dateFormat); - MaxLength = dateFormat.Length; + + if (ShowType == UIDateType.YearMonthDay) + { + Text = Value.ToString(dateFormat); + 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; + } } } }