From decb112c21e0fb93258cc76e4e68803c1c51e179 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 13 May 2023 12:19:09 +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/Controls/DropItem/UIDateItem.cs | 38 ++++---------- SunnyUI/Controls/DropItem/UIDateTimeItem.cs | 41 ++++----------- SunnyUI/Controls/UICalendar.cs | 56 +++++---------------- SunnyUI/Controls/UIImageButton.cs | 41 +-------------- 4 files changed, 35 insertions(+), 141 deletions(-) diff --git a/SunnyUI/Controls/DropItem/UIDateItem.cs b/SunnyUI/Controls/DropItem/UIDateItem.cs index b296242f..53129fba 100644 --- a/SunnyUI/Controls/DropItem/UIDateItem.cs +++ b/SunnyUI/Controls/DropItem/UIDateItem.cs @@ -20,6 +20,7 @@ * 2020-05-29: V2.2.5 重写 * 2020-07-04: V2.2.6 重写下拉窗体,缩短创建时间 * 2023-03-26: V3.3.3 修复下拉框鼠标移动位置的Bug + * 2022-05-13: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -610,8 +611,7 @@ namespace Sunny.UI if (i == activeMonth) color = PrimaryColor; } - SizeF sf = e.Graphics.MeasureString(months[i], Font); - e.Graphics.DrawString(months[i], Font, color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(months[i], Font, color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } } @@ -676,7 +676,6 @@ namespace Sunny.UI int left = width * (i % 4); int top = height * (i / 4); - SizeF sf = e.Graphics.MeasureString(years[i].ToString(), Font); Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor; if (years[i] < min.Year || years[i] > max.Year) { @@ -692,7 +691,7 @@ namespace Sunny.UI if (years[i] != 10000) { - e.Graphics.DrawString(years[i].ToString(), Font, color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(years[i].ToString(), Font, color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } } } @@ -748,21 +747,11 @@ namespace Sunny.UI { int width = p3.Width / 7; int height = (p3.Height - 30) / 6; - - SizeF sf = e.Graphics.MeasureString(UILocalize.Sunday, Font); - e.Graphics.DrawString(UILocalize.Sunday, Font, ForeColor, width * 0 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Monday, Font); - e.Graphics.DrawString(UILocalize.Monday, Font, ForeColor, width * 1 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Tuesday, Font); - e.Graphics.DrawString(UILocalize.Tuesday, Font, ForeColor, width * 2 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Wednesday, Font); - e.Graphics.DrawString(UILocalize.Wednesday, Font, ForeColor, width * 3 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Thursday, Font); - e.Graphics.DrawString(UILocalize.Thursday, Font, ForeColor, width * 4 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Friday, Font); - e.Graphics.DrawString(UILocalize.Friday, Font, ForeColor, width * 5 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Saturday, Font); - e.Graphics.DrawString(UILocalize.Saturday, Font, ForeColor, width * 6 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); + string[] weeks = { UILocalize.Sunday, UILocalize.Monday, UILocalize.Tuesday, UILocalize.Wednesday, UILocalize.Thursday, UILocalize.Friday, UILocalize.Saturday }; + for (int i = 0; i < weeks.Length; i++) + { + e.Graphics.DrawString(weeks[i], Font, ForeColor, new Rectangle(width * i, 4, width, 19), ContentAlignment.MiddleCenter); + } e.Graphics.DrawLine(Color.DarkGray, 8, 26, 268, 26); @@ -771,8 +760,6 @@ namespace Sunny.UI { int left = width * (i % 7); int top = height * (i / 7); - - sf = e.Graphics.MeasureString(days[i].Day.ToString(), Font); Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray; color = (days[i].DateString() == date.DateString()) ? PrimaryColor : color; if (days[i] < min) color = Color.DarkGray; @@ -780,7 +767,7 @@ namespace Sunny.UI if (!maxDrawer) { - e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? PrimaryColor : color, left + (width - sf.Width) / 2, top + 30 + (height - sf.Height) / 2); + e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? PrimaryColor : color, new Rectangle(left, top + 30, width, height), ContentAlignment.MiddleCenter); } if (!maxDrawer && days[i].Date.Equals(DateTime.MaxValue.Date)) @@ -795,12 +782,7 @@ namespace Sunny.UI { e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle(p3.Width - width * 4 + 6, p3.Height - height + 3, 8, height - 10), 3); - - sf = e.Graphics.MeasureString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont); - e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, p3.Width - width * 4 + 17, p3.Height - height - 1 + (height - sf.Height) / 2.0f); - - //sf = e.Graphics.MeasureString(DateTime.Now.DateString(), Font); - //e.Graphics.DrawString(DateTime.Now.DateString(), Font, isToday ? PrimaryColor : Color.DarkGray, p3.Width - width * 4 + 55, p3.Height - height - 1 + (height - sf.Height) / 2.0f); + e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, new Rectangle(p3.Width - width * 4 + 17, p3.Height - height - 1, Width, height), ContentAlignment.MiddleLeft); } } } diff --git a/SunnyUI/Controls/DropItem/UIDateTimeItem.cs b/SunnyUI/Controls/DropItem/UIDateTimeItem.cs index 9b642526..8f12b7a9 100644 --- a/SunnyUI/Controls/DropItem/UIDateTimeItem.cs +++ b/SunnyUI/Controls/DropItem/UIDateTimeItem.cs @@ -19,6 +19,7 @@ * 2020-01-01: V2.2.0 增加文件说明 * 2020-05-29: V2.2.5 重写 * 2020-07-08: V2.2.6 重写下拉窗体,缩短创建时间 + * 2022-05-13: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -1157,15 +1158,13 @@ namespace Sunny.UI int height = p2.Height / 3; int left = width * (i % 4); int top = height * (i / 4); - - SizeF sf = e.Graphics.MeasureString(months[i], Font); if (i + 1 == Month) { - e.Graphics.DrawString(months[i], Font, PrimaryColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(months[i], Font, PrimaryColor, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } else { - e.Graphics.DrawString(months[i], Font, i == activeMonth ? PrimaryColor : ForeColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(months[i], Font, i == activeMonth ? PrimaryColor : ForeColor, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } } } @@ -1205,10 +1204,8 @@ namespace Sunny.UI int height = p1.Height / 3; int left = width * (i % 4); int top = height * (i / 4); - - SizeF sf = e.Graphics.MeasureString(years[i].ToString(), Font); Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor; - e.Graphics.DrawString(years[i].ToString(), Font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(years[i].ToString(), Font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } } @@ -1244,21 +1241,11 @@ namespace Sunny.UI { int width = p3.Width / 7; int height = (p3.Height - 30) / 6; - - SizeF sf = e.Graphics.MeasureString(UILocalize.Sunday, Font); - e.Graphics.DrawString(UILocalize.Sunday, Font, ForeColor, width * 0 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Monday, Font); - e.Graphics.DrawString(UILocalize.Monday, Font, ForeColor, width * 1 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Tuesday, Font); - e.Graphics.DrawString(UILocalize.Tuesday, Font, ForeColor, width * 2 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Wednesday, Font); - e.Graphics.DrawString(UILocalize.Wednesday, Font, ForeColor, width * 3 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Thursday, Font); - e.Graphics.DrawString(UILocalize.Thursday, Font, ForeColor, width * 4 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Friday, Font); - e.Graphics.DrawString(UILocalize.Friday, Font, ForeColor, width * 5 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Saturday, Font); - e.Graphics.DrawString(UILocalize.Saturday, Font, ForeColor, width * 6 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); + string[] weeks = { UILocalize.Sunday, UILocalize.Monday, UILocalize.Tuesday, UILocalize.Wednesday, UILocalize.Thursday, UILocalize.Friday, UILocalize.Saturday }; + for (int i = 0; i < weeks.Length; i++) + { + e.Graphics.DrawString(weeks[i], Font, ForeColor, new Rectangle(width * i, 4, width, 19), ContentAlignment.MiddleCenter); + } e.Graphics.DrawLine(Color.DarkGray, 8, 26, 268, 26); @@ -1268,13 +1255,12 @@ namespace Sunny.UI int left = width * (i % 7); int top = height * (i / 7); - sf = e.Graphics.MeasureString(days[i].Day.ToString(), Font); Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray; color = (days[i].DateString() == date.DateString()) ? PrimaryColor : color; if (!maxDrawer) { - e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? PrimaryColor : color, left + (width - sf.Width) / 2, top + 30 + (height - sf.Height) / 2); + e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? PrimaryColor : color, new Rectangle(left, top + 30, width, height), ContentAlignment.MiddleCenter); } if (!maxDrawer && days[i].Date.Equals(DateTime.MaxValue.Date)) @@ -1289,12 +1275,7 @@ namespace Sunny.UI { e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle((int)(p3.Width - width * 4 + 6), p3.Height - height + 3, 8, height - 10), 3); - - sf = e.Graphics.MeasureString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont); - e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, p3.Width - width * 4 + 17, p3.Height - height - 1 + (height - sf.Height) / 2.0f); - - //sf = e.Graphics.MeasureString(DateTime.Now.DateString(), Font); - //e.Graphics.DrawString(DateTime.Now.DateString(), Font, isToday ? PrimaryColor : Color.DarkGray, p3.Width - width * 4 + 55, p3.Height - height - 1 + (height - sf.Height) / 2.0f); + e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, new Rectangle(p3.Width - width * 4 + 17, p3.Height - height - 1, Width, height), ContentAlignment.MiddleLeft); } } } diff --git a/SunnyUI/Controls/UICalendar.cs b/SunnyUI/Controls/UICalendar.cs index 94a8cf88..b3b2e4a0 100644 --- a/SunnyUI/Controls/UICalendar.cs +++ b/SunnyUI/Controls/UICalendar.cs @@ -17,6 +17,7 @@ * 创建日期: 2022-06-28 * * 2022-06-28: V3.2.0 增加文件说明 + * 2022-05-13: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -599,15 +600,13 @@ namespace Sunny.UI int height = p2.Height / 3; int left = width * (i % 4); int top = height * (i / 4); - - SizeF sf = e.Graphics.MeasureString(months[i], Font); if (i + 1 == Month) { - e.Graphics.DrawString(months[i], Font, PrimaryColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(months[i], Font, PrimaryColor, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } else { - e.Graphics.DrawString(months[i], Font, i == activeMonth ? PrimaryColor : ForeColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(months[i], Font, i == activeMonth ? PrimaryColor : ForeColor, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } } } @@ -663,11 +662,10 @@ namespace Sunny.UI int left = width * (i % 4); int top = height * (i / 4); - SizeF sf = e.Graphics.MeasureString(years[i].ToString(), Font); Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor; if (years[i] != 10000) { - e.Graphics.DrawString(years[i].ToString(), Font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(years[i].ToString(), Font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter); } } } @@ -713,21 +711,11 @@ namespace Sunny.UI { int width = p3.Width / 7; int height = (p3.Height - 30) / 6; - - SizeF sf = e.Graphics.MeasureString(UILocalize.Sunday, Font); - e.Graphics.DrawString(UILocalize.Sunday, Font, ForeColor, width * 0 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Monday, Font); - e.Graphics.DrawString(UILocalize.Monday, Font, ForeColor, width * 1 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Tuesday, Font); - e.Graphics.DrawString(UILocalize.Tuesday, Font, ForeColor, width * 2 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Wednesday, Font); - e.Graphics.DrawString(UILocalize.Wednesday, Font, ForeColor, width * 3 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Thursday, Font); - e.Graphics.DrawString(UILocalize.Thursday, Font, ForeColor, width * 4 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Friday, Font); - e.Graphics.DrawString(UILocalize.Friday, Font, ForeColor, width * 5 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); - sf = e.Graphics.MeasureString(UILocalize.Saturday, Font); - e.Graphics.DrawString(UILocalize.Saturday, Font, ForeColor, width * 6 + (width - sf.Width) / 2, 4 + (19 - sf.Height) / 2); + string[] weeks = { UILocalize.Sunday, UILocalize.Monday, UILocalize.Tuesday, UILocalize.Wednesday, UILocalize.Thursday, UILocalize.Friday, UILocalize.Saturday }; + for (int i = 0; i < weeks.Length; i++) + { + e.Graphics.DrawString(weeks[i], Font, ForeColor, new Rectangle(width * i, 4, width, 19), ContentAlignment.MiddleCenter); + } e.Graphics.DrawLine(Color.DarkGray, 6, 26, Width - 12, 26); @@ -737,15 +725,13 @@ namespace Sunny.UI int left = width * (i % 7); int top = height * (i / 7); - sf = e.Graphics.MeasureString(days[i].Day.ToString(), Font); Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray; - bool isDate = days[i].DateString() == date.DateString(); color = isDate ? PrimaryColor : color; if (!maxDrawer) { - e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? PrimaryColor : color, left + (width - sf.Width) / 2, top + 30 + (height - sf.Height) / 2); + e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? PrimaryColor : color, new Rectangle(left, top + 30, width, height), ContentAlignment.MiddleCenter); } if (!maxDrawer && days[i].Date.Equals(DateTime.MaxValue.Date)) @@ -755,29 +741,13 @@ namespace Sunny.UI if (isDate) { - sf = e.Graphics.MeasureString("00", Font); + SizeF sf = e.Graphics.MeasureString("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)); } } - - //if (ShowToday) - //{ - // using (Font SubFont = new Font("微软雅黑", 10.5f / UIDPIScale.DPIScale())) - // { - // e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); - // e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle(p3.Width - width * 4 + 6, p3.Height - height + 3, 8, height - 10), 3); - // - // sf = e.Graphics.MeasureString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont); - // e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, p3.Width - width * 4 + 17, p3.Height - height - 1 + (height - sf.Height) / 2.0f); - // - // //sf = e.Graphics.MeasureString(DateTime.Now.DateString(), Font); - // //e.Graphics.DrawString(DateTime.Now.DateString(), Font, isToday ? PrimaryColor : Color.DarkGray, p3.Width - width * 4 + 55, p3.Height - height - 1 + (height - sf.Height) / 2.0f); - // } - //} } private int activeDay = -1; - //private bool isToday; private void p3_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { @@ -793,11 +763,9 @@ namespace Sunny.UI int x = e.Location.X / width; int y = (e.Location.Y - 30) / height; int iy = x + y * 7; - //bool istoday = ShowToday && e.Location.Y > p3.Top + p3.Height - height && e.Location.X > p3.Left + width * 3; - if (activeDay != iy)// || istoday != isToday) + if (activeDay != iy) { - //isToday = istoday; activeDay = iy; p3.Invalidate(); } diff --git a/SunnyUI/Controls/UIImageButton.cs b/SunnyUI/Controls/UIImageButton.cs index 5bd2e9c5..f49eb2ca 100644 --- a/SunnyUI/Controls/UIImageButton.cs +++ b/SunnyUI/Controls/UIImageButton.cs @@ -19,6 +19,7 @@ * 2020-01-01: V2.2.0 增加文件说明 * 2022-03-19: V3.1.1 重构主题配色 * 2023-05-04: V3.3.6 增加调用点击事件PerformClick + * 2022-05-13: V3.3.6 重构DrawString函数 ******************************************************************************/ using System; @@ -435,45 +436,7 @@ namespace Sunny.UI base.OnPaint(pe); } - SizeF sf = pe.Graphics.MeasureString(Text, Font); - switch (TextAlign) - { - case ContentAlignment.TopLeft: - pe.Graphics.DrawString(text, Font, ForeColor, Padding.Left, Padding.Top); - break; - - case ContentAlignment.TopCenter: - pe.Graphics.DrawString(text, Font, ForeColor, (Width - sf.Width) / 2, Padding.Top); - break; - - case ContentAlignment.TopRight: - pe.Graphics.DrawString(text, Font, ForeColor, Width - Padding.Right - sf.Width, Padding.Top); - break; - - case ContentAlignment.MiddleLeft: - pe.Graphics.DrawString(text, Font, ForeColor, Padding.Left, (Height - sf.Height) / 2); - break; - - case ContentAlignment.MiddleCenter: - pe.Graphics.DrawString(text, Font, ForeColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2); - break; - - case ContentAlignment.MiddleRight: - pe.Graphics.DrawString(text, Font, ForeColor, Width - Padding.Right - sf.Width, (Height - sf.Height) / 2); - break; - - case ContentAlignment.BottomLeft: - pe.Graphics.DrawString(text, Font, ForeColor, Padding.Left, Height - Padding.Bottom - sf.Height); - break; - - case ContentAlignment.BottomCenter: - pe.Graphics.DrawString(text, Font, ForeColor, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height); - break; - - case ContentAlignment.BottomRight: - pe.Graphics.DrawString(text, Font, ForeColor, Width - Padding.Right - sf.Width, Height - Padding.Bottom - sf.Height); - break; - } + pe.Graphics.DrawString(text, Font, ForeColor, new Rectangle(Padding.Left, Padding.Top, Width - Padding.Left - Padding.Right, Height - Padding.Top - Padding.Bottom), TextAlign); } } } \ No newline at end of file