(二十)c#Winform自定义控件-有后退的窗体-HZHControls

官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

其实我也不知道这个应该叫什么名字,暂且叫有后退的窗体吧,这个窗体继承子基类窗体FrmBase,如果你对FrmBase还不了解,请移步 (十七)c#Winform自定义控件-基类窗体 查看

开始

添加一个Form,命名FrmBack,继承自FrmBase

属性

 1  private string _frmTitle = "自定义窗体";
 2         /// <summary>
 3         /// 窗体标题
 4         /// </summary>
 5         [Description("窗体标题"), Category("自定义")]
 6         public string FrmTitle
 7         {
 8             get { return _frmTitle; }
 9             set
10             {
11                 _frmTitle = value;
12                 btnBack1.BtnText = value;
13             }
14         }
15         [Description("帮助按钮点击事件"), Category("自定义")]
16         public event EventHandler BtnHelpClick;

一点小事件

 1  private void btnBack1_btnClick(object sender, EventArgs e)
 2         {
 3             this.Close();
 4         }
 5 
 6         private void label1_MouseDown(object sender, MouseEventArgs e)
 7         {
 8             if (BtnHelpClick != null)
 9                 BtnHelpClick(sender, e);
10         }

完整代码

 1 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
 2 // 文件名称:FrmTemp1.cs
 3 // 创建日期:2019-08-15 16:04:48
 4 // 功能描述:FrmTemp1
 5 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
 6 using System;
 7 using System.Collections.Generic;
 8 using System.ComponentModel;
 9 using System.Data;
10 using System.Drawing;
11 using System.Linq;
12 using System.Text;
13 using System.Windows.Forms;
14 
15 namespace HZH_Controls.Forms
16 {
17     [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
18     public partial class FrmBack : FrmBase
19     {
20         private string _frmTitle = "自定义窗体";
21         /// <summary>
22         /// 窗体标题
23         /// </summary>
24         [Description("窗体标题"), Category("自定义")]
25         public string FrmTitle
26         {
27             get { return _frmTitle; }
28             set
29             {
30                 _frmTitle = value;
31                 btnBack1.BtnText = value;
32             }
33         }
34         [Description("帮助按钮点击事件"), Category("自定义")]
35         public event EventHandler BtnHelpClick;
36 
37         public FrmBack()
38         {
39             InitializeComponent();
40         }
41 
42         private void btnBack1_btnClick(object sender, EventArgs e)
43         {
44             this.Close();
45         }
46 
47         private void label1_MouseDown(object sender, MouseEventArgs e)
48         {
49             if (BtnHelpClick != null)
50                 BtnHelpClick(sender, e);
51         }
52     }
53 }
View Code
  1 namespace HZH_Controls.Forms
  2 {
  3     partial class FrmBack
  4     {
  5         /// <summary>
  6         /// Required designer variable.
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// Clean up any resources being used.
 12         /// </summary>
 13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows Form Designer generated code
 24 
 25         /// <summary>
 26         /// Required method for Designer support - do not modify
 27         /// the contents of this method with the code editor.
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.components = new System.ComponentModel.Container();
 32             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmBack));
 33             this.panTop = new System.Windows.Forms.Panel();
 34             this.label1 = new System.Windows.Forms.Label();
 35             this.btnBack1 = new HZH_Controls.Controls.UCBtnImg();
 36             this.imageList1 = new System.Windows.Forms.ImageList(this.components);
 37             this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
 38             this.panTop.SuspendLayout();
 39             this.SuspendLayout();
 40             // 
 41             // panTop
 42             // 
 43             this.panTop.Controls.Add(this.label1);
 44             this.panTop.Controls.Add(this.btnBack1);
 45             this.panTop.Dock = System.Windows.Forms.DockStyle.Top;
 46             this.panTop.Location = new System.Drawing.Point(0, 0);
 47             this.panTop.Name = "panTop";
 48             this.panTop.Size = new System.Drawing.Size(679, 60);
 49             this.panTop.TabIndex = 2;
 50             // 
 51             // label1
 52             // 
 53             this.label1.BackColor = System.Drawing.Color.Transparent;
 54             this.label1.Dock = System.Windows.Forms.DockStyle.Right;
 55             this.label1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold);
 56             this.label1.Image = global::HZH_Controls.Properties.Resources.help;
 57             this.label1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
 58             this.label1.Location = new System.Drawing.Point(612, 0);
 59             this.label1.Name = "label1";
 60             this.label1.Size = new System.Drawing.Size(67, 60);
 61             this.label1.TabIndex = 1;
 62             this.label1.Text = "帮助";
 63             this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
 64             this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
 65             // 
 66             // btnBack1
 67             // 
 68             this.btnBack1.BackColor = System.Drawing.Color.Transparent;
 69             this.btnBack1.BtnBackColor = System.Drawing.Color.Transparent;
 70             this.btnBack1.BtnFont = new System.Drawing.Font("微软雅黑", 17F);
 71             this.btnBack1.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102)))));
 72             this.btnBack1.BtnText = "自定义按钮";
 73             this.btnBack1.ConerRadius = 5;
 74             this.btnBack1.Cursor = System.Windows.Forms.Cursors.Hand;
 75             this.btnBack1.Dock = System.Windows.Forms.DockStyle.Left;
 76             this.btnBack1.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
 77             this.btnBack1.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
 78             this.btnBack1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102)))));
 79             this.btnBack1.Image = ((System.Drawing.Image)(resources.GetObject("btnBack1.Image")));
 80             this.btnBack1.IsRadius = true;
 81             this.btnBack1.IsShowRect = true;
 82             this.btnBack1.IsShowTips = false;
 83             this.btnBack1.Location = new System.Drawing.Point(0, 0);
 84             this.btnBack1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
 85             this.btnBack1.Name = "btnBack1";
 86             this.btnBack1.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
 87             this.btnBack1.RectWidth = 1;
 88             this.btnBack1.Size = new System.Drawing.Size(200, 60);
 89             this.btnBack1.TabIndex = 0;
 90             this.btnBack1.TabStop = false;
 91             this.btnBack1.TipsText = "";
 92             this.btnBack1.BtnClick += new System.EventHandler(this.btnBack1_btnClick);
 93             // 
 94             // imageList1
 95             // 
 96             this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
 97             this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
 98             this.imageList1.Images.SetKeyName(0, "help.png");
 99             // 
100             // ucSplitLine_H1
101             // 
102             this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
103             this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
104             this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 60);
105             this.ucSplitLine_H1.MaximumSize = new System.Drawing.Size(0, 1);
106             this.ucSplitLine_H1.Name = "ucSplitLine_H1";
107             this.ucSplitLine_H1.Size = new System.Drawing.Size(679, 1);
108             this.ucSplitLine_H1.TabIndex = 0;
109             this.ucSplitLine_H1.TabStop = false;
110             // 
111             // FrmTemp1
112             // 
113             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
114             this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
115             this.ClientSize = new System.Drawing.Size(679, 477);
116             this.Controls.Add(this.ucSplitLine_H1);
117             this.Controls.Add(this.panTop);
118             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
119             this.Name = "FrmTemp1";
120             this.ShowIcon = false;
121             this.ShowInTaskbar = false;
122             this.Text = "FrmTemp1";
123             this.panTop.ResumeLayout(false);
124             this.ResumeLayout(false);
125 
126         }
127 
128         #endregion
129 
130         private Controls.UCBtnImg btnBack1;
131         private System.Windows.Forms.Label label1;
132         private System.Windows.Forms.ImageList imageList1;
133         private Controls.UCSplitLine_H ucSplitLine_H1;
134         private System.Windows.Forms.Panel panTop;
135     }
136 }
View Code

设计效果

用处及效果

用处:这个看个人使用情况吧,你高兴的话就用这个窗体就可以了

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

posted @ 2019-08-16 14:51  冰封一夏  阅读(1816)  评论(0编辑  收藏  举报
HZHControls控件库官网:http://hzhcontrols.com