C#實(shí)現(xiàn)窗體中動(dòng)態(tài)按鈕的設(shè)計(jì)方法
前言
在窗體界面中,通常以按鈕來代替菜單欄的功能,這種形式雖然給用戶一種直觀、界面風(fēng)格各異的感覺,但通常按鈕都是以靜止的形式顯示,當(dāng)光標(biāo)移到按鈕上時(shí),可以使按鈕上的圖片和文字說明動(dòng)態(tài)化,使用戶快捷地找到所選按鈕,這時(shí)就需要一個(gè)動(dòng)態(tài)按鈕的窗體界面。
1.動(dòng)態(tài)按鈕的設(shè)計(jì)方法
就是使得按鈕的Image屬性加載資源圖片,再進(jìn)一步設(shè)置其他屬性,使得按鈕的外觀變?yōu)椋簣D片+文字,或者圖片,或者特效文字。
// // 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;
還可以用事件動(dòng)態(tài)改變按鈕布局:
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.實(shí)例
(1) Resources.Designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成。
// 運(yùn)行時(shí)版本:4.0.30319.42000
//
// 對(duì)此文件的更改可能會(huì)導(dǎo)致不正確的行為,并且如果
// 重新生成代碼,這些更改將會(huì)丟失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace _195.Properties {
using System;
/// <summary>
/// 一個(gè)強(qiáng)類型的資源類,用于查找本地化的字符串等。
/// </summary>
// 此類是由 StronglyTypedResourceBuilder
// 類通過類似于 ResGen 或 Visual Studio 的工具自動(dòng)生成的。
// 若要添加或移除成員,請(qǐng)編輯 .ResX 文件,然后重新運(yùn)行 ResGen
// (以 /str 作為命令選項(xiàng)),或重新生成 VS 項(xiàng)目。
[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 實(shí)例。
/// </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>
/// 重寫當(dāng)前線程的 CurrentUICulture 屬性,對(duì)
/// 使用此強(qiáng)類型資源類的所有資源查找執(zhí)行重寫。
/// </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 = "員工調(diào)動(dòng)管理";
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 = "動(dòng)態(tài)按鈕";
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#實(shí)現(xiàn)窗體中動(dòng)態(tài)按鈕的設(shè)計(jì)方法的詳細(xì)內(nèi)容,更多關(guān)于C#窗體動(dòng)態(tài)按鈕的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#批量插入數(shù)據(jù)到Sqlserver中的三種方式
這篇文章主要為大家詳細(xì)介紹了C#批量插入數(shù)據(jù)到Sqlserver中的三種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
C#?Winform實(shí)現(xiàn)進(jìn)度條顯示
這篇文章主要為大家詳細(xì)介紹了C#?Winform實(shí)現(xiàn)進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
c# 獲取CookieContainer的所有cookies函數(shù)代碼
這篇文章主要介紹了c# 獲取CookieContainer所有cookies的函數(shù)代碼,需要的朋友可以參考下2013-06-06
C#實(shí)現(xiàn)tostring轉(zhuǎn)換成16進(jìn)制的方法
本文介紹了在C#中將整數(shù)、字節(jié)數(shù)組、字符串轉(zhuǎn)換為十六進(jìn)制字符串,以及將十六進(jìn)制字符串轉(zhuǎn)換回整數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Js中的substring,substr與C#中的Substring比較
本篇文章主要是對(duì)Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01

