前言
在窗体界面中,通常以按钮来代替菜单栏的功能,这种形式虽然给用户一种直观、界面风格各异的感觉,但通常按钮都是以静止的形式显示,当光标移到按钮上时,可以使按钮上的图片和文字说明动态化,使用户快捷地找到所选按钮,这时就需要一个动态按钮的窗体界面。
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#实现窗体中动态按钮的设计方法的详细内容,更多关于c#窗体动态按钮的资料请关注代码网其它相关文章!
发表评论