C# Winform 自定义控件——竖着的Navbar
效果:
描述:
代码:
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的更多相关文章
- C# winform 自定义控件
近来因为项目的问题,开始研究winform自定义控件,这篇主要是将自定义控件的属性在属性编辑器中可编辑,如果你对自定义控件比较了解的,就不用继续往下看了 首先,我创建了一个类UserButton,继承 ...
- C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果
要想玩转Winform自定义控件需要对GDI+非常熟悉,对常用的控件有一些了解,好选择合适的基类控件来简化. 要点说明及代码 1)定义接口: using System; using System.Wi ...
- Winform自定义控件实例
本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...
- winform 自定义控件:半透明Loading控件
winform 自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...
- c#Winform自定义控件-目录
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二)c#Winform自定义控件-按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三)c#Winform自定义控件-有图标的按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (四)c#Winform自定义控件-选择按钮组
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (七)c#Winform自定义控件-进度条
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- 如何在VPS上搭建WordPress博客网站(史上最全图文教程)
由于现在很多人仍然使用共享主机,所以我决定写这篇教程,教你如何设置自己的虚拟专用服务器(VPS),以便为启动一个 WordPress 网站准备好所有必要的服务. 为什么共享托管不是最好的选择? 你的 ...
- POI自动调整列宽支持中文
/** * @Description:表格自适应宽度(中文支持) * @Author: * @param sheet sheet * @param columnLength 列数 */ private ...
- ConnectionPool实现redis在python中的连接
这篇文章主要介绍了Python与Redis的连接教程,Redis是一个高性能的基于内存的数据库,需要的朋友可以参考下 今天在写zabbix storm job监控脚本的时候用到了python的re ...
- S7-300CPU存储器介绍及存储卡使用
1. S7 300存储区概述 S7-300 PLC的存储区可以划分为四个区域:装载存储器(Load Memory).工作存储器(Work Memory). 系统存储器(System Memory)和保 ...
- java中动态代理的使用
代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在关联关系,一个代 ...
- 网络下载器 EagleGet v2.0.4.60 Full 绿色便携版
下载地址:点我 基本介绍 EagleGet(亦称 EG Download Accelerator)是一个用于 Windows 系统的下载管理器,它是免费软件.EagleGet 使用多线程技术,支持从Y ...
- Yii basic 模板支持连接多数据库
1.首先修改db配置文件,修改格式如下: return [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:hos ...
- plot3d网格读取写入与可视化
目录 说明 对于程序的说明 源码 说明 plot3d格式是NASA制定并大量使用的CFD网格文件格式,在CFD编程过程中经常涉及到.本文利用Python语言编写一个读取plot3d文件,写入plot3 ...
- 浅入深出Vue:发布项目
项目完成之后,当然不能满足于在我们的开发环境下跑一跑.我们可以打包发布到服务器上,让大家一起来欣赏一下你的作品. 那么 vue 项目如何打包发布呢,新建的项目目录下通常都有一个 README.md 的 ...
- signed char类型取值范围计算
在C语言程序中,给定一个类型,如何计算这个类型变量的取值范围呢?比如有一个字符型变量定义如下: signed char c: 这个字符变量c的取值范围是[-128,127],是计算出来的呢? 假设字符 ...