官网

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连载22-for循环

    一.循环结构 在程序当中总有一些需要反复的/重复的执行的代码,假设没有循环结构,那么这段需要重复执行的代码自然式子最需要重复编写的,代码无法得到重复使用,所以多数编程语言都是支持循环结构的,将来把需要 ...

  2. MySql定时器,亲测可用

    1. 查看数据库的event功能是否开启,在MySql中event默认是关闭的,需要查看并且要确保event处于开启状态 sql:show VARIABLES LIKE '%sche%'; 如果eve ...

  3. hdfs运行机制

    hdfs:分布式文件系统 hdfs有着文件系统共同的特征: 1.有目录结构,顶层目录是:  / 2.系统中存放的就是文件 3.系统可以提供对文件的:创建.删除.修改.查看.移动等功能 hdfs跟普通的 ...

  4. c3p0,dbcp与druid 三大连接池的区别[转]

    说到druid,这个是在开源中国开源项目中看到的,说是比较好的数据连接池.于是乎就看看.扯淡就到这. 下面就讲讲用的比较多的数据库连接池.(其实我最先接触的是dbcp这个) 1)DBCP DBCP是一 ...

  5. java实现查找PDF关键字所在页码及其坐标

    1.因为最近有这方面的需求,用过之后记录一下. 2.此功能跟PDF中Ctrl+F性质一样,如果PDF中为图片形式的不支持定位到关键字. import com.itextpdf.awt.geom.Rec ...

  6. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

  7. CSS3 04. 伸缩布局、设置主轴,侧轴方向、主/侧轴对齐方式、 伸缩比例、元素换行、换行控制、覆盖父元素的align-items;控制子元素顺序、web字体、突变字体

    CSS3 在布局方面做了非常大的改进,对块级元素的布局排列变得十分灵活,适应性非常强,其强大的伸缩性,在响应式开发中可以发挥极大的作用.(兼容性不好) 必要元素: 指定一个盒子为伸缩盒子 displa ...

  8. CSS3 03. 3D变换、坐标系、透视perspective、transformZ、transform-style添加3D效果、backface-visibility元素背面可见、动画animation、@keyfarmes、多列布局

    1.左手坐标系 伸出左手,让拇指和食指成“L”形,大拇指向右,食指向上,中指指向前方.这样我们就建立了一个左手坐标系,拇指.食指和中指分别代表X.Y.Z轴的正方向.如下图 CSS中的3D坐标系 CSS ...

  9. 带你深入了解NPM——NPM初学者指南

    前段时间,我们邀请了我们“城内”(葡萄城)资深开发工程师刘涛为大家分享了一次干货满满的关于Electron线上公开课,在课程过程中有不少同学对于NPM的概念和用法有一些疑问,所以这次我们希望通过这篇文 ...

  10. Vert.x 学习之MongoDB Client

    Vert.x MongoDB Client 原文档:Vert.x MongoDB Client 组件介绍 您的 Vert.x 应用可以使用 Vert.x MongoDB Client(以下简称客户端) ...