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 ...
随机推荐
- [apue] popen/pclose 疑点解惑
问题请看这里: [apue] 使用 popen/pclose 的一点疑问 当时怀疑是pclose关闭了使用完成的管道,因此在pclose之前加一个足够长的sleep,再次观察进程文件列表: 哈哈,这下 ...
- ajax 的登录认证
在models中 先创建一个表 from django.db import models # Create your models here. class UserInfo(models.Model) ...
- spring boot freemarker 导出word 带echarts图形报表
创建word文件内容如下 将word导出为xml格式 将文件后缀名改为 .ftl 在springboot项目中添加freemarker依赖 <!-- 导出word文档--> <dep ...
- VB非常见知识总结
1.VB.Net设置Excel中单元格字体 sheet.Range(sheet.Cells(row, stp), sheet.Cells(row, stp)).Font.Name = "Wi ...
- Oracle数据库常用脚本命令(二)
--创建学生信息表create table student( sid number(8,0), name varchar2(20), sex char(2), birthday date, addre ...
- 【深入浅出-JVM】(5):Java 虚拟机结构
Java 虚拟机基本结构 Java 堆 新生代.老年代划分 栈帧 感谢您的耐心阅读,如果您发现文章中有一些没表述清楚的,或者是不对的地方,请给我留言,您的鼓励是作者写作最大的动力. 作 者 : @mo ...
- RDBMS与数据库之间的关系
什么是数据库? 数据库就是用来存放数据的仓库,它是一种特殊的文件. 什么是关系型数据库? 关系型数据库就是指建立在关系模型基础上的数据库,通常由多张表组成,这些表之间存在一定的关系. 什么是RDBMS ...
- GStreamer基础教程04 - 动态连接Pipeline
摘要 在以前的文章中,我们了解到了2种播放文件的方式:一种是在知道了文件的类型及编码方式后,手动创建所需Element并构造Pipeline:另一种是直接使用playbin,由playbin内部动态创 ...
- java截取避免空字符丢失
1. 场景描述 数据后端是Hbase等nosql数据库,返回的数据以逗号分隔,java后端获取数据后,需要新增组装数据后再返回给前端. 2. 问题解决 2.1 问题定位 本来用的java的split进 ...
- CDQZ集训DAY6 日记
又炸了. 早上起来其他竞赛生也走了,食堂做饭做的挺潦草,但为什么四川烧麦的馅是米啊??!! 起来看题总觉得都似曾相识.第一题打完40分暴力后想拿莫队搞到70分,但发现能想到的莫队维护都是nsqrt(n ...