前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
来都来了,点个【推荐】再走吧,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
GDI+画图,不了解先百度
开始
思路:
控件扩展属性,在组件中对需要显示倒影的控件的父控件添加Paint事件,在Paint事件中绘制控件,并旋转180,然后画到父控件上,然后再覆盖一层渐变色,完美。
添加一个类ShadowComponent 继承Component,实现 IExtenderProvider接口,扩展属性
代码比较少,直接放上来了
1 /// <summary> 2 /// The m control cache 3 /// </summary> 4 Dictionary<Control, bool> m_controlCache = new Dictionary<Control, bool>(); 5 6 #region 构造函数 English:Constructor 7 /// <summary> 8 /// Initializes a new instance of the <see cref="ShadowComponent" /> class. 9 /// </summary> 10 public ShadowComponent() 11 { 12 13 } 14 15 /// <summary> 16 /// Initializes a new instance of the <see cref="ShadowComponent" /> class. 17 /// </summary> 18 /// <param name="container">The container.</param> 19 public ShadowComponent(IContainer container) 20 : this() 21 { 22 container.Add(this); 23 } 24 #endregion 25 26 /// <summary> 27 /// 指定此对象是否可以将其扩展程序属性提供给指定的对象。 28 /// </summary> 29 /// <param name="extendee">要接收扩展程序属性的 <see cref="T:System.Object" />。</param> 30 /// <returns>如果此对象可以扩展程序属性提供给指定对象,则为 true;否则为 false。</returns> 31 public bool CanExtend(object extendee) 32 { 33 if (extendee is Control && !(extendee is Form)) 34 return true; 35 return false; 36 } 37 38 /// <summary> 39 /// Gets the show shadow. 40 /// </summary> 41 /// <param name="control">The control.</param> 42 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 43 [Browsable(true), Category("自定义属性"), Description("是否显示倒影"), DisplayName("ShowShadow"), Localizable(true)] 44 public bool GetShowShadow(Control control) 45 { 46 if (m_controlCache.ContainsKey(control)) 47 return m_controlCache[control]; 48 else 49 return false; 50 } 51 52 /// <summary> 53 /// Sets the show shadow. 54 /// </summary> 55 /// <param name="control">The control.</param> 56 /// <param name="isShowShadow">if set to <c>true</c> [is show shadow].</param> 57 public void SetShowShadow(Control control, bool isShowShadow) 58 { 59 control.ParentChanged += control_ParentChanged; 60 m_controlCache[control] = isShowShadow; 61 } 62 63 /// <summary> 64 /// Handles the ParentChanged event of the control control. 65 /// </summary> 66 /// <param name="sender">The source of the event.</param> 67 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 68 void control_ParentChanged(object sender, EventArgs e) 69 { 70 Control control = sender as Control; 71 if (control.Parent != null && m_controlCache[control]) 72 { 73 if (!lstPaintEventControl