1.解决方案下添加新建项目新建类库
2. 在项目下添加新建项选择新建组件类
3.点击项目属性,选择资源,添加资源,把准备好的图片加入Resource
4.先引用,然后导入两个命名空间
5.因为是扩展控件,把继承自Component改成继承自Button
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Diagnostics; 5 using System.Linq; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Drawing; 9 namespace FJZControl 10 { 11 public partial class FJZButton : Button 12 { 13 public FJZButton() 14 { 15 InitializeComponent(); 16 } 17 18 public FJZButton(IContainer container) 19 { 20 container.Add(this); 21 22 InitializeComponent(); 23 } 24 #region 枚举类型 25 public enum ButtonPreSetImage 26 { None, 27 Alarm, 28 Folder, 29 DownLoad, 30 Password, 31 Power 32 } 33 #endregion 34 35 #region 控件属性 36 private ButtonPreSetImage preSetButtonImage; 37 [Category("FJZ自定义Button属性")] 38 [Description("按钮样式选择")] 39 public ButtonPreSetImage PreSetButtonImage 40 { 41 get 42 { 43 return preSetButtonImage; 44 } 45 46 set 47 { 48 preSetButtonImage = value; 49 switch (preSetButtonImage) 50 { 51 case ButtonPreSetImage.None: 52 Image = null; 53 break; 54 case ButtonPreSetImage.Alarm: 55 Image = Properties.Resources.alarm; 56 break; 57 case ButtonPreSetImage.DownLoad: 58 Image = Properties.Resources.Download; 59 break; 60 case ButtonPreSetImage.Folder: 61 Image = Properties.Resources.folder; 62 break; 63 case ButtonPreSetImage.Password: 64 Image = Properties.Resources.password; 65 break; 66 case ButtonPreSetImage.Power: 67 Image = Properties.Resources.password; 68 break; 69 default: 70 break; 71 } 72 if (preSetButtonImage == ButtonPreSetImage.None) 73 { 74 this.ImageAlign = ContentAlignment.MiddleCenter; 75 this.TextAlign = ContentAlignment.MiddleCenter; 76 77 } 78 else 79 { 80 this.ImageAlign = ContentAlignment.MiddleLeft; 81 this.TextAlign = ContentAlignment.MiddleRight; 82 } 83 84 } 85 } 86 87 #endregion 88 } 89 }
显示效果