Files
lserp_cs_6.0/插件库/Lskj.Main/Control/Menu_ItemNew.cs
T
2026-07-02 10:11:39 +00:00

151 lines
4.8 KiB
C#

/******************************
* Menu_ItemNew 1.2.2 - fixed badge & hover
******************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Lskj.Business;
using Lskj.Business.Impl;
namespace Lskj.Main.Control
{
public partial class Menu_ItemNew : UserControl
{
/* —— 样式 —— */
private readonly Color _hoverBorder = Color.FromArgb(211, 211, 211); // 淡灰色
private readonly Color _hoverBack = Color.White;
private readonly Color _normalBack = Color.FromArgb(240, 240, 240);
/* —— 状态 —— */
private bool _isHover;
private Image _imgDefault, _imgHover, _imgBorderHover;
private int _radius = 10;
/* —— 外部信息 —— */
public int menuRow;
public int menuGroup;
public string menuNode;
public Menu_ItemNew()
{
InitializeComponent();
/* 统一绑定悬停事件 */
BindHover(this);
BindHover(btn_icon);
BindHover(lbl_title);
BindHover(lbl_sub);
BindHover(badgeDot);
BindHover(badgeNum);
/* 统一绑定 Click —— 1 行即可 */
BindClick(btn_icon);
BindClick(lbl_title);
BindClick(lbl_sub);
BindClick(badgeDot);
}
private void BindClick(System.Windows.Forms.Control ctl)
{
ctl.Click += (s, e) =>
{
// 直接把 Click 冒泡给 Menu_ItemNew 本身
this.OnClick(e); // this 就是 Menu_ItemNew
};
}
/* ===== 对外 API ===== */
public void SetMenuCaption(string text)
{ lbl_title.Text = text; toolTip1.SetToolTip(lbl_title, text); }
public void SetSubCaption(string text) => lbl_sub.Text = text;
public void SetMenuImg(Image img)
{ _imgDefault = img; btn_icon.BackgroundImage = img; }
public void SetMenuHoverImg(Image img) => _imgHover = img;
public void SetMenuHoverBorderImg(Image img) => _imgBorderHover = img;
public void SetMenuNum(int num)
{
if (SystemInfo.Instance.DisableShowNotify || num < 1)
{
badgeDot.Visible = false;
return;
}
badgeDot.Number = num; // 一行搞定
badgeDot.Visible = true;
RepositionBadge();
}
/* ===== 圆角属性(如需保留) ===== */
[Browsable(true), Description("圆角半径(px)")]
public int CornerRadius
{
get => _radius;
set { _radius = Math.Max(0, value); Invalidate(); }
}
/* —— 兼容旧写法 —— */
public int SetRoundRadius
{
get => CornerRadius;
set => CornerRadius = value;
}
public Button GetButton() => btn_icon;
public Label GetLabelTitle() => lbl_title;
public Label GetLabelTitleSub() => lbl_sub;
/* ===== 悬停逻辑 ===== */
private void HoverEnter(object s, EventArgs e)
{
if (_isHover) return;
_isHover = true;
//BackColor = _hoverBack;
btn_icon.BackgroundImage = _imgHover ?? _imgDefault;
if (_imgBorderHover != null)
{
this.BackgroundImage = _imgBorderHover;
}
Cursor = Cursors.Hand;
Invalidate();
Update();
}
private void HoverLeave(object s, EventArgs e)
{
if (ClientRectangle.Contains(PointToClient(System.Windows.Forms.Control.MousePosition))) return;
_isHover = false;
//BackColor = _normalBack;
btn_icon.BackgroundImage = _imgDefault;
this.BackgroundImage = null;
Invalidate();
Update();
}
private void BindHover(System.Windows.Forms.Control ctl)
{
ctl.MouseEnter += HoverEnter;
ctl.MouseLeave += HoverLeave;
}
/* ===== 角标定位 ===== */
protected override void OnResize(EventArgs e)
{ base.OnResize(e); RepositionBadge(); }
private void RepositionBadge()
{
if (!badgeDot.Visible) return;
const int SAFE = 2; // 距离边缘 2px
//badgeDot.Location = new Point(
// Math.Min(btn_icon.Right - badgeDot.Width / 2, Width - badgeDot.Width - SAFE),
// Math.Max(btn_icon.Top - badgeDot.Height / 2, SAFE));
/* 数字已 Dock/Size 居中,无需再调位置,但保证层级 */
badgeDot.BringToFront();
badgeNum.BringToFront();
}
}
}