你的位置:首页 > ASP.net教程

[ASP.net教程]Winform自定义控件—Button(下)


在上一篇博客中,写了自定义一个Button控件,接下来来演示一下能实现哪些功能。

先看效果图

以上所有样式都是通过VS属性工具设置的,无需手动写一行代码很方便

为了演示效果,第二行的控件是第一行鼠标移动上去的样式

1 第一个控件采用的是背景色加文字实现的效果

2 第二个控件采用的是背景图加文字实现的效果

3 第三个控件完全采用背景图实现的效果

以上三种形式的Button样式在实际开发中用的较为普遍,满足了基本开发中Button样式的需求

下来附上代码:

ButtonDemo.cs

using System.Windows.Forms;namespace Demo{  public partial class ButtonDemo : Form  {    public ButtonDemo()    {      InitializeComponent();    }  }}

View Code

ButtonDemo.Designer.cs

namespace Demo{  partial class ButtonDemo  {    /// <summary>    /// 必需的设计器变量。    /// </summary>    private System.ComponentModel.IContainer components = null;    /// <summary>    /// 清理所有正在使用的资源。    /// </summary>    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>    protected override void Dispose(bool disposing)    {      if (disposing && (components != null))      {        components.Dispose();      }      base.Dispose(disposing);    }    #region Windows 窗体设计器生成的代码    /// <summary>    /// 设计器支持所需的方法 - 不要    /// 使用代码编辑器修改此方法的内容。    /// </summary>    private void InitializeComponent()    {      this.buttonM1 = new landptf.controls.ButtonM();      this.buttonM3 = new landptf.controls.ButtonM();      this.buttonM2 = new landptf.controls.ButtonM();      this.buttonM4 = new landptf.controls.ButtonM();      this.buttonM5 = new landptf.controls.ButtonM();      this.buttonM6 = new landptf.controls.ButtonM();      this.SuspendLayout();      //       // buttonM1      //       this.buttonM1.AutoSize = true;      this.buttonM1.BackColor = System.Drawing.Color.Transparent;      this.buttonM1.BackColorLeave = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(68)))), ((int)(((byte)(51)))));      this.buttonM1.BackColorM = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(68)))), ((int)(((byte)(51)))));      this.buttonM1.BackColorMove = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(205)))), ((int)(((byte)(110)))));      this.buttonM1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;      this.buttonM1.FontM = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));      this.buttonM1.ImageLeave = null;      this.buttonM1.ImageM = null;      this.buttonM1.ImageMove = null;      this.buttonM1.Location = new System.Drawing.Point(18, 16);      this.buttonM1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);      this.buttonM1.Name = "buttonM1";      this.buttonM1.Size = new System.Drawing.Size(81, 69);      this.buttonM1.TabIndex = 0;      this.buttonM1.TextColor = System.Drawing.Color.White;      this.buttonM1.TextM = "Button1";      //       // buttonM3      //       this.buttonM3.AutoSize = true;      this.buttonM3.BackColor = System.Drawing.Color.Transparent;      this.buttonM3.BackColorLeave = System.Drawing.Color.Transparent;      this.buttonM3.BackColorM = System.Drawing.Color.Transparent;      this.buttonM3.BackColorMove = System.Drawing.Color.Transparent;      this.buttonM3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;      this.buttonM3.FontM = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));      this.buttonM3.ImageLeave = global::Demo.Properties.Resources.close2;      this.buttonM3.ImageM = global::Demo.Properties.Resources.close2;      this.buttonM3.ImageMove = global::Demo.Properties.Resources.close1;      this.buttonM3.Location = new System.Drawing.Point(459, 16);      this.buttonM3.Name = "buttonM3";      this.buttonM3.Size = new System.Drawing.Size(77, 69);      this.buttonM3.TabIndex = 2;      this.buttonM3.TextColor = System.Drawing.Color.Black;      this.buttonM3.TextM = "";      //       // buttonM2      //       this.buttonM2.AutoSize = true;      this.buttonM2.BackColor = System.Drawing.Color.Transparent;      this.buttonM2.BackColorLeave = System.Drawing.Color.Transparent;      this.buttonM2.BackColorM = System.Drawing.Color.Transparent;      this.buttonM2.BackColorMove = System.Drawing.Color.Transparent;      this.buttonM2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;      this.buttonM2.FontM = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));      this.buttonM2.ImageLeave = global::Demo.Properties.Resources.round2;      this.buttonM2.ImageM = global::Demo.Properties.Resources.round2;      this.buttonM2.ImageMove = global::Demo.Properties.Resources.round1;      this.buttonM2.Location = new System.Drawing.Point(251, 16);      this.buttonM2.Name = "buttonM2";      this.buttonM2.Size = new System.Drawing.Size(86, 69);      this.buttonM2.TabIndex = 1;      this.buttonM2.TextColor = System.Drawing.Color.White;      this.buttonM2.TextM = "Button2";      //       // buttonM4      //       this.buttonM4.AutoSize = true;      this.buttonM4.BackColor = System.Drawing.Color.Transparent;      this.buttonM4.BackColorLeave = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(205)))), ((int)(((byte)(110)))));      this.buttonM4.BackColorM = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(205)))), ((int)(((byte)(110)))));      this.buttonM4.BackColorMove = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(68)))), ((int)(((byte)(51)))));      this.buttonM4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;      this.buttonM4.FontM = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));      this.buttonM4.ImageLeave = null;      this.buttonM4.ImageM = null;      this.buttonM4.ImageMove = null;      this.buttonM4.Location = new System.Drawing.Point(18, 120);      this.buttonM4.Margin = new System.Windows.Forms.Padding(4);      this.buttonM4.Name = "buttonM4";      this.buttonM4.Size = new System.Drawing.Size(81, 69);      this.buttonM4.TabIndex = 0;      this.buttonM4.TextColor = System.Drawing.Color.White;      this.buttonM4.TextM = "Button1";      //       // buttonM5      //       this.buttonM5.AutoSize = true;      this.buttonM5.BackColor = System.Drawing.Color.Transparent;      this.buttonM5.BackColorLeave = System.Drawing.Color.Transparent;      this.buttonM5.BackColorM = System.Drawing.Color.Transparent;      this.buttonM5.BackColorMove = System.Drawing.Color.Transparent;      this.buttonM5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;      this.buttonM5.FontM = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));      this.buttonM5.ImageLeave = global::Demo.Properties.Resources.round1;      this.buttonM5.ImageM = global::Demo.Properties.Resources.round1;      this.buttonM5.ImageMove = global::Demo.Properties.Resources.round2;      this.buttonM5.Location = new System.Drawing.Point(251, 120);      this.buttonM5.Name = "buttonM5";      this.buttonM5.Size = new System.Drawing.Size(86, 69);      this.buttonM5.TabIndex = 1;      this.buttonM5.TextColor = System.Drawing.Color.White;      this.buttonM5.TextM = "Button2";      //       // buttonM6      //       this.buttonM6.AutoSize = true;      this.buttonM6.BackColor = System.Drawing.Color.Transparent;      this.buttonM6.BackColorLeave = System.Drawing.Color.Transparent;      this.buttonM6.BackColorM = System.Drawing.Color.Transparent;      this.buttonM6.BackColorMove = System.Drawing.Color.Transparent;      this.buttonM6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;      this.buttonM6.FontM = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));      this.buttonM6.ImageLeave = global::Demo.Properties.Resources.close1;      this.buttonM6.ImageM = global::Demo.Properties.Resources.close1;      this.buttonM6.ImageMove = global::Demo.Properties.Resources.close2;      this.buttonM6.Location = new System.Drawing.Point(459, 120);      this.buttonM6.Name = "buttonM6";      this.buttonM6.Size = new System.Drawing.Size(77, 69);      this.buttonM6.TabIndex = 2;      this.buttonM6.TextColor = System.Drawing.Color.Black;      this.buttonM6.TextM = "";      //       // ButtonDemo      //       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;      this.ClientSize = new System.Drawing.Size(571, 464);      this.Controls.Add(this.buttonM6);      this.Controls.Add(this.buttonM5);      this.Controls.Add(this.buttonM3);      this.Controls.Add(this.buttonM4);      this.Controls.Add(this.buttonM2);      this.Controls.Add(this.buttonM1);      this.Name = "ButtonDemo";      this.Text = "ButtonDemo";      this.ResumeLayout(false);      this.PerformLayout();    }    #endregion    private landptf.controls.ButtonM buttonM1;    private landptf.controls.ButtonM buttonM2;    private landptf.controls.ButtonM buttonM3;    private landptf.controls.ButtonM buttonM4;    private landptf.controls.ButtonM buttonM5;    private landptf.controls.ButtonM buttonM6;  }}

View Code