ShadowComponent.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-28
  4. //
  5. // ***********************************************************************
  6. // <copyright file="ShadowComponent.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Drawing;
  21. using System.Windows.Forms;
  22. using System.ComponentModel;
  23. using System.Drawing.Drawing2D;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class ShadowComponent.
  28. /// Implements the <see cref="System.ComponentModel.Component" />
  29. /// Implements the <see cref="System.ComponentModel.IExtenderProvider" />
  30. /// </summary>
  31. /// <seealso cref="System.ComponentModel.Component" />
  32. /// <seealso cref="System.ComponentModel.IExtenderProvider" />
  33. [ProvideProperty("ShowShadow", typeof(Control))]
  34. public class ShadowComponent : Component, IExtenderProvider
  35. {
  36. /// <summary>
  37. /// The m control cache
  38. /// </summary>
  39. Dictionary<Control, bool> m_controlCache = new Dictionary<Control, bool>();
  40. #region 构造函数 English:Constructor
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="ShadowComponent" /> class.
  43. /// </summary>
  44. public ShadowComponent()
  45. {
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="ShadowComponent" /> class.
  49. /// </summary>
  50. /// <param name="container">The container.</param>
  51. public ShadowComponent(IContainer container)
  52. : this()
  53. {
  54. container.Add(this);
  55. }
  56. #endregion
  57. /// <summary>
  58. /// 指定此对象是否可以将其扩展程序属性提供给指定的对象。
  59. /// </summary>
  60. /// <param name="extendee">要接收扩展程序属性的 <see cref="T:System.Object" />。</param>
  61. /// <returns>如果此对象可以扩展程序属性提供给指定对象,则为 true;否则为 false。</returns>
  62. public bool CanExtend(object extendee)
  63. {
  64. if (extendee is Control && !(extendee is Form))
  65. return true;
  66. return false;
  67. }
  68. /// <summary>
  69. /// Gets the show shadow.
  70. /// </summary>
  71. /// <param name="control">The control.</param>
  72. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  73. [Browsable(true), Category("自定义属性"), Description("是否显示倒影"), DisplayName("ShowShadow"), Localizable(true)]
  74. public bool GetShowShadow(Control control)
  75. {
  76. if (m_controlCache.ContainsKey(control))
  77. return m_controlCache[control];
  78. else
  79. return false;
  80. }
  81. /// <summary>
  82. /// Sets the show shadow.
  83. /// </summary>
  84. /// <param name="control">The control.</param>
  85. /// <param name="isShowShadow">if set to <c>true</c> [is show shadow].</param>
  86. public void SetShowShadow(Control control, bool isShowShadow)
  87. {
  88. control.ParentChanged += control_ParentChanged;
  89. m_controlCache[control] = isShowShadow;
  90. }
  91. /// <summary>
  92. /// Handles the ParentChanged event of the control control.
  93. /// </summary>
  94. /// <param name="sender">The source of the event.</param>
  95. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  96. void control_ParentChanged(object sender, EventArgs e)
  97. {
  98. Control control = sender as Control;
  99. if (control.Parent != null && m_controlCache[control])
  100. {
  101. if (!lstPaintEventControl.Contains(control.Parent))
  102. {
  103. lstPaintEventControl.Add(control.Parent);
  104. Type type = control.Parent.GetType();
  105. System.Reflection.PropertyInfo pi = type.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  106. pi.SetValue(control.Parent, true, null);
  107. control.Parent.Paint += Parent_Paint;
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// The LST paint event control
  113. /// </summary>
  114. List<Control> lstPaintEventControl = new List<Control>();
  115. /// <summary>
  116. /// The shadow height
  117. /// </summary>
  118. private float shadowHeight = 0.3f;
  119. /// <summary>
  120. /// Gets or sets the height of the shadow.
  121. /// </summary>
  122. /// <value>The height of the shadow.</value>
  123. [Browsable(true), Category("自定义属性"), Description("倒影高度,0-1"), Localizable(true)]
  124. public float ShadowHeight
  125. {
  126. get { return shadowHeight; }
  127. set { shadowHeight = value; }
  128. }
  129. /// <summary>
  130. /// The BLN loading
  131. /// </summary>
  132. bool blnLoading = false;
  133. /// <summary>
  134. /// Handles the Paint event of the Parent control.
  135. /// </summary>
  136. /// <param name="sender">The source of the event.</param>
  137. /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
  138. void Parent_Paint(object sender, PaintEventArgs e)
  139. {
  140. if (blnLoading)
  141. return;
  142. if (shadowHeight > 0)
  143. {
  144. var control = sender as Control;
  145. var lst = m_controlCache.Where(p => p.Key.Parent == control && p.Value);
  146. if (lst != null && lst.Count() > 0)
  147. {
  148. blnLoading = true;
  149. e.Graphics.SetGDIHigh();
  150. foreach (var item in lst)
  151. {
  152. Control _control = item.Key;
  153. using (Bitmap bit = new Bitmap(_control.Width, _control.Height))
  154. {
  155. _control.DrawToBitmap(bit, _control.ClientRectangle);
  156. using (Bitmap bitNew = new Bitmap(bit.Width, (int)(bit.Height * shadowHeight)))
  157. {
  158. using (var g = Graphics.FromImage(bitNew))
  159. {
  160. g.DrawImage(bit, new RectangleF(0, 0, bitNew.Width, bitNew.Height), new RectangleF(0, bit.Height - bit.Height * shadowHeight, bit.Width, bit.Height * shadowHeight), GraphicsUnit.Pixel);
  161. }
  162. bitNew.RotateFlip(RotateFlipType.RotateNoneFlipY);
  163. e.Graphics.DrawImage(bitNew, new Point(_control.Location.X, _control.Location.Y + _control.Height + 1));
  164. Color bgColor = GetParentColor(_control);
  165. LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(_control.Location.X, _control.Location.Y + _control.Height + 1, bitNew.Width, bitNew.Height), Color.FromArgb(50, bgColor), bgColor, 90f); //75f 表示角度
  166. e.Graphics.FillRectangle(lgb, new Rectangle(new Point(_control.Location.X, _control.Location.Y + _control.Height + 1), bitNew.Size));
  167. }
  168. }
  169. }
  170. }
  171. }
  172. blnLoading = false;
  173. }
  174. /// <summary>
  175. /// Gets the color of the parent.
  176. /// </summary>
  177. /// <param name="c">The c.</param>
  178. /// <returns>Color.</returns>
  179. private Color GetParentColor(Control c)
  180. {
  181. if (c.Parent.BackColor != Color.Transparent)
  182. {
  183. return c.Parent.BackColor;
  184. }
  185. return GetParentColor(c.Parent);
  186. }
  187. }
  188. }