(八十三)c#Winform自定义控件-导航菜单(扩展)
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
来都来了,点个【推荐】再走吧,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
以上为demo效果,你使用此控件可以实现以下弹出效果
准备工作
没什么准备的
开始
添加一个类NavigationMenuItemExt 继承NavigationMenuItemBase
public class NavigationMenuItemExt : NavigationMenuItemBase
{
public System.Windows.Forms.Control ShowControl { get; set; }
}
添加一个用户控件UCNavigationMenuExt
添加属性
/// <summary>
/// Occurs when [click itemed].
/// </summary>
[Description("点击节点事件"), Category("自定义")] public event EventHandler ClickItemed;
/// <summary>
/// The select item
/// </summary>
private NavigationMenuItemExt selectItem = null; /// <summary>
/// Gets the select item.
/// </summary>
/// <value>The select item.</value>
[Description("选中的节点"), Category("自定义")]
public NavigationMenuItemExt SelectItem
{
get { return selectItem; }
private set { selectItem = value; }
} /// <summary>
/// The items
/// </summary>
NavigationMenuItemExt[] items; /// <summary>
/// Gets or sets the items.
/// </summary>
/// <value>The items.</value>
[Description("节点列表"), Category("自定义")]
public NavigationMenuItemExt[] Items
{
get { return items; }
set
{
items = value;
ReloadMenu();
}
}
/// <summary>
/// The tip color
/// </summary>
private Color tipColor = Color.FromArgb(, , ); /// <summary>
/// Gets or sets the color of the tip.
/// </summary>
/// <value>The color of the tip.</value>
[Description("角标颜色"), Category("自定义")]
public Color TipColor
{
get { return tipColor; }
set { tipColor = value; }
} /// <summary>
/// 获取或设置控件的前景色。
/// </summary>
/// <value>The color of the fore.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
foreach (Control c in this.Controls)
{
c.ForeColor = value;
}
}
}
/// <summary>
/// 获取或设置控件显示的文字的字体。
/// </summary>
/// <value>The font.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
foreach (Control c in this.Controls)
{
c.Font = value;
}
}
} /// <summary>
/// The m LST anchors
/// </summary>
Dictionary<NavigationMenuItemExt, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItemExt, FrmAnchor>();
加载和绘图
private void ReloadMenu()
{
try
{
ControlHelper.FreezeControl(this, true);
this.Controls.Clear();
if (items != null && items.Length > )
{
foreach (var item in items)
{
var menu = (NavigationMenuItemExt)item;
Label lbl = new Label();
lbl.AutoSize = false;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Width = menu.ItemWidth;
lbl.Text = menu.Text; lbl.Font = Font;
lbl.ForeColor = ForeColor; lbl.Paint += lbl_Paint;
lbl.MouseEnter += lbl_MouseEnter;
lbl.Tag = menu;
lbl.Click += lbl_Click;
if (menu.AnchorRight)
{
lbl.Dock = DockStyle.Right;
}
else
{
lbl.Dock = DockStyle.Left;
}
this.Controls.Add(lbl); lbl.BringToFront();
} }
}
finally
{
ControlHelper.FreezeControl(this, false);
}
} /// <summary>
/// Handles the Click event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void lbl_Click(object sender, EventArgs e)
{
Label lbl = sender as Label;
if (lbl.Tag != null)
{
var menu = (NavigationMenuItemExt)lbl.Tag;
if (menu.ShowControl == null)
{
selectItem = menu; while (m_lstAnchors.Count > )
{
try
{
foreach (var item in m_lstAnchors)
{
item.Value.Hide();
}
}
catch { }
} if (ClickItemed != null)
{
ClickItemed(this, e);
}
}
}
}
/// <summary>
/// Handles the MouseEnter event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void lbl_MouseEnter(object sender, EventArgs e)
{
Label lbl = sender as Label;
var menu = lbl.Tag as NavigationMenuItemExt;
foreach (var item in m_lstAnchors)
{
m_lstAnchors[item.Key].Hide();
}
if (menu.ShowControl != null)
{
if (!m_lstAnchors.ContainsKey(menu))
{
m_lstAnchors[menu] = new FrmAnchor(lbl, menu.ShowControl);
}
m_lstAnchors[menu].Show();
m_lstAnchors[menu].Size = menu.ShowControl.Size;
}
}
/// <summary>
/// Handles the Paint event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
void lbl_Paint(object sender, PaintEventArgs e)
{
Label lbl = sender as Label;
if (lbl.Tag != null)
{
var menu = (NavigationMenuItemExt)lbl.Tag;
e.Graphics.SetGDIHigh(); if (menu.ShowTip)
{
if (!string.IsNullOrEmpty(menu.TipText))
{
var rect = new Rectangle(lbl.Width - , lbl.Height / - , , );
var path = rect.CreateRoundedRectanglePath();
e.Graphics.FillPath(new SolidBrush(tipColor), path);
e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
else
{
e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - , lbl.Height / - , , ));
}
}
if (menu.Icon != null)
{
e.Graphics.DrawImage(menu.Icon, new Rectangle(, (lbl.Height - ) / , , ), , , menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
}
}
}
全部代码
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-10-11
//
// ***********************************************************************
// <copyright file="UCNavigationMenuExt.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZH_Controls.Forms; namespace HZH_Controls.Controls
{
/// <summary>
/// Class UCNavigationMenuExt.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
[DefaultEvent("ClickItemed")]
public partial class UCNavigationMenuExt : UserControl
{
/// <summary>
/// Occurs when [click itemed].
/// </summary>
[Description("点击节点事件"), Category("自定义")] public event EventHandler ClickItemed;
/// <summary>
/// The select item
/// </summary>
private NavigationMenuItemExt selectItem = null; /// <summary>
/// Gets the select item.
/// </summary>
/// <value>The select item.</value>
[Description("选中的节点"), Category("自定义")]
public NavigationMenuItemExt SelectItem
{
get { return selectItem; }
private set { selectItem = value; }
} /// <summary>
/// The items
/// </summary>
NavigationMenuItemExt[] items; /// <summary>
/// Gets or sets the items.
/// </summary>
/// <value>The items.</value>
[Description("节点列表"), Category("自定义")]
public NavigationMenuItemExt[] Items
{
get { return items; }
set
{
items = value;
ReloadMenu();
}
}
/// <summary>
/// The tip color
/// </summary>
private Color tipColor = Color.FromArgb(, , ); /// <summary>
/// Gets or sets the color of the tip.
/// </summary>
/// <value>The color of the tip.</value>
[Description("角标颜色"), Category("自定义")]
public Color TipColor
{
get { return tipColor; }
set { tipColor = value; }
} /// <summary>
/// 获取或设置控件的前景色。
/// </summary>
/// <value>The color of the fore.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
foreach (Control c in this.Controls)
{
c.ForeColor = value;
}
}
}
/// <summary>
/// 获取或设置控件显示的文字的字体。
/// </summary>
/// <value>The font.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
foreach (Control c in this.Controls)
{
c.Font = value;
}
}
} /// <summary>
/// The m LST anchors
/// </summary>
Dictionary<NavigationMenuItemExt, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItemExt, FrmAnchor>();
/// <summary>
/// Initializes a new instance of the <see cref="UCNavigationMenuExt"/> class.
/// </summary>
public UCNavigationMenuExt()
{
InitializeComponent();
items = new NavigationMenuItemExt[];
if (ControlHelper.IsDesignMode())
{
items = new NavigationMenuItemExt[];
for (int i = ; i < ; i++)
{
items[i] = new NavigationMenuItemExt()
{
Text = "菜单" + (i + ),
AnchorRight = i >=
};
}
}
} /// <summary>
/// Reloads the menu.
/// </summary>
private void ReloadMenu()
{
try
{
ControlHelper.FreezeControl(this, true);
this.Controls.Clear();
if (items != null && items.Length > )
{
foreach (var item in items)
{
var menu = (NavigationMenuItemExt)item;
Label lbl = new Label();
lbl.AutoSize = false;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Width = menu.ItemWidth;
lbl.Text = menu.Text; lbl.Font = Font;
lbl.ForeColor = ForeColor; lbl.Paint += lbl_Paint;
lbl.MouseEnter += lbl_MouseEnter;
lbl.Tag = menu;
lbl.Click += lbl_Click;
if (menu.AnchorRight)
{
lbl.Dock = DockStyle.Right;
}
else
{
lbl.Dock = DockStyle.Left;
}
this.Controls.Add(lbl); lbl.BringToFront();
} }
}
finally
{
ControlHelper.FreezeControl(this, false);
}
} /// <summary>
/// Handles the Click event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void lbl_Click(object sender, EventArgs e)
{
Label lbl = sender as Label;
if (lbl.Tag != null)
{
var menu = (NavigationMenuItemExt)lbl.Tag;
if (menu.ShowControl == null)
{
selectItem = menu; while (m_lstAnchors.Count > )
{
try
{
foreach (var item in m_lstAnchors)
{
item.Value.Hide();
}
}
catch { }
} if (ClickItemed != null)
{
ClickItemed(this, e);
}
}
}
}
/// <summary>
/// Handles the MouseEnter event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void lbl_MouseEnter(object sender, EventArgs e)
{
Label lbl = sender as Label;
var menu = lbl.Tag as NavigationMenuItemExt;
foreach (var item in m_lstAnchors)
{
m_lstAnchors[item.Key].Hide();
}
if (menu.ShowControl != null)
{
if (!m_lstAnchors.ContainsKey(menu))
{
m_lstAnchors[menu] = new FrmAnchor(lbl, menu.ShowControl);
}
m_lstAnchors[menu].Show();
m_lstAnchors[menu].Size = menu.ShowControl.Size;
}
}
/// <summary>
/// Handles the Paint event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
void lbl_Paint(object sender, PaintEventArgs e)
{
Label lbl = sender as Label;
if (lbl.Tag != null)
{
var menu = (NavigationMenuItemExt)lbl.Tag;
e.Graphics.SetGDIHigh(); if (menu.ShowTip)
{
if (!string.IsNullOrEmpty(menu.TipText))
{
var rect = new Rectangle(lbl.Width - , lbl.Height / - , , );
var path = rect.CreateRoundedRectanglePath();
e.Graphics.FillPath(new SolidBrush(tipColor), path);
e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
else
{
e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - , lbl.Height / - , , ));
}
}
if (menu.Icon != null)
{
e.Graphics.DrawImage(menu.Icon, new Rectangle(, (lbl.Height - ) / , , ), , , menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
}
}
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(八十三)c#Winform自定义控件-导航菜单(扩展)的更多相关文章
- (十三)c#Winform自定义控件-导航菜单
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (七十九)c#Winform自定义控件-导航菜单
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (八十四)c#Winform自定义控件-导航菜单(类Office菜单)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (四十二)c#Winform自定义控件-进度条扩展
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (十八)c#Winform自定义控件-提示框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (四十八)c#Winform自定义控件-下拉按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (二十八)c#Winform自定义控件-文本框(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (七十八)c#Winform自定义控件-倒影组件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (八)c#Winform自定义控件-分割线
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- 【原创】(三)Linux paging_init解析
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 现代 JavaScript 教程到底是什么?
手册与规范 <现代 JavaScript 教程>是开源的现代 JavaScript 从入门到进阶的优质教程,它旨在帮助你逐渐掌握 JavaScript 这门语言.但是一旦你已经熟悉了这门语 ...
- 这一次,彻底弄懂 Promise 原理
作者声明 本人将迁移至个人公众号「前端Q」及「掘金」平台写文章.博客园的文章将不再及时更新发布.欢迎大家关注公众号「前端Q」及我的掘金主页:https://juejin.im/user/5874526 ...
- HTTPS页面使用CNZZ统计代码,Chrome显示警告怎么办?
很多站长会遇到一个问题,网站加入CNZZ的JS统计代码后,Chrome浏览器出现警告:阻止跨站解析器阻断脚本通过document.write调用(A parser-blocking, cross si ...
- (六十五)c#Winform自定义控件-图标字体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- Go语言基础之单元测试
不写测试的开发不是好程序员.我个人非常崇尚TDD(Test Driven Development)的,然而可惜的是国内的程序员都不太关注测试这一部分. 这篇文章主要介绍下在Go语言中如何做单元测试和基 ...
- [1]尝试用Unity3d制作一个王者荣耀(持续更新)->选择英雄-(上)
如果已经看过本章节:目录传送门:这是目录鸭~ 1.场景搭建: 首先我们去AssetStore逛淘宝~~~ 我淘到的是这个资源,其他好看的场景(消耗不高的都行). 然后我们导入了这个资源后,把资源根文件 ...
- unittest 常用断言
1 assertEqual(a,b) 判断a==b 2 assertNotEqual(a,b) 判断a !=b 3 assertTrue(x) bool (x) is True 4 assertF ...
- 常用 JS 函数
各种业务开发都离不开对数据的处理,然而遇到的很多数据都是不好处理的.这个时候就需要寻求搜索引擎的帮助.这种方法效率是非常低下的,而且根据作者的个性不能保证其对自己的口味.因此这篇文字包含了一份 JS ...
- 第一次参与国际空间站ISS 的SSTV活动
先来看看本次 ISS 的 SSTV活动公告 SSTV Event planned for Early August ARISS News Release ...