From c5e5a11188ab504863b175aac5e3ec1643032d62 Mon Sep 17 00:00:00 2001 From: Sunny Date: Thu, 17 Apr 2025 13:48:50 +0800 Subject: [PATCH] =?UTF-8?q?*=20UINavMenu:=20=E5=A2=9E=E5=8A=A0=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=96=87=E5=AD=97=E5=B1=85=E4=B8=AD=E7=9A=84=E5=B1=9E?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UINavMenu.cs | 41 ++++++++++++++++++++++++++--- SunnyUI/Controls/UINavMenuHelper.cs | 7 +++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/SunnyUI/Controls/UINavMenu.cs b/SunnyUI/Controls/UINavMenu.cs index 625766f0..9f1afa6b 100644 --- a/SunnyUI/Controls/UINavMenu.cs +++ b/SunnyUI/Controls/UINavMenu.cs @@ -39,6 +39,7 @@ * 2023-11-16: V3.5.2 重构主题 * 2024-04-13: V3.6.5 修复通过代码设置背景色无效的问题 * 2024-05-17: V3.6.6 防止控件闪烁 +* 2025-04-17: V3.8.2 增加节点文字居中的属性 ******************************************************************************/ using System; @@ -106,6 +107,22 @@ namespace Sunny.UI selectedHighColor = UIStyles.Blue.NavMenuMenuSelectedColor; } + private NodeTextAlign _nodeTextAlign = NodeTextAlign.Left; + + [DefaultValue(NodeTextAlign.Left), Category("SunnyUI"), Description("节点文字显示位置")] + public NodeTextAlign NodeTextAlign + { + get => _nodeTextAlign; + set + { + if (_nodeTextAlign != value) + { + _nodeTextAlign = value; + Invalidate(); + } + } + } + protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); @@ -697,13 +714,25 @@ namespace Sunny.UI e.Graphics.FillRectangle(SelectedColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); } - e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleLeft); + if (NodeTextAlign == NodeTextAlign.Left) + e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleLeft); + if (NodeTextAlign == NodeTextAlign.TextAreaCenter) + e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleCenter); + if (NodeTextAlign == NodeTextAlign.Center) + e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, ItemHeight), ContentAlignment.MiddleCenter); + e.Graphics.FillRectangle(SelectedHighColor, new Rectangle(0, e.Bounds.Y, 4, e.Bounds.Height)); } else if (e.Node == CurrentNode && (e.State & TreeNodeStates.Hot) != 0) { e.Graphics.FillRectangle(HoverColor, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); - e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleLeft); + + if (NodeTextAlign == NodeTextAlign.Left) + e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleLeft); + if (NodeTextAlign == NodeTextAlign.TextAreaCenter) + e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleCenter); + if (NodeTextAlign == NodeTextAlign.Center) + e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, ItemHeight), ContentAlignment.MiddleCenter); } else { @@ -714,7 +743,13 @@ namespace Sunny.UI } e.Graphics.FillRectangle(color, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); - e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleLeft); + + if (NodeTextAlign == NodeTextAlign.Left) + e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleLeft); + if (NodeTextAlign == NodeTextAlign.TextAreaCenter) + e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(drawLeft, e.Bounds.Y, e.Bounds.Width - drawLeft, ItemHeight), ContentAlignment.MiddleCenter); + if (NodeTextAlign == NodeTextAlign.Center) + e.Graphics.DrawString(e.Node.Text, Font, ForeColor, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, ItemHeight), ContentAlignment.MiddleCenter); } //画右侧图标 diff --git a/SunnyUI/Controls/UINavMenuHelper.cs b/SunnyUI/Controls/UINavMenuHelper.cs index 4c8ac2df..f766e574 100644 --- a/SunnyUI/Controls/UINavMenuHelper.cs +++ b/SunnyUI/Controls/UINavMenuHelper.cs @@ -205,6 +205,13 @@ namespace Sunny.UI } } + public enum NodeTextAlign + { + Left, + Center, + TextAreaCenter + } + public class NavMenuItem : ISymbol { public string Text { get; set; }