目录

1.动态按钮的设计方法

2.实例

(1) Resources.Designer.cs

(2)Form1.Designer.cs

(3)Form1.cs

(4) 生成效果


        在窗体界面中,通常以按钮来代替菜单栏的功能,这种形式虽然给用户一种直观、界面风格各异的感觉,但通常按钮都是以静止的形式显示,当光标移到按钮上时,可以使按钮上的图片和文字说明动态化,使用户快捷地找到所选按钮,这时就需要一个动态按钮的窗体界面。

1.动态按钮的设计方法

         就是使得按钮的Image属性加载资源图片,再进一步设置其他属性,使得按钮的外观变为:图片+文字,或者图片,或者特效文字。

// 
// button1
// 
button1.BackColor = Color.Transparent;
button1.Image = Properties.Resources._1;
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.Location = new Point(1, 16);
button1.Name = "button1";
button1.Size = new Size(141, 48);
button1.TabIndex = 1;
button1.Text = "公司信息管理";
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.UseVisualStyleBackColor = false;

         还可以用事件动态改变按钮布局:

private void Button1_MouseMove(object sender, MouseEventArgs e)
{
    button1.ImageAlign = ContentAlignment.MiddleCenter;
    button1.TextImageRelation = TextImageRelation.ImageAboveText;
}

private void Button1_MouseLeave(object sender, EventArgs e)
{
    button1.ImageAlign = ContentAlignment.MiddleLeft;
    button1.TextImageRelation = TextImageRelation.ImageBeforeText;
}

2.实例

(1) Resources.Designer.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace _195.Properties {
    using System;
    
    
    /// <summary>
    ///   一个强类型的资源类,用于查找本地化的字符串等。
    /// </summary>
    // 此类是由 StronglyTypedResourceBuilder
    // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
    // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
    // (以 /str 作为命令选项),或重新生成 VS 项目。
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }
        
        /// <summary>
        ///   返回此类使用的缓存的 ResourceManager 实例。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_195.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// <summary>
        ///   重写当前线程的 CurrentUICulture 属性,对
        ///   使用此强类型资源类的所有资源查找执行重写。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _1 {
            get {
                object obj = ResourceManager.GetObject("_1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _2 {
            get {
                object obj = ResourceManager.GetObject("_2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _3 {
            get {
                object obj = ResourceManager.GetObject("_3", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _4 {
            get {
                object obj = ResourceManager.GetObject("_4", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _5 {
            get {
                object obj = ResourceManager.GetObject("_5", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _6 {
            get {
                object obj = ResourceManager.GetObject("_6", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap 中 {
            get {
                object obj = ResourceManager.GetObject("中", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
    }
}

(2)Form1.Designer.cs

namespace _195
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            label1 = new Label();
            button1 = new Button();
            button2 = new Button();
            button3 = new Button();
            button4 = new Button();
            button5 = new Button();
            button6 = new Button();
            panel1 = new Panel();
            panel1.SuspendLayout();
            SuspendLayout();
            // 
            // label1
            // 
            label1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
            label1.AutoSize = true;
            label1.Font = new Font("Microsoft YaHei UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 134);
            label1.Location = new Point(62, 13);
            label1.Name = "label1";
            label1.Size = new Size(221, 25);
            label1.TabIndex = 0;
            label1.Text = "吉林省丰满水电有限公司";
            // 
            // button1
            // 
            button1.BackColor = Color.Transparent;
            button1.Image = Properties.Resources._1;
            button1.ImageAlign = ContentAlignment.MiddleLeft;
            button1.Location = new Point(1, 16);
            button1.Name = "button1";
            button1.Size = new Size(141, 48);
            button1.TabIndex = 1;
            button1.Text = "公司信息管理";
            button1.TextImageRelation = TextImageRelation.ImageBeforeText;
            button1.UseVisualStyleBackColor = false;
            // 
            // button2
            // 
            button2.BackColor = Color.Transparent;
            button2.Image = Properties.Resources._2;
            button2.ImageAlign = ContentAlignment.MiddleLeft;
            button2.Location = new Point(1, 64);
            button2.Name = "button2";
            button2.Size = new Size(141, 48);
            button2.TabIndex = 2;
            button2.Text = "部门信息管理";
            button2.TextImageRelation = TextImageRelation.ImageBeforeText;
            button2.UseVisualStyleBackColor = false;
            // 
            // button3
            // 
            button3.Image = Properties.Resources._3;
            button3.ImageAlign = ContentAlignment.MiddleLeft;
            button3.Location = new Point(1, 112);
            button3.Name = "button3";
            button3.Size = new Size(141, 48);
            button3.TabIndex = 3;
            button3.Text = "人员信息管理";
            button3.TextImageRelation = TextImageRelation.ImageBeforeText;
            button3.UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            button4.Image = Properties.Resources._4;
            button4.ImageAlign = ContentAlignment.MiddleLeft;
            button4.Location = new Point(1, 160);
            button4.Name = "button4";
            button4.Size = new Size(141, 48);
            button4.TabIndex = 4;
            button4.Text = "银行信息管理";
            button4.TextImageRelation = TextImageRelation.ImageBeforeText;
            button4.UseVisualStyleBackColor = true;
            // 
            // button5
            // 
            button5.Image = Properties.Resources._5;
            button5.ImageAlign = ContentAlignment.MiddleLeft;
            button5.Location = new Point(1, 208);
            button5.Name = "button5";
            button5.Size = new Size(141, 48);
            button5.TabIndex = 5;
            button5.Text = "员工信息管理";
            button5.TextImageRelation = TextImageRelation.ImageBeforeText;
            button5.UseVisualStyleBackColor = true;
            // 
            // button6
            // 
            button6.BackColor = Color.Transparent;
            button6.Image = Properties.Resources._6;
            button6.ImageAlign = ContentAlignment.MiddleLeft;
            button6.Location = new Point(1, 256);
            button6.Name = "button6";
            button6.Size = new Size(141, 48);
            button6.TabIndex = 6;
            button6.Text = "员工调动管理";
            button6.TextImageRelation = TextImageRelation.ImageBeforeText;
            button6.UseVisualStyleBackColor = false;
            // 
            // panel1
            // 
            panel1.Controls.Add(button1);
            panel1.Controls.Add(button6);
            panel1.Controls.Add(button2);
            panel1.Controls.Add(button5);
            panel1.Controls.Add(button3);
            panel1.Controls.Add(button4);
            panel1.Location = new Point(2, 99);
            panel1.Name = "panel1";
            panel1.Size = new Size(146, 323);
            panel1.TabIndex = 7;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Properties.Resources.中;
            BackgroundImageLayout = ImageLayout.Stretch;
            ClientSize = new Size(540, 454);
            Controls.Add(panel1);
            Controls.Add(label1);
            Name = "Form1";
            Text = "动态按钮";
            panel1.ResumeLayout(false);
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private Label label1;
        private Button button1;
        private Button button2;
        private Button button3;
        private Button button4;
        private Button button5;
        private Button button6;
        private Panel panel1;
    }
}

(3)Form1.cs

namespace _195
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_MouseMove(object sender, MouseEventArgs e)
        {
            button1.ImageAlign = ContentAlignment.MiddleCenter;
            button1.TextImageRelation = TextImageRelation.ImageAboveText;
        }

        private void Button1_MouseLeave(object sender, EventArgs e)
        {
            button1.ImageAlign = ContentAlignment.MiddleLeft;
            button1.TextImageRelation = TextImageRelation.ImageBeforeText;
        }

        private void Button2_MouseMove(object sender, MouseEventArgs e)
        {
            button2.ImageAlign = ContentAlignment.MiddleCenter;
            button2.TextImageRelation = TextImageRelation.ImageAboveText;
        }

        private void Button2_MouseLeave(object sender, EventArgs e)
        {
            button2.ImageAlign = ContentAlignment.MiddleLeft;
            button2.TextImageRelation = TextImageRelation.ImageBeforeText;
        }

        private void Button3_MouseMove(object sender, MouseEventArgs e)
        {
            button3.ImageAlign = ContentAlignment.MiddleCenter;
            button3.TextImageRelation = TextImageRelation.ImageAboveText;
        }

        private void Button3_MouseLeave(object sender, EventArgs e)
        {
            button3.ImageAlign = ContentAlignment.MiddleLeft;
            button3.TextImageRelation = TextImageRelation.ImageBeforeText;
        }

        private void Button4_MouseMove(object sender, MouseEventArgs e)
        {
            button4.ImageAlign = ContentAlignment.MiddleCenter;
            button4.TextImageRelation = TextImageRelation.ImageAboveText;
        }

        private void Button4_MouseLeave(object sender, EventArgs e)
        {
            button4.ImageAlign = ContentAlignment.MiddleLeft;
            button4.TextImageRelation = TextImageRelation.ImageBeforeText;
        }

        private void Button5_MouseMove(object sender, MouseEventArgs e)
        {
            button5.ImageAlign = ContentAlignment.MiddleCenter;
            button5.TextImageRelation = TextImageRelation.ImageAboveText;
        }

        private void Button5_MouseLeave(object sender, EventArgs e)
        {
            button5.ImageAlign = ContentAlignment.MiddleLeft;
            button5.TextImageRelation = TextImageRelation.ImageBeforeText;
        }

        private void Button6_MouseMove(object sender, MouseEventArgs e)
        {
            button6.ImageAlign = ContentAlignment.MiddleCenter;
            button6.TextImageRelation = TextImageRelation.ImageAboveText;
        }

        private void Button6_MouseLeave(object sender, EventArgs e)
        {
            button6.ImageAlign = ContentAlignment.MiddleLeft;
            button6.TextImageRelation = TextImageRelation.ImageBeforeText;
        }
    }
}

(4) 生成效果

 C#窗体中动态按钮的设计方法:创建特殊窗体-LMLPHP

C#窗体中动态按钮的设计方法:创建特殊窗体-LMLPHP

04-25 11:47