(七十八)c#Winform自定义控件-倒影组件
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
来都来了,点个【推荐】再走吧,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果

准备工作
GDI+画图,不了解先百度
开始
思路:
控件扩展属性,在组件中对需要显示倒影的控件的父控件添加Paint事件,在Paint事件中绘制控件,并旋转180,然后画到父控件上,然后再覆盖一层渐变色,完美。
添加一个类ShadowComponent 继承Component,实现 IExtenderProvider接口,扩展属性
代码比较少,直接放上来了
/// <summary>
/// The m control cache
/// </summary>
Dictionary<Control, bool> m_controlCache = new Dictionary<Control, bool>(); #region 构造函数 English:Constructor
/// <summary>
/// Initializes a new instance of the <see cref="ShadowComponent" /> class.
/// </summary>
public ShadowComponent()
{ } /// <summary>
/// Initializes a new instance of the <see cref="ShadowComponent" /> class.
/// </summary>
/// <param name="container">The container.</param>
public ShadowComponent(IContainer container)
: this()
{
container.Add(this);
}
#endregion /// <summary>
/// 指定此对象是否可以将其扩展程序属性提供给指定的对象。
/// </summary>
/// <param name="extendee">要接收扩展程序属性的 <see cref="T:System.Object" />。</param>
/// <returns>如果此对象可以扩展程序属性提供给指定对象,则为 true;否则为 false。</returns>
public bool CanExtend(object extendee)
{
if (extendee is Control && !(extendee is Form))
return true;
return false;
} /// <summary>
/// Gets the show shadow.
/// </summary>
/// <param name="control">The control.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
[Browsable(true), Category("自定义属性"), Description("是否显示倒影"), DisplayName("ShowShadow"), Localizable(true)]
public bool GetShowShadow(Control control)
{
if (m_controlCache.ContainsKey(control))
return m_controlCache[control];
else
return false;
} /// <summary>
/// Sets the show shadow.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="isShowShadow">if set to <c>true</c> [is show shadow].</param>
public void SetShowShadow(Control control, bool isShowShadow)
{
control.ParentChanged += control_ParentChanged;
m_controlCache[control] = isShowShadow;
} /// <summary>
/// Handles the ParentChanged event of the control control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void control_ParentChanged(object sender, EventArgs e)
{
Control control = sender as Control;
if (control.Parent != null && m_controlCache[control])
{
if (!lstPaintEventControl.Contains(control.Parent))
{
lstPaintEventControl.Add(control.Parent);
Type type = control.Parent.GetType();
System.Reflection.PropertyInfo pi = type.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
pi.SetValue(control.Parent, true, null);
control.Parent.Paint += Parent_Paint;
}
}
} /// <summary>
/// The LST paint event control
/// </summary>
List<Control> lstPaintEventControl = new List<Control>();
/// <summary>
/// The shadow height
/// </summary>
private float shadowHeight = 0.3f; /// <summary>
/// Gets or sets the height of the shadow.
/// </summary>
/// <value>The height of the shadow.</value>
[Browsable(true), Category("自定义属性"), Description("倒影高度,0-1"), Localizable(true)]
public float ShadowHeight
{
get { return shadowHeight; }
set { shadowHeight = value; }
}
/// <summary>
/// The BLN loading
/// </summary>
bool blnLoading = false;
/// <summary>
/// Handles the Paint event of the Parent control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
void Parent_Paint(object sender, PaintEventArgs e)
{
if (blnLoading)
return;
if (shadowHeight > )
{
var control = sender as Control;
var lst = m_controlCache.Where(p => p.Key.Parent == control && p.Value);
if (lst != null && lst.Count() > )
{
blnLoading = true;
e.Graphics.SetGDIHigh();
foreach (var item in lst)
{
Control _control = item.Key; using (Bitmap bit = new Bitmap(_control.Width, _control.Height))
{
_control.DrawToBitmap(bit, _control.ClientRectangle);
using (Bitmap bitNew = new Bitmap(bit.Width, (int)(bit.Height * shadowHeight)))
{
using (var g = Graphics.FromImage(bitNew))
{
g.DrawImage(bit, new RectangleF(, , bitNew.Width, bitNew.Height), new RectangleF(, bit.Height - bit.Height * shadowHeight, bit.Width, bit.Height * shadowHeight), GraphicsUnit.Pixel);
}
bitNew.RotateFlip(RotateFlipType.Rotate180FlipNone);
e.Graphics.DrawImage(bitNew, new Point(_control.Location.X, _control.Location.Y + _control.Height + ));
Color bgColor = GetParentColor(_control);
LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(_control.Location.X, _control.Location.Y + _control.Height + , bitNew.Width, bitNew.Height), Color.FromArgb(, bgColor), bgColor, 90f); //75f 表示角度
e.Graphics.FillRectangle(lgb, new Rectangle(new Point(_control.Location.X, _control.Location.Y + _control.Height + ), bitNew.Size));
}
}
}
}
}
blnLoading = false;
} /// <summary>
/// Gets the color of the parent.
/// </summary>
/// <param name="c">The c.</param>
/// <returns>Color.</returns>
private Color GetParentColor(Control c)
{
if (c.Parent.BackColor != Color.Transparent)
{
return c.Parent.BackColor;
}
return GetParentColor(c.Parent);
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(七十八)c#Winform自定义控件-倒影组件的更多相关文章
- (七十)c#Winform自定义控件-饼状图
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (七十三)c#Winform自定义控件-资源加载窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- Unity3D研究院之Jenkins的使用(七十八)
长夜漫漫无心睡眠,来一篇嘿嘿.我相信如果已经用Shell脚本完成IOS和Android打包的朋友一定需要Jenkins 怎么才能让策划打包ipa和apk?怎么才能彻底省去程序的时间,只要在同一局域网内 ...
- (七十八)使用第三方框架INTULocationManager实现定位
前面(第七十五.七十六篇)讲述了如何通过CoreLocation获取位置,授权.获取等相当复杂,如果借助于第三方框架,可以简单的实现授权与定位. 首先在GitHub中搜索LocationManager ...
- (二十)c#Winform自定义控件-有后退的窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (五十)c#Winform自定义控件-滑块
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (八十)c#Winform自定义控件-分割线标签-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (十)c#Winform自定义控件-横向列表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- 使用jQuery.extend创建一个简单的选项卡插件
选项卡样式如图,请忽略丑陋的样式,样式可以随意更改 主要是基于jquery的extend扩展出的一个简单的选项卡插件,注意:这里封装的类使用的是es6中的class,所以不兼容ie8等低版本浏览器呦! ...
- 设计模式(C#)——10享元模式
推荐阅读: 我的CSDN 我的博客园 QQ群:704621321 前言 在软件开发中,当我们需要对某些对象重复创建,且最终只需要得到单一结果.如果使用一般思维,那我们将浪费很多内存空 ...
- python 3.7.4下载与安装的问题
发病时间:2019 年 8 月12 日 周一 1.操作系统环境: Win10 64位 2.pyhon版本3.7.4 python官网地址:www.python.org 本机下载的文件名为:python ...
- Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 ...
- pytest-html报告修改与汉化
前言 Pytest框架可以使用两种测试报告,其中一种就是使用pytest-html插件生成的测试报告,但是报告中有一些信息没有什么用途或者显示的不太好看,还有一些我们想要在报告中展示的信息却没有,最近 ...
- 2019nc#2
A Eddy Walker 题意 你有n个点(0-n-1),按顺序形成一个环,初始时你在0的位子,你随机顺时针走一步或者逆时针走一步, 一旦你走到一个点后,环上所有点都被经过至少一次后,你就必须停下来 ...
- “玲珑杯”ACM比赛 Round #18 1147 - 最后你还是AK了(思维,边的贡献)
题目链接:http://www.ifrog.cc/acm/problem/1147 题解:这题很容易想到的是边的贡献也就是每条边最多被取到几次,和点的贡献类似,那些加边只要加在边贡献大的边上就行.然后 ...
- Investigating Div-Sum Property UVA - 11361
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...
- 阿里巴巴资深技术专家雷卷:值得开发者关注的 Java 8 后时代的语言特性
作者 | 阿里巴巴资深技术专家 雷卷,GitHub ID @linux-china 导读:在 Python.JavaScript 等一众编程语言崛起风靡之际,一代霸主 Java 风采虽不及当年,但仍 ...
- 【Nginx】应用场景
一.概述 二.Nginx虚拟主机配置 2.1 外网映射工具 2.2 基于虚拟主机配置域名 2.3 基于端口的虚拟主机 三.Nginx配置反向代理 3.1 反向代理的作用 3.2 反向代理的好处 3.3 ...