diff --git a/SunnyUI/Common/UGraphics.cs b/SunnyUI/Common/UGraphics.cs index 58a06092..94061bf4 100644 --- a/SunnyUI/Common/UGraphics.cs +++ b/SunnyUI/Common/UGraphics.cs @@ -33,36 +33,74 @@ namespace Sunny.UI /// public static class GraphicsEx { - /// - /// 绘制字符串 - /// - /// 绘图图元 - /// 文字 - /// 字体 - /// 颜色 - /// 区域 - /// 格式 - public static void DrawString(this Graphics g, string text, Font font, Color color, RectangleF rect, StringFormat format) + public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, ContentAlignment alignment = ContentAlignment.MiddleCenter) { if (text.IsNullOrEmpty()) return; - using Brush br = color.Brush(); - g.DrawString(text, font, br, rect, format); + TextFormatFlags flags = new TextFormatFlags(); + switch (alignment) + { + case ContentAlignment.TopLeft: + flags = TextFormatFlags.Top | TextFormatFlags.Left; + break; + case ContentAlignment.TopCenter: + flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; + break; + case ContentAlignment.TopRight: + flags = TextFormatFlags.Top | TextFormatFlags.Right; + break; + case ContentAlignment.MiddleLeft: + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left; + break; + case ContentAlignment.MiddleCenter: + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter; + break; + case ContentAlignment.MiddleRight: + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; + break; + case ContentAlignment.BottomLeft: + flags = TextFormatFlags.Bottom | TextFormatFlags.Left; + break; + case ContentAlignment.BottomCenter: + flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; + break; + case ContentAlignment.BottomRight: + flags = TextFormatFlags.Bottom | TextFormatFlags.Right; + break; + default: + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter; + break; + } + + TextRenderer.DrawText(g, text, font, rect, color, flags); } - /// - /// 绘制字符串 - /// - /// 绘图图元 - /// 文字 - /// 字体 - /// 颜色 - /// 区域 - /// 格式 - public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, StringFormat format) + public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment) { if (text.IsNullOrEmpty()) return; - using Brush br = color.Brush(); - g.DrawString(text, font, br, rect, format); + TextFormatFlags flags = new TextFormatFlags(); + + if (alignment == StringAlignment.Near && lineAlignment == StringAlignment.Near) + flags = TextFormatFlags.Top | TextFormatFlags.Left; + if (alignment == StringAlignment.Center && lineAlignment == StringAlignment.Near) + flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; + if (alignment == StringAlignment.Far && lineAlignment == StringAlignment.Near) + flags = TextFormatFlags.Top | TextFormatFlags.Right; + + if (alignment == StringAlignment.Near && lineAlignment == StringAlignment.Center) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left; + if (alignment == StringAlignment.Center && lineAlignment == StringAlignment.Center) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter; + if (alignment == StringAlignment.Far && lineAlignment == StringAlignment.Center) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; + + if (alignment == StringAlignment.Near && lineAlignment == StringAlignment.Far) + flags = TextFormatFlags.Bottom | TextFormatFlags.Left; + if (alignment == StringAlignment.Center && lineAlignment == StringAlignment.Far) + flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; + if (alignment == StringAlignment.Far && lineAlignment == StringAlignment.Far) + flags = TextFormatFlags.Bottom | TextFormatFlags.Right; + + TextRenderer.DrawText(g, text, font, rect, color, flags); } /// diff --git a/SunnyUI/Controls/Color/UILabelRotate.cs b/SunnyUI/Controls/Color/UILabelRotate.cs index 0e18325a..db33049f 100644 --- a/SunnyUI/Controls/Color/UILabelRotate.cs +++ b/SunnyUI/Controls/Color/UILabelRotate.cs @@ -149,7 +149,7 @@ namespace Sunny.UI { if (TextAngle.EqualsFloat(0)) { - e.Graphics.DrawString(Text, Font, ForeColor, r, format); + e.Graphics.DrawString(Text, Font, ForeColor, r, TextAlign); } else { diff --git a/SunnyUI/Controls/UIListBoxEx.cs b/SunnyUI/Controls/UIListBoxEx.cs index df60bd23..8cf7161c 100644 --- a/SunnyUI/Controls/UIListBoxEx.cs +++ b/SunnyUI/Controls/UIListBoxEx.cs @@ -300,10 +300,6 @@ namespace Sunny.UI return; } - StringFormat sStringFormat = new StringFormat(); - sStringFormat.LineAlignment = StringAlignment.Center; - sStringFormat.Alignment = textAlignment; - bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected; Color backColor = isSelected ? ItemSelectBackColor : BackColor; Color foreColor = isSelected ? ItemSelectForeColor : ForeColor; @@ -315,7 +311,8 @@ namespace Sunny.UI { e.Graphics.FillRectangle(BackColor, e.Bounds); e.Graphics.FillRectangle(backColor, rect); - e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat); + //e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat); + e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, textAlignment, StringAlignment.Center); } else { @@ -333,7 +330,8 @@ namespace Sunny.UI e.Graphics.FillRectangle(BackColor, e.Bounds); e.Graphics.FillRectangle(backColor, rect); - e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat); + //e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat); + e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, textAlignment, StringAlignment.Center); } AfterDrawItem?.Invoke(this, Items, e); diff --git a/SunnyUI/Controls/UINavMenu.cs b/SunnyUI/Controls/UINavMenu.cs index dd245665..6cdfd12e 100644 --- a/SunnyUI/Controls/UINavMenu.cs +++ b/SunnyUI/Controls/UINavMenu.cs @@ -756,27 +756,26 @@ namespace Sunny.UI //显示Tips圆圈 if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid()) { - SizeF tipsSize = e.Graphics.MeasureString(MenuHelper.GetTipsText(e.Node), TempFont); - float sfMax = Math.Max(tipsSize.Width, tipsSize.Height) + 1; - float tipsLeft = Width - sfMax - 16; + Size tipsSize = TextRenderer.MeasureText(MenuHelper.GetTipsText(e.Node), TempFont); + int sfMax = Math.Max(tipsSize.Width, tipsSize.Height) + 1; + int tipsLeft = Width - sfMax - 16; if (e.Node.Nodes.Count > 0) tipsLeft -= 24; if (Bar.Visible) tipsLeft -= Bar.Width; if (TreeNodeSymbols.ContainsKey(e.Node)) tipsLeft -= TreeNodeSymbols[e.Node].Count * 30; - - float tipsTop = e.Bounds.Y + (ItemHeight - sfMax) / 2; + int tipsTop = e.Bounds.Y + (ItemHeight - sfMax) / 2; if (MenuHelper[e.Node] != null) { using StringFormat alignment = GDI.SetCenterAlignment(); if (MenuHelper[e.Node].TipsCustom) { - e.Graphics.FillEllipse(MenuHelper[e.Node].TipsBackColor, tipsLeft, tipsTop, sfMax, sfMax); - e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, MenuHelper[e.Node].TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); + e.Graphics.FillEllipse(MenuHelper[e.Node].TipsBackColor, tipsLeft - 1, tipsTop, sfMax, sfMax); + e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, MenuHelper[e.Node].TipsForeColor, new Rectangle(tipsLeft, tipsTop, sfMax, sfMax)); } else { - e.Graphics.FillEllipse(TipsColor, tipsLeft, tipsTop, sfMax, sfMax); - e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); + e.Graphics.FillEllipse(TipsColor, tipsLeft - 1, tipsTop, sfMax, sfMax); + e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, TipsForeColor, new Rectangle(tipsLeft, tipsTop, sfMax, sfMax)); } } }