在上一篇博客中,寫了自定義一個Button控件,接下來來演示一下能實現哪些功能。
先看效果圖
以上所有樣式都是通過VS屬性工具設置的,無需手動寫一行代碼很方便
為了演示效果,第二行的控件是第一行鼠標移動上去的樣式
1 第一個控件采用的是背景色加文字實現的效果
2 第二個控件采用的是背景圖加文字實現的效果
3 第三個控件完全采用背景圖實現的效果
以上三種形式的Button樣式在實際開發中用的較為普遍,滿足了基本開發中Button樣式的需求
下來附上代碼:
ButtonDemo.cs

using System.Windows.Forms; namespace Demo { public partial class ButtonDemo : Form { public ButtonDemo() { InitializeComponent(); } } }
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; } }