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 this.FlatStyle = FlatStyle.Flat; 17 } 18 19 public FJZButton(IContainer container) 20 { 21 container.Add(this); 22 23 InitializeComponent(); 24 } 25 #region 枚举类型 26 public enum ButtonPreSetImage 27 { None, 28 Alarm, 29 Folder, 30 DownLoad, 31 Password, 32 Power 33 } 34 #endregion 35 36 #region 控件属性 37 private ButtonPreSetImage preSetButtonImage; 38 [Category("FJZ自定义Button属性")] 39 [Description("按钮样式选择")] 40 public ButtonPreSetImage PreSetButtonImage 41 { 42 get 43 { 44 return preSetButtonImage; 45 } 46 47 set 48 { 49 preSetButtonImage = value; 50 switch (preSetButtonImage) 51 { 52 case ButtonPreSetImage.None: 53 Image = null; 54 break; 55 case ButtonPreSetImage.Alarm: 56 Image = Properties.Resources.alarm; 57 break; 58 case ButtonPreSetImage.DownLoad: 59 Image = Properties.Resources.Download; 60 break; 61 case ButtonPreSetImage.Folder: 62 Image = Properties.Resources.folder; 63 break; 64 case ButtonPreSetImage.Password: 65 Image = Properties.Resources.password; 66 break; 67 case ButtonPreSetImage.Power: 68 Image = Properties.Resources.power; 69 break; 70 default: 71 break; 72 } 73 if (preSetButtonImage == ButtonPreSetImage.None) 74 { 75 this.ImageAlign = ContentAlignment.MiddleCenter; 76 this.TextAlign = ContentAlignment.MiddleCenter; 77 78 } 79 else 80 { 81 this.ImageAlign = ContentAlignment.MiddleLeft; 82 this.TextAlign = ContentAlignment.MiddleRight; 83 } 84 85 } 86 } 87 88 #endregion 89 } 90 }
显示效果