今天突然想让button按钮自动有鼠标触摸换背景功能即实现网页上面的mousemove,mouseout功能,于是就想到了使用自定义控件来继承button按钮类,来实现这个功能

下面来介绍如何实现,首先在.net新建用户控件模板,然后再页面设计界面上添加imageList,顺便在该控件中添加一些图片,然后开始编写代码,代码如下:

UButtondemo
 public partial class UButton:System.Windows.Forms.Button
    {
        
public UButton()
        {
            InitializeComponent();
            
this.FlatStyle = FlatStyle.Popup;
            
this.BackgroundImage = imageList1.Images[4];
            
this.ForeColor = Color.Black;
        }
        
protected override void OnMouseDown(MouseEventArgs e)
        {
            
this.BackgroundImage = imageList1.Images[5];

        }

        
protected override void  OnMouseEnter(EventArgs e)
        {
              
this.BackgroundImage = imageList1.Images[5];
           
        }

        
protected override void OnMouseLeave(EventArgs e)
        {
             
this.BackgroundImage = imageList1.Images[4];
        
        }


        
protected override void OnMouseUp(MouseEventArgs e)
        {
             
this.BackgroundImage = imageList1.Images[4];
            
        }
    }

 说明:该类继承了系统自带的button类,然后重写了几个方法,鼠标移除,移近等,编译运行。那么将在工具栏里面出现你编写的自定义控件啦。

 代码下载

posted on 2011-06-01 14:53  jackdesk  阅读(399)  评论(0)    收藏  举报