(⼗三)c#Winform⾃定义控件-导航菜单-HZHControls 官⽹
前提
⼊⾏已经7,8年了,⼀直想做⼀套漂亮点的⾃定义控件,于是就有了本系列⽂章。
GitHub:
码云:
如果觉得写的还⾏,请点个 star ⽀持⼀下吧
欢迎前来交流探讨:企鹅568015492
⽬录
准备⼯作
有时候我们需要左侧的导航菜单,那么来整⼀个吧
先来分析分析,导航菜单⼀般分为2级或多级,如果是多级的话⽤前⾯的treeview更合适,这⾥只做2级,为了⽗⼦节点样式更⽅便控制,我们分别实现⽗⼦节点。
为了更加的Open,我们使⽤接⼝来定义⼀下
开始
定义⼀个节点数据绑定实体
1 [Serializable]
2public class MenuItemEntity
3 {
4///<summary>
5///键
6///</summary>
7public string Key { get; set; }
8///<summary>
9///⽂字
10///</summary>
11public string Text { get; set; }
12///<summary>
13///⼦节点
14///</summary>
15public List<MenuItemEntity> Childrens { get; set; }
16///<summary>
17///⾃定义数据源,⼀般⽤于扩展展⽰,⽐如定义节点图⽚等
18///</summary>
19public object DataSource { get; set; }
20
21 }
再定义⼀个接⼝来约束
1public interface IMenuItem
2 {
3event EventHandler SelectedItem;
4 MenuItemEntity DataSource { get; set; }
5///<summary>
6///设置样式
7///</summary>
8///<param name="styles">key:属性名称,value:属性值</param>
9void SetStyle(Dictionary<string, object> styles);
10///<summary>
11///设置选中样式
12///</summary>
13///<param name="blnSelected">是否选中</param>
14void SetSelectedStyle(bool blnSelected);
15 }
⾸先看⽗节点定义,添加⼀个⽤户控件,命名UCMenuParentItem,并且实现接⼝IMenuItem
public event EventHandler SelectedItem;
private MenuItemEntity m_dataSource;
public MenuItemEntity DataSource
{
get
{
return m_dataSource;
}
set
{
m_dataSource = value;
if (value != null)
{
lblTitle.Text = value.Text;
}
}
}
public void SetStyle(Dictionary<string, object> styles)
{
Type t = this.GetType();
foreach (var item in styles)
{
var pro = t.GetProperty(item.Key);
if (pro != null && pro.CanWrite)
{
try
{
pro.SetValue(this, item.Value, null);
}
catch (Exception ex)
{
throw new Exception("菜单元素设置样式异常", ex);
}
}
}
}
public void SetSelectedStyle(bool blnSelected)
{
if (blnSelected)
{
this.lblTitle.Image = Properties.Resources.sanjiao1;
}
else
{
this.lblTitle.Image = Properties.Resources.sanjiao2;
}
}
然后处理下点击事件
lblTitle.MouseDown += lblTitle_MouseDown;
void lblTitle_MouseDown(object sender, MouseEventArgs e)
{
if (SelectedItem != null)
{
SelectedItem(this, e);
}
}
这样就完事了,看下完整代码
1// 版权所有黄正辉交流:568015492 QQ:623128629
2// ⽂件名称:UCMenuParentItem.cs
3// 创建⽇期:2019-08-15 16:02:35
4// 功能描述:Menu
5// 项⽬地址:gitee/kwwwvagaa/net_winform_custom_control 6using System;
7using System.Collections.Generic;
8using System.ComponentModel;
9using System.Drawing;
10using System.Data;
11using System.Linq;
12using System.Text;
13using System.Windows.Forms;
14
15namespace HZH_Controls.Controls
16 {
17///<summary>
18///⽗类节点
19///</summary>
20 [ToolboxItem(false)]
21public partial class UCMenuParentItem : UserControl, IMenuItem 22 {
23public event EventHandler SelectedItem;
24
25private MenuItemEntity m_dataSource;
26public MenuItemEntity DataSource
27 {
28get
29 {
30return m_dataSource;
31 }
32set
33 {
34 m_dataSource = value;
35if (value != null)
36 {
37 lblTitle.Text = value.Text;
38 }
39 }
40 }
41
42public UCMenuParentItem()
43 {
44 InitializeComponent();
45 lblTitle.MouseDown += lblTitle_MouseDown;
46 }
47
48public void SetStyle(Dictionary<string, object> styles)
49 {
50 Type t = this.GetType();
51foreach (var item in styles)
52 {
53var pro = t.GetProperty(item.Key);
54if (pro != null && pro.CanWrite)
55 {
56try
57 {
58 pro.SetValue(this, item.Value, null);
59 }
60catch (Exception ex)
61 {
62throw new Exception("菜单元素设置样式异常", ex);
63 }
64 }
65 }
66 }
67
68public void SetSelectedStyle(bool blnSelected)
69 {
70if (blnSelected)
71 {
72this.lblTitle.Image = Properties.Resources.sanjiao1;
73 }
74else
75 {
76this.lblTitle.Image = Properties.Resources.sanjiao2;
77 }
78 }
79
80void lblTitle_MouseDown(object sender, MouseEventArgs e) 81 {
82if (SelectedItem != null)
83 {
84 SelectedItem(this, e);
85 }
86 }
87 }
88 }
View Code
1namespace HZH_Controls.Controls
2 {
3partial class UCMenuParentItem
4 {
5///<summary>
6///必需的设计器变量。导航菜单
7///</summary>
8private System.ComponentModel.IContainer components = null; 9
10///<summary>
11///清理所有正在使⽤的资源。
12///</summary>
13///<param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14protected override void Dispose(bool disposing)
15 {
16if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20base.Dispose(disposing);
21 }
22
23#region组件设计器⽣成的代码
24
25///<summary>
26///设计器⽀持所需的⽅法 - 不要
27///使⽤代码编辑器修改此⽅法的内容。
28///</summary>
29private void InitializeComponent()
30 {
31this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
32this.lblTitle = new System.Windows.Forms.Label();
33this.SuspendLayout();
34//
35// ucSplitLine_H1
36//
37this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(30)))), ((int)(((byte)(39)))));
38this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Bottom;
39this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 59);
40this.ucSplitLine_H1.Name = "ucSplitLine_H1";
41this.ucSplitLine_H1.Size = new System.Drawing.Size(200, 1);
42this.ucSplitLine_H1.TabIndex = 0;
43this.ucSplitLine_H1.TabStop = false;
44//
45// lblTitle
46//
47this.lblTitle.Dock = System.Windows.Forms.DockStyle.Fill;
48this.lblTitle.ForeColor = System.Drawing.Color.White;
49this.lblTitle.Image = global::HZH_Controls.Properties.Resources.sanjiao2;
50this.lblTitle.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
51this.lblTitle.Location = new System.Drawing.Point(0, 0);
52this.lblTitle.Name = "lblTitle";
53this.lblTitle.Size = new System.Drawing.Size(200, 59);
54this.lblTitle.TabIndex = 1;
55this.lblTitle.Text = "⽗项";
56this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
57//
58// UCMenuParentItem
59//
60this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
61this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(43)))), ((int)(((byte)(54)))));
62this.Controls.Add(this.lblTitle);
63this.Controls.Add(this.ucSplitLine_H1);
64this.Font = new System.Drawing.Font("微软雅⿊", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 65this.Name = "UCMenuParentItem";
66this.Size = new System.Drawing.Size(200, 60);
67this.ResumeLayout(false);
68
69 }
70
71#endregion
72
73private UCSplitLine_H ucSplitLine_H1;
74private System.Windows.Forms.Label lblTitle;
75 }
76 }
View Code
设计效果就是这样⼦的了
⽗节点弄好了,下⾯就是⼦节点了
添加⽤户控件,命名UCMenuChildrenItem,实现接⼝IMenuItem
public event EventHandler SelectedItem;
private MenuItemEntity m_dataSource;
public MenuItemEntity DataSource
{
get
{
return m_dataSource;
}
set
{
m_dataSource = value;
if (value != null)
{
lblTitle.Text = value.Text;
}
}
}
public void SetStyle(Dictionary<string, object> styles)
{
Type t = this.GetType();
foreach (var item in styles)
{
var pro = t.GetProperty(item.Key);
if (pro != null && pro.CanWrite)
{
try
{
pro.SetValue(this, item.Value, null);
}
catch (Exception ex)
{
throw new Exception("菜单元素设置样式异常", ex);
}
}
}
}
处理下点击事件
1this.lblTitle.MouseDown += lblTitle_MouseDown;
2
3void lblTitle_MouseDown(object sender, MouseEventArgs e)
4 {
5if (SelectedItem != null)
6 {
7 SelectedItem(this, null);
8 }
9 }
这样就完成了,看下完整代码
1// 版权所有黄正辉交流:568015492 QQ:623128629
2// ⽂件名称:UCMenuChildrenItem.cs
3// 创建⽇期:2019-08-15 16:02:26
4// 功能描述:Menu
5// 项⽬地址:gitee/kwwwvagaa/net_winform_custom_control 6using System;
7using System.Collections.Generic;
8using System.ComponentModel;
9using System.Drawing;
10using System.Data;
11using System.Linq;
12using System.Text;
13using System.Windows.Forms;
14
15namespace HZH_Controls.Controls
16 {
17///<summary>
18///⼦类节点
19///</summary>
20 [ToolboxItem(false)]
21public partial class UCMenuChildrenItem : UserControl, IMenuItem 22 {
23public event EventHandler SelectedItem;
24
25private MenuItemEntity m_dataSource;
26public MenuItemEntity DataSource
27 {
28get
29 {
30return m_dataSource;
31 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论