* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-12 13:54:24 +08:00
parent 982e50761e
commit 0101bad8c5
3 changed files with 13 additions and 6 deletions

View File

@ -20,6 +20,7 @@
* 2021-06-19: V3.0.4 * 2021-06-19: V3.0.4
* 2021-08-07: V3.0.5 线 * 2021-08-07: V3.0.5 线
* 2022-05-15: V3.1.8 * 2022-05-15: V3.1.8
* 2022-05-12: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -89,8 +90,9 @@ namespace Sunny.UI
/// <param name="path">绘图路径</param> /// <param name="path">绘图路径</param>
protected override void OnPaintFore(Graphics g, GraphicsPath path) protected override void OnPaintFore(Graphics g, GraphicsPath path)
{ {
SizeF sf = g.MeasureString(Text, Font); //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, Width / 2 - sf.Width / 2, Height / 2 - sf.Height / 2);
g.DrawString(Text, Font, ForeColor, ClientRectangle, TextAlign);
} }
[DefaultValue(500), Description("显示间隔"), Category("SunnyUI")] [DefaultValue(500), Description("显示间隔"), Category("SunnyUI")]

View File

@ -18,6 +18,7 @@
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2022-08-11: V3.0.2 ItemSize * 2022-08-11: V3.0.2 ItemSize
* 2022-05-12: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -377,7 +378,8 @@ namespace Sunny.UI
// 绘制标题 // 绘制标题
Color textColor = index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor; 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, 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);
// 绘制图标 // 绘制图标
if (ImageList != null) if (ImageList != null)

View File

@ -45,6 +45,7 @@
* 2022-11-30: V3.3.0 RemoveAllPages函数 * 2022-11-30: V3.3.0 RemoveAllPages函数
* 2023-01-25: V3.3.1 * 2023-01-25: V3.3.1
* 2023-02-24: V3.3.2 PageSelected可能未显示选中页面的问题 * 2023-02-24: V3.3.2 PageSelected可能未显示选中页面的问题
* 2022-05-12: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -1242,14 +1243,16 @@ namespace Sunny.UI
} }
} }
SizeF sf = e.Graphics.MeasureString(Text, TitleFont); //SizeF sf = e.Graphics.MeasureString(Text, TitleFont);
if (TextAlignment == StringAlignment.Center) if (TextAlignment == StringAlignment.Center)
{ {
e.Graphics.DrawString(Text, TitleFont, titleForeColor, (Width - sf.Width) / 2, (TitleHeight - sf.Height) / 2); //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 else
{ {
e.Graphics.DrawString(Text, TitleFont, titleForeColor, 6 + (ShowTitleIcon && Icon != null ? 26 : 0), (TitleHeight - sf.Height) / 2); //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);
} }
} }