C# 皮肤美化——按钮

  昨天,在 钱李峰的博客 中看了有关按钮皮肤美化的文章。并跟着钱李峰大大的思路做了一遍,发现这个方法比我以前(PictureBox+图片+类库)的方法好太多了,在此特别感谢钱李峰大大。

  不知道是什么原因,在拿到别人的代码之后总想要改一下。不想了,以 钱李峰大大的 源代码 为基础,开工吧!

Assembly.GetExecutingAssembly().GetManifestResourceStream("图片的相对路径")

  这个方法对我来说,虽然很新奇,但还是很有点麻烦,有什么方法可以代替吗?第一印象就是ImageList控件,而且还有封装好的Transparent透明颜色指定器。马上就动手………………可是,茶几上总是放着杯具的。ImageList控件的呈现图像的颜色数最高是32,所以效果差了;

  既然ImageList不行,就打资源文件的主意。把图片都拉进资源文件里面,然后再改一下辅助函数……

 

代码
 
   
#region 构造函数

public LButton()
{
InitializeComponent();
_downImage
= MakeTransparent(Properties.Resources.btndown);
_moveImage
= MakeTransparent(Properties.Resources.btnfore);
_normalImage
= MakeTransparent(Properties.Resources.btnnomal);
this .SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true );
this .BackgroundImage = _normalImage;
}

#endregion

#region 辅助函数

private Bitmap MakeTransparent(Bitmap bitmap)
{
bitmap.MakeTransparent(_transparentColor);

return bitmap;
}

#endregion

这一次,终于达到目的了。但是,这样的意义好像不大。

 

  既然这样,那就一事不烦二主了。就不用Label+UserControl,直接继承Button类算了。

    [DefaultEvent("Click")]//指定组件的默认事件,可以换成其它
    public partial class LButton : UserControl
    {

 

改成

    public partial class LButton : Button
    {

 

主意:LButton的属性:FlatStyle=Flat;

   同时要在构造函数中再加一句:this.SetStyle(ControlStyles.Selectable, false);//不接收焦点,即去掉按钮的虚线框

 最后再加一个“指定背景图中被视为透明的颜色”的属性,这个按钮就改好了。虽然最终结果还是一样,但是我还是修改过了,哈哈哈……

最后最后,还是要再次感谢钱李峰大大。

最终代码:(其实没改多少东西)

 

代码
 
   
public partial class LButton : Button
{
#region 变量

// 三种不同状态下的图片
Image _normalImage = null ;
Image _moveImage
= null ;
Image _downImage
= null ;
Color _transparentColor
= Color.Empty;

#endregion

#region 属性

/// <summary>
/// 控件在正常状态下显示的图片
/// </summary>
/// 楼雨:2010-11-2 14:22
[CategoryAttribute( " 自定义 " ), DescriptionAttribute( " 控件在正常状态下显示的图片 " )]
public Image NormalImage
{
get
{
return _normalImage;

}
set
{
_normalImage
= value;
}
}

/// <summary>
/// 鼠标在控件上方按下按钮旱显示的图片
/// </summary>
/// 楼雨:2010-11-2 14:20
[CategoryAttribute( " 自定义 " ), DescriptionAttribute( " 鼠标在控件上方按下按钮时显示的图片 " )]
public Image DownImage
{
get { return _downImage; }
set
{
_downImage
= value;
}
}

/// <summary>
/// 鼠标移过控件时显示的图片
/// </summary>
/// 楼雨:2010-11-2 14:16
[CategoryAttribute( " 自定义 " ), DescriptionAttribute( " 鼠标指针移过控件时显示的图片 " )]
public Image MoveImage
{
get { return _moveImage; }
set
{
_moveImage
= value;
}
}

/// <summary>
/// 指定背景图中被视为透明的颜色
/// </summary>
/// <value>
/// </value>
/// 楼雨:2010-11-2 14:06
[CategoryAttribute( " 自定义 " ), DescriptionAttribute( " 指定背景图中被视为透明的颜色 " )]
public Color TransparentColor
{
get { return _transparentColor; }
set { _transparentColor = value; }
}
#endregion

#region 构造函数

public LButton()
{
InitializeComponent();
_transparentColor
= Color.FromArgb( 255 , 0 , 255 );
_downImage
= MakeTransparent(Properties.Resources.btndown);
_moveImage
= MakeTransparent(Properties.Resources.btnfore);
_normalImage
= MakeTransparent(Properties.Resources.btnnomal);
this .SetStyle(ControlStyles.Selectable, false ); // 不接收焦点,即去掉按钮的虚线框
this .SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true );
this .BackgroundImage = _normalImage;
}

#endregion

#region 辅助函数

private Bitmap MakeTransparent(Bitmap bitmap)
{
bitmap.MakeTransparent(_transparentColor);

return bitmap;
}

#endregion

#region 事件

private void LButton_MouseDown( object sender, MouseEventArgs e)
{
this .BackgroundImage = _downImage;
}

private void LButton_MouseEnter( object sender, EventArgs e)
{
this .BackgroundImage = _moveImage;
this .Cursor = Cursors.Hand;
}

private void LButton_MouseLeave( object sender, EventArgs e)
{
this .BackgroundImage = _normalImage;
this .Cursor = Cursors.Default;
}

private void LButton_MouseUp( object sender, MouseEventArgs e)
{
this .BackgroundImage = _normalImage;
}

#endregion
}

 

转载于:https://www.cnblogs.com/louyu/archive/2010/11/02/1867447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值