官网

http://www.hzhcontrols.com

前提

入行已经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自定义控件-倒影组件的更多相关文章

  1. (七十)c#Winform自定义控件-饼状图

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  2. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (七十三)c#Winform自定义控件-资源加载窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  4. Unity3D研究院之Jenkins的使用(七十八)

    长夜漫漫无心睡眠,来一篇嘿嘿.我相信如果已经用Shell脚本完成IOS和Android打包的朋友一定需要Jenkins 怎么才能让策划打包ipa和apk?怎么才能彻底省去程序的时间,只要在同一局域网内 ...

  5. (七十八)使用第三方框架INTULocationManager实现定位

    前面(第七十五.七十六篇)讲述了如何通过CoreLocation获取位置,授权.获取等相当复杂,如果借助于第三方框架,可以简单的实现授权与定位. 首先在GitHub中搜索LocationManager ...

  6. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (五十)c#Winform自定义控件-滑块

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  8. (八十)c#Winform自定义控件-分割线标签-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  9. (十)c#Winform自定义控件-横向列表

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. java中的case里嵌套if条件句; 输入一个年份的某一个月份,判断这个月有多少天

    public class year {    public static void main(String arg[]){        Scanner a=new Scanner(System.in ...

  2. python多进程通信实例分析

    操作系统会为每一个创建的进程分配一个独立的地址空间,不同进程的地址空间是完全隔离的,因此如果不加其他的措施,他们完全感觉不到彼此的存在.那么进程之间怎么进行通信?他们之间的关联是怎样的?实现原理是什么 ...

  3. Socket I/O模型之select模型

    socket网络编程中有多种常见的I/O模型: 1.blocking阻塞 2.nonblocking非阻塞 3.I/O multiplexing复用 4.signal driven 5.asynchr ...

  4. spring-cloud-kubernetes背后的三个关键知识点

    在<你好spring-cloud-kubernetes>一文中,对spring-cloud-kubernetes这个SpringCloud官方kubernetes服务框架有了基本了解,今天 ...

  5. Mac下搭建go和beego开发环境

     go安装 首先到golang的官网下载 直接双击安装包,安装 ,下一步,下一步....完成既可 验证安装是否成功 打开终端执行 输出 go version go1.11.5 darwin/amd64 ...

  6. lightoj 1158 - Anagram Division(记忆化搜索+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1158 题解:这题看起来就像是记忆搜索,由于s很少最多就10位所以可以考虑用状压 ...

  7. 简单粗暴详细讲解javascript实现函数柯里化与反柯里化

    函数柯里化(黑人问号脸)???Currying(黑人问号脸)???妥妥的中式翻译既视感:下面来一起看看究竟什么是函数柯里化: 维基百科的解释是:把接收多个参数的函数变换成接收一个单一参数(最初函数的第 ...

  8. 【LeetCode】56-合并区间

    题目描述 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 ...

  9. 060 Python必备库-从数据处理到人工智能

    目录 一.概述 1.1 从数据处理到人工智能 二.Python库之数据分析 2.1 numpy 2.2 pandas 2.3 scipy 三.Python库之数据可视化 3.1 matplotlib ...

  10. springcloud(四):应用配置中心config的安全设置

    springcloud应用配置中心config的安全设置 在springcloud应用开发中,为了方便在线管理我们的配置文件,通常会配一个配置中心config-server,这里托管着应用的一些配置文 ...