关于图片按钮的使用

1.直接使用编译好的PicButton.dll在项目中添加引用,在"工具栏"--右键--选择项--.NET Framework 组件--浏览--找到PicButton添加--PicButton图片按钮在工具栏中

2.在项目中编辑PicButton项目,其实上边方法的dll文件 就是在这里编译出来的具体实现方法:

 1.在项目中在重新新建一个项目(添加类库),如下图中PicButton类库

  

添加完后:

添加引用,并编辑class1.cs,如下:

     

class1.cs修改后如下,当把class1修改为继承Control时,注意到Class1的文件标志发生了变化:

然后生成项目,可以看到在窗口上看到Class1控件(也可以根据需要改成自己喜欢的名字),将其添加到窗体上改写

 

 

  1. using System;   
  2.   
  3. using System.Drawing;   
  4.   
  5. using System.Drawing.Imaging;   
  6.   
  7. using System.Windows.Forms;   
  8.   
  9. using System.Collections.Generic;   
  10.   
  11. using System.Text;   
  12.   
  13. using System.ComponentModel;   
  14.   
  15. using System.Runtime.InteropServices;   
  16.   
  17.   
  18.   
  19. namespace PicButton   
  20.   
  21. {   
  22.   
  23.     public class Class1:Control   
  24.   
  25.     {   
  26.   
  27.         //私有成员     
  28.         private Image imageOri, imageHover;   
  29.         //表示按下状态的标记   
  30.         private bool bPushed, bHovered;   
  31.         //缓冲位图   
  32.         private Bitmap m_bmpOffscreen;   
  33.         //是否自动调整控件大小,以适合不同大小的图片   
  34.         private bool bAutoSetSize;   
  35.         public Class1():base()   
  36.         {   
  37.             bPushed = false;   
  38.             bHovered = false;   
  39.             bAutoSetSize = true;   
  40.         }   
  41.         [Description("默认的的图片,大小应与ImageHover一致"), Category("自定义")]   
  42.         public Image ImageOri   
  43.         {   
  44.             get  
  45.             {   
  46.                 return imageOri;   
  47.             }   
  48.             set  
  49.             { imageOri = value;   
  50.                 if (bAutoSetSize)   
  51.                     SetSizeToImage(value);   
  52.                 this.Invalidate();   
  53.             }   
  54.         }   
  55.         [Description("鼠标悬停时的图片,大小应与ImageOriginal一致"), Category("自定义")]   
  56.         public Image ImageHover   
  57.         {   
  58.             get  
  59.             { return imageHover;   
  60.             }   
  61.             set  
  62.             {  imageHover = value;   
  63.                 if (bAutoSetSize)   
  64.                    SetSizeToImage(value);   
  65.                 this.Invalidate();   
  66.             }   
  67.         }   
  68.         [Description("是否自动调整以适应ImageOri的大小"), Category("自定义")]   
  69.         public bool AutoSetSize   
  70.         {  get  
  71.             {   return bAutoSetSize;   
  72.             }   
  73.   
  74.             set  
  75.             {   
  76.                 bAutoSetSize = value;   
  77.                 if (bAutoSetSize)   
  78.                  {   
  79.                     SetSizeToImage(this.imageOri);   
  80.                 }   
  81.             }   
  82.         }   
  83.         protected override void OnPaintBackground(PaintEventArgs e)   
  84.          {   
  85.             //不执行任何操作   
  86.           }   
  87.         protected override void OnClick(EventArgs e)   
  88.         {   
  89.             //触发Click事件前更新UI,避免事件处理完毕才响应UI   
  90.             this.bPushed = false;   
  91.             this.bHovered = true;   
  92.             this.Refresh();   
  93.             base.OnClick(e);   
  94.         }   
  95.        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)   
  96.         {   
  97.            base.OnPaint(e);   
  98.             Graphics gxOff;      //屏幕外的图像   
  99.             Rectangle imgRect; //图像矩形   
  100.             if (imageOri == null)   
  101.             {   
  102.                e.Graphics.Clear(this.BackColor);   
  103.                 return;   
  104.            }   
  105.   
  106.             if (m_bmpOffscreen == null//要双缓冲的位图   
  107.             {//见OnResize方法   
  108.                 m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);   
  109.             }   
  110.   
  111.            gxOff = Graphics.FromImage(m_bmpOffscreen);   
  112.             gxOff.Clear(Parent.BackColor);   
  113.             ImageAttributes imageAttr = new ImageAttributes();   
  114.            Image imageNow = imageOri;   
  115.            //将图像相对于控件居中   
  116.             imgRect = new Rectangle((this.Width - imageNow.Width) / 2, (this.Height - imageNow.Height) / 2,   
  117.   
  118.                                     imageNow.Width, imageNow.Height);   
  119.             if (this.Enabled && bPushed && imageHover != null)   
  120.             {   
  121.                 imageNow = imageHover;   
  122.                 imgRect = new Rectangle((this.Width - imageNow.Width) / 2, (this.Height - imageNow.Height) / 2,             
  1.            imageNow.Width, imageNow.Height);   
  2.         imgRect.Offset(1, 1);   
  3.    }   
  4.   else if (this.Enabled && bHovered && imageHover != null)   
  5.     {   
  6.         imageNow = imageHover;   
  7.     }   
  8.    imageAttr.SetColorKey(BackgroundImageColor(imageNow), BackgroundImageColor(imageNow));   
  9.     //如果不可用,使用变换办法,将当前图象变换为灰度   
  10.     if (!this.Enabled)   
  11.        GetColorTrans(imageAttr);   
  12.     //绘制图像   
  13.     gxOff.DrawImage(imageNow, imgRect, 0, 0, imageNow.Width, imageNow.Height, GraphicsUnit.Pixel, imageAttr);   
  14.     //从内存位图绘制   
  15.     e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);   
  16.     gxOff.Dispose();   
  17.     gxOff = null;   
  18.   
  19. private void SetSizeToImage(Image img)   
  20. {   
  21.     if (img != null)   
  22.     {   
  23.         System.Drawing.Rectangle r = new Rectangle(0, 0, img.Width, img.Height);   
  24.         r.Inflate(2, 2);   
  25.         this.Width = r.Width;   
  26.         this.Height = r.Height;   
  27.     }   
  28. }   
  29.   
  30. private Color BackgroundImageColor(Image image)   
  31. {   
  32.    Bitmap bmp = new Bitmap(image);   
  33.     return bmp.GetPixel(0, 0);   
  34. }   
  35.   
  36. private void GetColorTrans(ImageAttributes imageAttributes)   
  37. {   
  38.     //将当前r,g,b彩色分量按公式grey   =   r   *   0.39   +   g   *   0.50   +   b   *   0.11进行变换   
  39.     //注意是 r=grey,g=grey,b=grey   
  40.     float[][] colorMatrixElements = {    
  41.   
  42.                 new float[] {0.39f,  0.39f,  0.39f,  0, 0},        // red scaling factor of 2   
  43.   
  44.                 new float[] {0.50f,  0.50f,  0.50f,  0, 0},        // green scaling factor of 1   
  45.   
  46.                 new float[] {0.11f,  0.11f,  0.11f,  0, 0},        // blue scaling factor of 1   
  47.   
  48.                 new float[] {0,  0,  0,  1, 0},        // alpha scaling factor of 1   
  49.   
  50.                 new float[] {0,  0,  0, 0, 1}};    // three translations of 0.2   
  51.   
  52.   
  53.   
  54.     ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);   
  55.     imageAttributes.SetColorMatrix(   
  56.       colorMatrix,   
  57.         ColorMatrixFlag.Default,   
  58.       ColorAdjustType.Bitmap);  
  1.  }   
  2.   
  3. public new bool Enabled   
  4.  {   
  5.     get { return base.Enabled; }   
  6.      set  
  7.      {   
  8.          base.Enabled = value;   
  9.          this.Invalidate();   
  10.      }   
  11.   
  12.   
  13. protected override void OnMouseEnter(EventArgs e)   
  14.  {   
  15.      base.OnMouseEnter(e);   
  16.      bHovered = true;   
  17.      this.Invalidate();   
  18.  }   
  19.  protected override void OnMouseLeave(EventArgs e)   
  20.  {   
  21.      base.OnMouseLeave(e);   
  22.      bHovered = false;   
  23.      this.Invalidate();  
  1. }   
  2. protected override void OnMouseMove(MouseEventArgs e)   
  3.   
  4. {//鼠标处于按下状态时移到客户区之外仍会接收Move消息,但不会接收Leave消息   
  5.   
  6.     //因此需要在Move消息中判断当前鼠标是否在客户区内,并触发相应的OnMouseLeave或OnMouseEnter   
  7.   
  8.     base.OnMouseMove(e);   
  9.     if (e.Button == MouseButtons.Left)   
  10.    {   
  11.        if (!this.ClientRectangle.Contains(e.X, e.Y))   
  12.         {//鼠标处于按下状态时从客户区内移到客户区之外时,模拟OnMouseLeave   
  13.             if (this.bHovered)   
  14.             {   
  15.                 this.bPushed = false;   
  16.                 OnMouseLeave(EventArgs.Empty);   
  17.             }   
  18.         }   
  19.   
  20.         else  
  21.         {//鼠标处于按下状态时从客户区内移到客户区之外,再移回客户区内时,模拟OnMouseEnter,同时bPushed = true   
  22.   
  23.             if (!this.bHovered)   
  24.                 bPushed = true;   
  25.             OnMouseEnter(EventArgs.Empty);   
  26.         }   
  27.   
  28.     }//以下为增加的处理:   
  29.   
  30.     else  
  31.     {//***模拟OnMouseEnter,参见 关于空闲处理的必要性   
  32.   
  33.         if (bHovered == false)   
  34.   
  35.         {//***收得到Move消息时鼠标也不一定是在客户区内,必须进行if (this.ClientRectangle.Contains(e.X, e.Y))判断   
  36.   
  37.             if (this.ClientRectangle.Contains(e.X, e.Y))  
  1.                     {   
  2.                         bHovered = true;   
  3.                         this.Invalidate();   
  4.                     }   
  5.                 }   
  6.             }   
  7.   
  8.         }   
  9.   
  10.   
  11.   
  12.         protected override void OnResize(EventArgs e)   
  13.         {   
  14.            base.OnResize(e);   
  15.             /*重要,为节省资源,不在每次paint时都调用生成缓冲位图  
  16.              * 但在大小改变时必须重新生成新的大小的m_bmpOffscreen以适应新的大小  
  17.             */  
  18.             m_bmpOffscreen = null;   
  19.             this.Invalidate();   
  20.         }   
  21.   
  22.   
  23.   
  24.         protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)   
  25.         {   
  26.             base.OnMouseDown(e);   
  27.            if (e.Button == MouseButtons.Left)   
  28.             {   
  29.                 bPushed = true;   
  30.                 this.Invalidate();   
  31.             }   
  32.         }   
  33.         protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)   
  34.   
  35.         {  base.OnMouseUp(e);   
  36.             bPushed = false;   
  37.             this.Invalidate();   
  38.   
  39.         }   
  40.         public void PerformClick()   
  41.         {   
  42.            this.OnClick(EventArgs.Empty);   
  43.        }   
  44. }   
  45.   
  46. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值