效果:

描述:

这是一个可折叠的菜单导航,主要是由panel、picturebox、label完成,界面的颜色用来区分一下各个组合控件,便于调试。
首先,首先是ImageButton:
这个是由PictureBox和Label构成。
1.文字部分的居中需要设置label的宽度为父容器的宽度。
2.图片设置 picBox.SizeMode = PictureBoxSizeMode.StretchImage
 
然后是单个的菜单 命名为:ButtonGroup:
顶部灰色的是一个label,绿色里面的是一个包含ImageButton的列表。
1.顶部这个label注册了一个click事件,是为了完成点击它的时候会展开。
 
最后是整个的菜单,就是由ButtonGroup组成的一个list,我给它命名为:CustomNavBar。
 
这里需要注意的是:
事件的封装,像这种自定义控件,都是要定义自定义事件,并且把它暴露出来,元控件触发的时候,才触发这个暴露出来的控件。代码类似这种:
private void PicBoxClick(object sender,EventArgs args)
{
PictureBoxClick?.Invoke(sender, this.text);
}
PicBoxClick是PictureBox控件的原生Click事件,PictureBoxClick是我们自己定义的事件,这个事件需要到控件外部注册,就像这样:
for (int i = 0; i < _listImageButton.Count; i++)
{
var imgButton = _listImageButton[i];
 
imgButton.Top = i * imgButton.Height;
imgButton.Left = 0;
 
imgButton.PictureBoxClick += ImgButton_PictureBoxClick;
 
ButtonArea.Controls.Add(imgButton);
}
这段代码中,_listImageButton是属于ButtonGroup控件里的集合,这样做,最终点击PictureBox的时候,其实是调用ImgButton_PictureBoxClick这个方法里的内容。
假如这个ButtonGroup已经是最外层的自定义控件了,那么这个ImgButton_PictureBoxClick就属于供这个完整的自定义控件使用人也就是客户自己自定义的事件了,这个里面的代码就可以根据不同的客户,内容就可以不同了。
因为这个控件最外层的自定义控件是CustomNavBar,所以在CustomNavbar里还自定义了一个相同委托的事件:
public delegate void ImageButtonClick(object o,string moduleName);
public event ImageButtonClick NavButtonClick;
 
 
这代码也是我从其他地方下载的,然后自己看了一篇,在自己敲的。敲的过程中,了解到了内部类的使用,最大的收获是:一般窗体都是要有partial修饰的,而InitializeComponent()方法都是放在自动生成的代码里的,做这个控件的时候,我还是自己第一次这样通过写代码的方式来初始化一个控件。

代码:

public partial class CustomNavBar : UserControl
{
public CustomNavBar()
{
InitializeComponent();
InitializeButtonGroup();
pnlMain.BackColor = Color.DarkOliveGreen;
ResetMenu();
}
private string _jsonString;
/// <summary>
///暴露接口,可从外部(数据库)获得菜单的信息
/// </summary>
public string JsonString
{
get { return _jsonString; }
set { _jsonString = value; }
} private List<ButtonGroup> listGroup;
private int currentItemIndex; public delegate void ImageButtonClick(object o,string moduleName); public event ImageButtonClick NavButtonClick; public void InitializeButtonGroup()
{
listGroup = new List<ButtonGroup>() {
new ButtonGroup("欢迎使用",
new List<ImageButton>()
{
new ImageButton("欢迎使用",Resources.LogoMain),
new ImageButton("员工信息管理",Resources.LogoManageEmployee)
}), new ButtonGroup("系统设置",
new List<ImageButton>(){
new ImageButton("部门管理", Resources.LogoManageDepartment)}), new ButtonGroup("退出系统",null)
};
ButtonGroup bgPre=null;
int sum = ;
for (int i = ; i < listGroup.Count; i++)
{
int height = bgPre == null ? : bgPre.Height;
sum += height;
var buttonGroup = listGroup[i]; buttonGroup.Tag = i; buttonGroup.SetButtonLocation(); buttonGroup.ImageButtonClick += new ImageButtonClick(NavClick); buttonGroup.Left = ; buttonGroup.Top = sum; buttonGroup.ItemMeunClick += ButtonGroup_ItemMeunClick; this.pnlMain.Controls.Add(buttonGroup); bgPre = buttonGroup; }
} private void NavClick(object sender,string moduleName)
{
NavButtonClick?.Invoke(sender, moduleName);
} private void ButtonGroup_ItemMeunClick(object sender, EventArgs e)
{
Label lblItem = sender as Label; currentItemIndex = ; if (lblItem != null)
{
ButtonGroup bg = lblItem.Parent as ButtonGroup; if (bg != null)
{
currentItemIndex = Convert.ToInt32(bg.Tag);
} }
ResetMenu();
} private void ResetMenu()
{
if (listGroup != null && listGroup.Count > )
{
int barHeight = ; int totalHeight = pnlMain.Height; for (int n = ; n < listGroup.Count; n++)
{ ButtonGroup buttonGroup = listGroup[n]; int thisIndex = Convert.ToInt32(buttonGroup.Tag); if (thisIndex <= currentItemIndex)
{
buttonGroup.Top =n * barHeight;
}
else
{
buttonGroup.Top = totalHeight - (listGroup.Count-n) * barHeight;
}
if (thisIndex == currentItemIndex)
{
buttonGroup.Height = totalHeight - (listGroup.Count - ) * barHeight;
}
else
{
buttonGroup.Height = barHeight;
}
} }
} #region ButtonGroup
public class ButtonGroup : UserControl
{
private Label lblItem = new Label(); private string text; private Panel ButtonArea = new Panel(); public event EventHandler ItemMeunClick; public event ImageButtonClick ImageButtonClick; public ButtonGroup(string txt,List<ImageButton> listImageButton)
{
this.text = txt;
this._listImageButton = listImageButton; this.Initialize();
} private List<ImageButton> _listImageButton; public List<ImageButton> ListImageButton
{
get { return _listImageButton; }
set { _listImageButton = value; }
} public void Initialize()
{
lblItem.Text = this.text;
lblItem.Height = ;
lblItem.Width = ;
lblItem.TextAlign = ContentAlignment.MiddleCenter;
lblItem.BackColor = Color.DarkGray;
lblItem.ForeColor = Color.DarkRed;
lblItem.Cursor = Cursors.Hand;
lblItem.Click += LblItem_Click; this.Width = ;
this.BackColor = Color.DarkOliveGreen;
this.Controls.Add(lblItem);
this.Controls.Add(ButtonArea);
} private void LblItem_Click(object sender, EventArgs e)
{
ItemMeunClick?.Invoke(sender, e);
} public void SetButtonLocation()
{
ButtonArea.Location = new Point(, ); int buttonCount = _listImageButton == null ? : _listImageButton.Count; int Height = ;
//ButtonArea.BackColor = Color.Red;
ButtonArea.Size = new Size(, buttonCount * Height);
if (_listImageButton != null && _listImageButton.Count > )
{ for (int i = ; i < _listImageButton.Count; i++)
{
var imgButton = _listImageButton[i]; imgButton.Top = i * imgButton.Height;
imgButton.Left = ; imgButton.PictureBoxClick += ImgButton_PictureBoxClick; ButtonArea.Controls.Add(imgButton);
}
} this.Height = lblItem.Height + ButtonArea.Height; } private void ImgButton_PictureBoxClick(object o, string moduleName)
{
ImageButtonClick?.Invoke(o, moduleName);
}
}
#endregion #region ImageButton
public class ImageButton : UserControl
{ private string text; private Image img; private PictureBox picBox=new PictureBox();
private Label lblText = new Label(); public event ImageButtonClick PictureBoxClick; public ImageButton(string txt, Image image)
{
this.text = txt;
this.img = image; this.InitialImageButtonControl();
} public void InitialImageButtonControl()
{ picBox.Size = new Size(, );
picBox.Left = ;
picBox.Top = ;
picBox.Image = this.img;
picBox.Anchor = AnchorStyles.Top;
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.Cursor = Cursors.Hand;
picBox.Click += new EventHandler(PicBoxClick); lblText.Width = ;
lblText.Height = ;
lblText.Location = new Point(, );
lblText.Text = this.text;
lblText.TextAlign = ContentAlignment.MiddleCenter;
lblText.BackColor = Color.DarkOliveGreen;
lblText.ForeColor = Color.DarkGray;
lblText.Cursor = Cursors.Hand; this.Height = picBox.Height + lblText.Height;
this.Controls.Add(picBox);
this.Controls.Add(lblText);
} private void PicBoxClick(object sender,EventArgs args)
{ PictureBoxClick?.Invoke(sender, this.text);
} }
#endregion }
 

C# Winform 自定义控件——竖着的Navbar的更多相关文章

  1. C# winform 自定义控件

    近来因为项目的问题,开始研究winform自定义控件,这篇主要是将自定义控件的属性在属性编辑器中可编辑,如果你对自定义控件比较了解的,就不用继续往下看了 首先,我创建了一个类UserButton,继承 ...

  2. C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果

    要想玩转Winform自定义控件需要对GDI+非常熟悉,对常用的控件有一些了解,好选择合适的基类控件来简化. 要点说明及代码 1)定义接口: using System; using System.Wi ...

  3. Winform自定义控件实例

    本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...

  4. winform 自定义控件:半透明Loading控件

    winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...

  5. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (二)c#Winform自定义控件-按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (四)c#Winform自定义控件-选择按钮组

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. (七)c#Winform自定义控件-进度条

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. 如何确保TCP协议传输稳定可靠?

    TCP,控制传输协议,它充分实现了数据传输时的各种控制功能:针对发送端发出的数据包确认应答信号ACK:针对数据包丢失或者出现定时器超时的重发机制:针对数据包到达接收端主机顺序乱掉的顺序控制:针对高效传 ...

  2. python数据库-安装问题总结(48)

    一.ERROR1698(28000):Access denied for user root@localhost错误 我的操作系统是ubuntu: 我的MySQL版本是: 安装完成后,登录mysql的 ...

  3. C++ 洛谷 P2657 [SCOI2009]windy数 题解

    P2657 [SCOI2009]windy数 同步数位DP 这题还是很简单的啦(差点没做出来 个位打表大佬请离开(包括记搜),我这里讲的是DP!!! 首先Cal(b+1)-Cal(a),大家都懂吧(算 ...

  4. navicat中查重并删除

    # 查询所有重复的数据 SELECT * FROM hao123 WHERE ir_url IN (SELECT ir_url FROM `hao123` GROUP BY ir_url having ...

  5. Redis HyperLogLog用法简介

    (1)HyperLogLog简介 在Redis 在 2.8.9 版本才添加了 HyperLogLog,HyperLogLog算法是用于基数统计的算法,每个 HyperLogLog 键只需要花费 12 ...

  6. 20141209-基本概念-BlogEngine.NET(1)-笔记

    最近在读BlogEngine.NET3.1源代码,希望能坚持到底吧. 刚接触源代码,没有思路,于是读了14篇关于BlogEngine.Net1.4.5的系列博客,地址:http://www.cnblo ...

  7. CAD2014学习笔记-常用绘图命令和工具

    基于 虎课网huke88.com CAD教程 圆的绘制 快捷键c:选定圆心绘制半径长度的圆 快捷键c + 命令行输入 3p(三点成圆) 2p(两点成圆) t(选定两个圆的切点绘制与两圆相切的圆,第三部 ...

  8. TensorFlow笔记-组件

    张量 TensorFlow用张量这种数据结构来表示所有的数据.你可以把一个张量想象成一个n维的数组或列表.一个张量有一个静态类型和动态类型的维数.张量可以在图中的节点之间流通.其实张量更代表的就是一种 ...

  9. linux中type 、file、stat三者的区别

    1.type 用来查看命令类型,以区别是内部命令还是外部命令 示例:[root@localhost ~]# type cd cd 是 shell 内嵌    [root@localhost ~]# t ...

  10. 端口,OSI7层模型,TCP的三次握手和四次挥手,进度条

    1.端口: 端口是为了将同一个电脑上的不同程序进行隔离 IP是找电脑,端口是找电脑上的程序 MySQL是一个软件,帮助我们在硬盘上进行文件操作,默认端口是3306 Redis是一个软件,帮助我们在内存 ...