65.9K
CodeProject 正在变化。 阅读更多。
Home

多渐变按钮

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.88/5 (5投票s)

2004年12月15日

2分钟阅读

viewsIcon

72156

downloadIcon

1678

一个允许在设计时定义多个梯度的按钮。

Sample Image - gradbutton.jpg

引言

首先,我想感谢 David Preece 编写的彩虹按钮文章。David做得很好,这启发了我!该控件具有常规按钮的所有功能,并且可以通过属性编辑器在设计时完全自定义。它还使用颜色集合(提供的 DLL)来存储颜色。

背景

为什么要限制颜色为五种?所以,这就是结果!一个多色(至少两种颜色)的渐变按钮。

如何使用代码

MTMultiGradiantButton 可以在任何 C# Windows 项目中使用。

首先,将 MTCollections.dll 作为资源添加到你的项目中。然后将 MTControls.dll 添加到 Visual Studio 或 Sharp Develop(我用来创建控件的编译器)的工具箱中。从文件中选择 MTMultiGradiantButton 控件。然后将按钮从工具箱拖放到窗体上。或者,你可以像这样自己实例化按钮

private MTControls.MTButtons.MTMultiGradiantButton mTMultiGradiantButton1;
this.mTMultiGradiantButton1= new MTControls.MTButtons.MTMultiGradiantButton();

this.mTMultiGradiantButton1.Colors.Add(System.Drawing.Color.Black);
this.mTMultiGradiantButton1.Colors.Add(System.Drawing.Color.Lime);

或者使用接受两种颜色的不同构造函数。

private MTControls.MTButtons.MTMultiGradiantButton mTMultiGradiantButton1;
this.mTMultiGradiantButton1 = new MTControls.MTButtons.MTMultiGradiantButton(
System.Drawing.Color.Black, System.Drawing.Color.Lime);

接下来,只需以两种方式添加你想要的颜色

使用属性编辑器,选择按钮的 Colors 属性,并通过界面添加你的颜色,或者以编程方式添加你的颜色,如下所示

// this creates a rainbow button with 6 colors
this.mTMultiGradiantButton3 = 
       new MTControls.MTButtons.MTMultiGradiantButton();
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Red);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.FromArgb((
      (System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0))));
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Yellow);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Green);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Blue);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Purple);

代码中的特别之处

在代码中激活使用 MTColorCollection 的能力很有趣。我需要一个动态数组,它可以保存颜色值,并且可以通过设计时期的属性界面进行设置。我创建了 MTColorCollection 类来保存颜色,通过从 CollectionBase 派生来实现。但是,使属性界面使用它就像为 MTColorCollection 类创建一个只读属性一样简单。

private MTColorCollection m_clrColors = new MTColorCollection();
// This is for the properties interface so you can add colors at design time
// it tells the interface to use the follows MTColorColletion Colors property
// as a template to create the interface for adding colors to the control
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
EditorAttribute("typeof(CollectionEditor)",
           "typeof(System.Drawing.Design.UITypeEditor)")]

public MTColorCollection Colors
{
  get
  {
    return m_clrColors;
  }
}

总结

所以请查看代码。我没有包含项目文件,因为我使用了 SharpDevelop,对于 Visual Studio .NET 用户来说它们将毫无用处。

© . All rights reserved.