自定義控件:組合控件,實現導航菜單的功能
1,屬性
經過分析控件功能,有以下屬性
【1.1】導航欄圖片:Image NaviImage
【1.2】導航按鈕名:String NaviName
【1.3】是否激活:Bool IsActive
【1.4】激活滑塊變距:Int ActiveGap
【1.5】激活方塊高度:Int ActiveHeight
【1.6】激活方塊顏色:Color ActiveColor
【1.7】懸浮的漸變色系數:Float ColorDepth
【1.8】權限:Int Role
【1.9】單擊事件:ClickEvent
2,添加用戶控件
添加“用戶控件”
注意:字體和大小要與主窗體一致,要不然會導致拖入后大小不匹配的情況
3,UI設計
讓Label標簽字體永遠在下方,居中顯示,的設置
4,添加屬性
【4.1】哪些屬性需要propfull,哪些屬性只用prop就可以,取決於這個屬性是否要實時刷新
5,添加事件
【5.1】
6,代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Jason_Controls { //【自定義事件步驟3:最關鍵的設置,被調用的時候,默認就是調用這個ClickEvent事件】 [DefaultEvent("ClickEvent")] public partial class NaviButton : UserControl { public NaviButton() { InitializeComponent(); SaveBackColor = this.BackColor; } private Color SaveBackColor; private Image naviImage=Properties.Resources.首頁_32黑色;//一定要有個默認設置,要不然后面使用的時候,一直是null [Browsable(true)] [Category("Jason自定義屬性")] [Description("導航按鈕圖片設置")] public Image NaviImage { get { return naviImage; } set { naviImage = value; this.pic_main.Image = naviImage; } } private String naviName = "實時監控"; [Browsable(true)] [Category("Jason自定義屬性")] [Description("導航按鈕名稱設置")] public String NaviName { get { return naviName; } set { naviName = value; this.lbl_naviName.Text = naviName; } } private bool isActive = false; [Browsable(true)] [Category("Jason自定義屬性")] [Description("導航按鈕是否激活")] public bool IsActive { get { return isActive; } set { isActive = value; this.Invalidate();//重繪 } } private int activeGap = 0; [Browsable(true)] [Category("Jason自定義屬性")] [Description("激活方塊邊距")] public int ActiveGap { get { return activeGap; } set { activeGap = value; this.Invalidate();//重繪 } } private int activeHeight = 4; [Browsable(true)] [Category("Jason自定義屬性")] [Description("激活方塊高度")] public int ActiveHeight { get { return activeHeight; } set { activeHeight = value; this.Invalidate();//重繪 } } private Color activeColor = Color.FromArgb(236,250,243); [Browsable(true)] [Category("Jason自定義屬性")] [Description("激活方塊的顏色")] public Color ActiveColor { get { return activeColor; } set { activeColor = value; this.Invalidate();//重繪 } } //這個屬性不用實時刷新,就不需要propfull來設置屬性了 [Browsable(true)] [Category("Jason自定義屬性")] [Description("權限等級")] public int Role { get; set; } = 0; private void NvaiButton_Load(object sender, EventArgs e) { } //【自定義事件步驟1:自定義一個Event】 [Browsable(true)] [Category("Jason自定義事件")] [Description("導航按鈕的單擊事件")] public event EventHandler ClickEvent; //【自定義事件步驟2:什么時候調用這個事件】 private void lbl_naviName_Click(object sender, EventArgs e) { if (ClickEvent != null) { ClickEvent.Invoke(this,e);//【特別注意】第一個參數要用this,因為后面判斷的時候要用,this就是指這個當前自定義控件NaviButton。 //(如果后面不用判斷,參數都無所謂,用sender或null都可以) //ClickEvent.Invoke(sender,e);//【如果參數是sender,去點擊的時候sender對應的就是這個Label標簽】 } } private void pic_main_Click(object sender, EventArgs e) { if (ClickEvent != null) { ClickEvent.Invoke(this, e); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics graphics = e.Graphics; //畫矩形 Rectangle rectangle = new Rectangle(activeGap, this.Height - activeHeight, this.Width - 2 * activeGap, activeHeight); if (IsActive) { graphics.FillRectangle(new SolidBrush(this.activeColor) ,rectangle); } else { graphics.FillRectangle(new SolidBrush(this.BackColor), rectangle); } } #region 漸變色設置 //這個屬性不用實時刷新,就不需要propfull來設置屬性了 [Browsable(true)] [Category("Jason自定義屬性")] [Description("懸浮漸變色系數")] public float ColorDepth { get; set; } = -0.2f; //鼠標放上去,變色 private void lbl_naviName_MouseEnter(object sender, EventArgs e) { this.BackColor = ChangeColor(this.BackColor, ColorDepth); } //鼠標移走,變回原有的顏色 private void lbl_naviName_MouseLeave(object sender, EventArgs e) { this.BackColor = SaveBackColor; } /// <summary> /// 通用的顏色漸變方法:在某個顏色基礎上,添加一個修正系數(-1到1之間) /// </summary> /// <param name="color"></param> /// <param name="correctionFactor"></param> /// <returns></returns> private Color ChangeColor(Color color, float correctionFactor) { if (correctionFactor > 1.0f) correctionFactor = 1.0f; if (correctionFactor < -1.0f) correctionFactor = -1.0f; float red = (float) color.R; float green = (float) color.G; float blue = (float) color.B; if (correctionFactor < 0) { correctionFactor = 1 + correctionFactor; red *= correctionFactor; green *= correctionFactor; blue *= correctionFactor; } else { red = (255 - red) * correctionFactor + red; green = (255 - green) * correctionFactor + green; blue = (255 - blue) * correctionFactor + blue; } if (red < 0) red = 0; if (red > 255) red = 255; if (green < 0) green = 0; if (green > 255) green = 255; if (blue < 0) blue = 0; if (blue > 255) blue = 255; return Color.FromArgb(color.A, (int) red, (int) green, (int) blue); } #endregion } }
7,特別注意: