博客
关于我
(七十八)c#Winform自定义控件-倒影组件-HZHControls
阅读量:408 次
发布时间:2019-03-06

本文共 7176 字,大约阅读时间需要 23 分钟。

官网

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:

码云:

如果觉得写的还行,请点个star支持一下吧

欢迎前来交流探讨:企鹅群568015492

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

用处及效果

准备工作

GDI+画图,不了解先百度

开始

思路:

控件扩展属性,在组件中对需要显示倒影的控件的父控件添加Paint事件,在Paint事件中绘制控件,并旋转180,然后画到父控件上,然后再覆盖一层渐变色,完美。

添加一个类ShadowComponent继承Component,实现IExtenderProvider接口,扩展属性

代码比较少,直接放上来了

1 ///   2         /// The m control cache  3         ///   4         Dictionary m_controlCache = new Dictionary();  5   6         #region 构造函数    English:Constructor  7         ///         8         /// Initializes a new instance of the         class.  9         ///   10         public ShadowComponent()  11         {  12  13         }  14  15         ///         16         /// Initializes a new instance of the         class.  17         ///   18         ///       The container.  19         public ShadowComponent(IContainer container)  20             : this()  21         {  22             container.Add(this);  23         }  24         #endregion  25  26         ///         27         /// 指定此对象是否可以将其扩展程序属性提供给指定的对象。  28         ///   29         ///       要接收扩展程序属性的       。  30         ///              如果此对象可以扩展程序属性提供给指定对象,则为 true;否则为 false。         31         public bool CanExtend(object extendee)  32         {  33             if (extendee is Control && !(extendee is Form))  34                 return true;  35             return false;  36         }  37  38         ///         39         /// Gets the show shadow.  40         ///   41         ///       The control.  42         ///                      true         if XXXX,                false         otherwise.         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         ///         53         /// Sets the show shadow.  54         ///   55         ///       The control.  56         ///       if set to              true        [is show shadow].  57         public void SetShowShadow(Control control, bool isShowShadow)  58         {  59             control.ParentChanged += control_ParentChanged;  60             m_controlCache[control] = isShowShadow;  61         }  62  63         ///         64         /// Handles the ParentChanged event of the control control.  65         ///   66         ///       The source of the event.  67         ///         The          instance containing the event data.  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.Contains(control.Parent))  74                 {  75                     lstPaintEventControl.Add(control.Parent);  76                     Type type = control.Parent.GetType();  77                     System.Reflection.PropertyInfo pi = type.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);  78                     pi.SetValue(control.Parent, true, null);  79                     control.Parent.Paint += Parent_Paint;  80                 }  81             }  82         }  83  84         ///         85         /// The LST paint event control  86         ///   87         List lstPaintEventControl = new List();  88         ///           89         /// The shadow height  90         ///   91         private float shadowHeight = 0.3f;  92  93         ///           94         /// Gets or sets the height of the shadow.  95         ///   96         ///                  The height of the shadow.           97         [Browsable(true), Category("自定义属性"), Description("倒影高度,0-1"), Localizable(true)]  98         public float ShadowHeight  99         {100             get { return shadowHeight; }101             set { shadowHeight = value; }102         }  103         ///         104         /// The BLN loading105         /// 106         bool blnLoading = false;107         ///         108         /// Handles the Paint event of the Parent control.109         /// 110         ///         The source of the event.111         ///         The          instance containing the event data.112         void Parent_Paint(object sender, PaintEventArgs e)113         {114             if (blnLoading)115                 return;           116             if (shadowHeight > 0)117             {118                 var control = sender as Control;119                 var lst = m_controlCache.Where(p => p.Key.Parent == control && p.Value);120                 if (lst != null && lst.Count() > 0)121                 {122                     blnLoading = true;123                     e.Graphics.SetGDIHigh();124                     foreach (var item in lst)125                     {126                         Control _control = item.Key;127  128                         using (Bitmap bit = new Bitmap(_control.Width, _control.Height))129                         {130                             _control.DrawToBitmap(bit, _control.ClientRectangle);131                             using (Bitmap bitNew = new Bitmap(bit.Width, (int)(bit.Height * shadowHeight)))132                             {133                                 using (var g = Graphics.FromImage(bitNew))134                                 {135                                     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);136                                 }137                                 bitNew.RotateFlip(RotateFlipType.Rotate180FlipNone);138                                 e.Graphics.DrawImage(bitNew, new Point(_control.Location.X, _control.Location.Y + _control.Height + 1));139                                 Color bgColor = GetParentColor(_control);140                                 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 表示角度141                                 e.Graphics.FillRectangle(lgb, new Rectangle(new Point(_control.Location.X, _control.Location.Y + _control.Height + 1), bitNew.Size));142                             }143                         }144                     }145                 }146             }147             blnLoading = false;148         }149 150         ///         151         /// Gets the color of the parent.152         /// 153         ///         The c.154         ///                  Color.         155         private Color GetParentColor(Control c)156         {157             if (c.Parent.BackColor != Color.Transparent)158             {159                 return c.Parent.BackColor;160             }161             return GetParentColor(c.Parent);162         }

 

最后的话

如果你喜欢的话,请到  点个star吧

转载地址:http://odvkz.baihongyu.com/

你可能感兴趣的文章
NOIp模拟赛二十九
查看>>
Vue3+element plus+sortablejs实现table列表拖拽
查看>>
Nokia5233手机和我装的几个symbian V5手机软件
查看>>
non linear processor
查看>>
Non-final field ‘code‘ in enum StateEnum‘
查看>>
none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
查看>>
None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
查看>>
NoNodeAvailableException None of the configured nodes are available异常
查看>>
Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
查看>>
nopcommerce商城系统--文档整理
查看>>
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
NoSQL数据库概述
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
NOTE:rfc5766-turn-server
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>