* UINavMenu: 选中项可设置背景色渐变

This commit is contained in:
Sunny 2021-12-13 21:18:03 +08:00
parent b606394638
commit c8191e67c3
5 changed files with 163 additions and 66 deletions

Binary file not shown.

View File

@ -18,6 +18,7 @@
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2020-04-25: V2.2.4 * 2020-04-25: V2.2.4
* 2021-12-13: V3.0.9 线12
******************************************************************************/ ******************************************************************************/
using System; using System;

View File

@ -22,6 +22,7 @@
* 2021-06-14: V3.0.4 * 2021-06-14: V3.0.4
* 2021-08-07: V3.0.5 / * 2021-08-07: V3.0.5 /
* 2021-08-27: V3.0.6 TipsText显示的颜色 * 2021-08-27: V3.0.6 TipsText显示的颜色
* 2021-12-13: V3.0.9
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -29,6 +30,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms; using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
@ -291,6 +293,53 @@ namespace Sunny.UI
} }
} }
private bool fillColorGradient;
[Description("填充颜色渐变"), Category("SunnyUI")]
[DefaultValue(false)]
public bool SelectedColorGradient
{
get => fillColorGradient;
set
{
if (fillColorGradient != value)
{
fillColorGradient = value;
Invalidate();
}
}
}
/// <summary>
/// 设置填充颜色
/// </summary>
/// <param name="value">颜色</param>
private void SetFillColor2(Color value)
{
if (selectedColor2 != value)
{
selectedColor2 = value;
menuStyle = UIMenuStyle.Custom;
Invalidate();
}
}
/// <summary>
/// 填充颜色
/// </summary>
private Color selectedColor2 = Color.FromArgb(36, 36, 36);
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("填充颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "36, 36, 36")]
public Color SelectedColor2
{
get => selectedColor2;
set => SetFillColor2(value);
}
private Color selectedHighColor = UIColor.Blue; private Color selectedHighColor = UIColor.Blue;
/// <summary> /// <summary>
@ -372,6 +421,7 @@ namespace Sunny.UI
BackColor = uiColor.BackColor; BackColor = uiColor.BackColor;
fillColor = uiColor.BackColor; fillColor = uiColor.BackColor;
selectedColor = uiColor.SelectedColor; selectedColor = uiColor.SelectedColor;
selectedColor2 = uiColor.SelectedColor2;
foreColor = uiColor.UnSelectedForeColor; foreColor = uiColor.UnSelectedForeColor;
hoverColor = uiColor.HoverColor; hoverColor = uiColor.HoverColor;
secondBackColor = uiColor.SecondBackColor; secondBackColor = uiColor.SecondBackColor;
@ -556,8 +606,26 @@ namespace Sunny.UI
SizeF sf = e.Graphics.MeasureString(e.Node.Text, Font); SizeF sf = e.Graphics.MeasureString(e.Node.Text, Font);
if (e.Node == SelectedNode) if (e.Node == SelectedNode)
{ {
e.Graphics.FillRectangle((e.State & TreeNodeStates.Hot) != 0 ? HoverColor : SelectedColor, if (SelectedColorGradient)
new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height))); {
if ((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)));
}
else
{
LinearGradientBrush br = new LinearGradientBrush(new Point(0, 0), new Point(0, e.Node.Bounds.Height), SelectedColor, SelectedColor2);
br.GammaCorrection = true;
e.Graphics.FillRectangle(br, new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(Width, e.Node.Bounds.Height)));
br.Dispose();
}
}
else
{
e.Graphics.FillRectangle((e.State & TreeNodeStates.Hot) != 0 ? HoverColor : 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, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f); e.Graphics.DrawString(e.Node.Text, Font, SelectedForeColor, drawLeft, e.Bounds.Y + (ItemHeight - sf.Height) / 2.0f);
e.Graphics.FillRectangle(SelectedHighColor, new Rectangle(0, e.Bounds.Y, 4, e.Bounds.Height)); e.Graphics.FillRectangle(SelectedHighColor, new Rectangle(0, e.Bounds.Y, 4, e.Bounds.Height));

View File

@ -21,6 +21,7 @@
* 2021-05-09: V3.0.3 * 2021-05-09: V3.0.3
* 2021-09-03: V3.0.6 * 2021-09-03: V3.0.6
* 2021-12-11: V3.0.9 * 2021-12-11: V3.0.9
* 2021-12-13: V3.0.9 线12
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -371,7 +372,7 @@ namespace Sunny.UI
if (IsDisposed) return; if (IsDisposed) return;
Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1); Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
GraphicsPath path = rect.CreateRoundedRectanglePath(radius, RadiusSides); GraphicsPath path = rect.CreateRoundedRectanglePath(radius, RadiusSides, RectSize);
//填充背景色 //填充背景色
if (BackgroundImage == null && ShowFill && fillColor.IsValid()) if (BackgroundImage == null && ShowFill && fillColor.IsValid())
@ -406,8 +407,12 @@ namespace Sunny.UI
protected virtual void OnPaintRect(Graphics g, GraphicsPath path) protected virtual void OnPaintRect(Graphics g, GraphicsPath path)
{ {
Color color = GetRectColor(); g.DrawPath(GetRectColor(), path, true, RectSize);
PaintRectDisableSides(g);
}
private void PaintRectDisableSides(Graphics g)
{
//IsRadius为False时显示左侧边线 //IsRadius为False时显示左侧边线
bool ShowRectLeft = RectSides.GetValue(ToolStripStatusLabelBorderSides.Left); bool ShowRectLeft = RectSides.GetValue(ToolStripStatusLabelBorderSides.Left);
//IsRadius为False时显示上侧边线 //IsRadius为False时显示上侧边线
@ -426,89 +431,86 @@ namespace Sunny.UI
//IsRadius为True时显示右下圆角 //IsRadius为True时显示右下圆角
bool RadiusRightBottom = RadiusSides.GetValue(UICornerRadiusSides.RightBottom); bool RadiusRightBottom = RadiusSides.GetValue(UICornerRadiusSides.RightBottom);
if (RadiusSides == UICornerRadiusSides.None) var ShowRadius = RadiusSides > 0;//肯定少有一个角显示圆角
g.DrawRectangle(new Pen(color), 0, 0, Width - 1, Height - 1);
else
g.DrawPath(color, path);
using (Pen pen = new Pen(fillColor)) if (!ShowRadius || (!RadiusLeftBottom && !RadiusLeftTop))
using (Pen penR = new Pen(rectColor))
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusLeftTop)) g.DrawLine(GetRectColor(), 0, 0, 0, Height - 1);
{ }
g.DrawLine(penR, 0, 0, 0, Height - 1);
}
if (!ShowRadius || (ShowRadius && !RadiusRightTop && !RadiusLeftTop)) if (!ShowRadius || (!RadiusRightTop && !RadiusLeftTop))
{ {
g.DrawLine(penR, 0, 0, Width - 1, 0); g.DrawLine(GetRectColor(), 0, 0, Width - 1, 0);
} }
if (!ShowRadius || (ShowRadius && !RadiusRightTop && !RadiusRightBottom)) if (!ShowRadius || (!RadiusRightTop && !RadiusRightBottom))
{ {
g.DrawLine(penR, Width - 1, 0, Width - 1, Height - 1); g.DrawLine(GetRectColor(), Width - 1, 0, Width - 1, Height - 1);
} }
if (!ShowRectLeft) if (!ShowRadius || (!RadiusLeftBottom && !RadiusRightBottom))
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusLeftTop)) g.DrawLine(GetRectColor(), 0, Height - 1, Width - 1, Height - 1);
{ }
g.DrawLine(pen, 0, 1, 0, Height - 2);
}
}
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusRightBottom)) if (!ShowRectLeft)
{
if (!ShowRadius || (!RadiusLeftBottom && !RadiusLeftTop))
{ {
g.DrawLine(penR, 0, Height - 1, Width - 1, Height - 1); g.DrawLine(GetFillColor(), 0, RectSize, 0, Height - 1 - RectSize);
if (RectSize == 2) g.DrawLine(GetFillColor(), 1, RectSize, 1, Height - 1 - RectSize);
} }
}
if (!ShowRectTop) if (!ShowRectTop)
{
if (!ShowRadius || (!RadiusRightTop && !RadiusLeftTop))
{ {
if (!ShowRadius || (ShowRadius && !RadiusRightTop && !RadiusLeftTop)) g.DrawLine(GetFillColor(), RectSize, 0, Width - 1 - RectSize, 0);
{ if (RectSize == 2) g.DrawLine(GetFillColor(), RectSize, 1, Width - 1 - RectSize, 1);
g.DrawLine(pen, 1, 0, Width - 2, 0);
}
} }
}
if (!ShowRectRight) if (!ShowRectRight)
{
if (!ShowRadius || (!RadiusRightTop && !RadiusRightBottom))
{ {
if (!ShowRadius || (ShowRadius && !RadiusRightTop && !RadiusRightBottom)) g.DrawLine(GetFillColor(), Width - 1, RectSize, Width - 1, Height - 1 - RectSize);
{ if (RectSize == 2) g.DrawLine(GetFillColor(), Width - 2, RectSize, Width - 2, Height - 1 - RectSize);
g.DrawLine(pen, Width - 1, 1, Width - 1, Height - 2);
}
} }
}
if (!ShowRectBottom) if (!ShowRectBottom)
{
if (!ShowRadius || (!RadiusLeftBottom && !RadiusRightBottom))
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusRightBottom)) g.DrawLine(GetFillColor(), RectSize, Height - 1, Width - 1 - RectSize, Height - 1);
{ if (RectSize == 2) g.DrawLine(GetFillColor(), RectSize, Height - 2, Width - 1 - RectSize, Height - 2);
g.DrawLine(pen, 1, Height - 1, Width - 2, Height - 1);
}
} }
}
if (!ShowRectLeft && !ShowRectTop) if (!ShowRectLeft && !ShowRectTop)
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusLeftTop)) if (!ShowRadius || (!RadiusLeftBottom && !RadiusLeftTop))
g.DrawLine(pen, 0, 0, 0, 1); g.FillRectangle(GetFillColor(), 0, 0, RectSize, RectSize);
} }
if (!ShowRectRight && !ShowRectTop) if (!ShowRectRight && !ShowRectTop)
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusLeftTop)) if (!ShowRadius || (!RadiusLeftBottom && !RadiusLeftTop))
g.DrawLine(pen, Width - 1, 0, Width - 1, 1); g.FillRectangle(GetFillColor(), Width - 1 - RectSize, 0, RectSize, RectSize);
} }
if (!ShowRectLeft && !ShowRectBottom) if (!ShowRectLeft && !ShowRectBottom)
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusLeftTop)) if (!ShowRadius || (!RadiusLeftBottom && !RadiusLeftTop))
g.DrawLine(pen, 0, Height - 1, 0, Height - 2); g.FillRectangle(GetFillColor(), 0, Height - 1 - RectSize, RectSize, RectSize);
} }
if (!ShowRectRight && !ShowRectBottom) if (!ShowRectRight && !ShowRectBottom)
{ {
if (!ShowRadius || (ShowRadius && !RadiusLeftBottom && !RadiusLeftTop)) if (!ShowRadius || (!RadiusLeftBottom && !RadiusLeftTop))
g.DrawLine(pen, Width - 1, Height - 1, Width - 1, Height - 2); g.FillRectangle(GetFillColor(), Width - 1 - RectSize, Height - 1 - RectSize, RectSize, RectSize);
}
} }
} }
@ -695,5 +697,28 @@ namespace Sunny.UI
public delegate void OnTextAlignmentChange(object sender, ContentAlignment alignment); public delegate void OnTextAlignmentChange(object sender, ContentAlignment alignment);
public event OnTextAlignmentChange TextAlignmentChange; public event OnTextAlignmentChange TextAlignmentChange;
private int rectSize = 1;
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框宽度"), Category("SunnyUI")]
[DefaultValue(1)]
public int RectSize
{
get => rectSize;
set
{
int v = value;
if (v > 2) v = 2;
if (v < 1) v = 1;
if (rectSize != v)
{
rectSize = v;
Invalidate();
}
}
}
} }
} }

View File

@ -52,9 +52,11 @@ namespace Sunny.UI
public abstract UIMenuStyle Style { get; } public abstract UIMenuStyle Style { get; }
public virtual Color BackColor => Color.FromArgb(56, 56, 56); public virtual Color BackColor => Color.FromArgb(56, 56, 56);
public virtual Color SelectedColor => Color.FromArgb(36, 36, 36); public virtual Color SelectedColor => Color.FromArgb(36, 36, 36);
public virtual Color SelectedColor2 => Color.FromArgb(36, 36, 36);
public virtual Color UnSelectedForeColor => Color.FromArgb(240, 240, 240); public virtual Color UnSelectedForeColor => Color.FromArgb(240, 240, 240);
public virtual Color HoverColor => Color.FromArgb(76, 76, 76); public virtual Color HoverColor => Color.FromArgb(76, 76, 76);
public virtual Color SecondBackColor => Color.FromArgb(66, 66, 66); public virtual Color SecondBackColor => Color.FromArgb(66, 66, 66);
public override string ToString() public override string ToString()
{ {
return Style.DisplayText(); return Style.DisplayText();
@ -76,6 +78,7 @@ namespace Sunny.UI
public override UIMenuStyle Style => UIMenuStyle.White; public override UIMenuStyle Style => UIMenuStyle.White;
public override Color BackColor => Color.FromArgb(240, 240, 240); public override Color BackColor => Color.FromArgb(240, 240, 240);
public override Color SelectedColor => Color.FromArgb(250, 250, 250); public override Color SelectedColor => Color.FromArgb(250, 250, 250);
public override Color SelectedColor2 => Color.FromArgb(250, 250, 250);
public override Color UnSelectedForeColor => UIFontColor.Primary; public override Color UnSelectedForeColor => UIFontColor.Primary;
public override Color HoverColor => Color.FromArgb(230, 230, 230); public override Color HoverColor => Color.FromArgb(230, 230, 230);
public override Color SecondBackColor => Color.FromArgb(235, 235, 235); public override Color SecondBackColor => Color.FromArgb(235, 235, 235);