* UINavMenu: 增加节点文字居中的属性

This commit is contained in:
Sunny 2025-04-17 13:48:50 +08:00
parent 8bf338e488
commit c5e5a11188
2 changed files with 45 additions and 3 deletions

View File

@ -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)));
}
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)));
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)));
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);
}
//画右侧图标

View File

@ -205,6 +205,13 @@ namespace Sunny.UI
}
}
public enum NodeTextAlign
{
Left,
Center,
TextAreaCenter
}
public class NavMenuItem : ISymbol
{
public string Text { get; set; }