(七十五)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
用处及效果
此效果只是牛刀小试,需要注意的是,像textbox这样的控件并不起作用,请注意。
你可以向目标控件绘图,画任何你想画的东西,此效果只是向控件覆盖一个半透明随机颜色

准备工作
没什么可准备的
开始
添加一个类GraphicalOverlay ,继承Component
代码比较少,一次全上了,主要就是用控件的paint事件搞事情,逻辑比较简单
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
[DefaultEvent("Paint")]
public partial class GraphicalOverlay : Component
{
public event EventHandler<PaintEventArgs> Paint; public GraphicalOverlay()
{
InitializeComponent();
} public GraphicalOverlay(IContainer container)
{
container.Add(this); InitializeComponent();
}
private Control owner;
public Control Owner
{
get { return owner; }
set
{
// The owner form cannot be set to null.
if (value == null)
throw new ArgumentNullException(); // The owner form can only be set once.
if (owner != null)
throw new InvalidOperationException(); // Save the form for future reference.
owner = value; // Handle the form's Resize event.
owner.Resize += new EventHandler(Form_Resize); // Handle the Paint event for each of the controls in the form's hierarchy.
ConnectPaintEventHandlers(owner);
}
} private void Form_Resize(object sender, EventArgs e)
{
owner.Invalidate(true);
} private void ConnectPaintEventHandlers(Control control)
{
// Connect the paint event handler for this control.
// Remove the existing handler first (if one exists) and replace it.
control.Paint -= new PaintEventHandler(Control_Paint);
control.Paint += new PaintEventHandler(Control_Paint); control.ControlAdded -= new ControlEventHandler(Control_ControlAdded);
control.ControlAdded += new ControlEventHandler(Control_ControlAdded); // Recurse the hierarchy.
foreach (Control child in control.Controls)
ConnectPaintEventHandlers(child);
} private void Control_ControlAdded(object sender, ControlEventArgs e)
{
// Connect the paint event handler for the new control.
ConnectPaintEventHandlers(e.Control);
} private void Control_Paint(object sender, PaintEventArgs e)
{
// As each control on the form is repainted, this handler is called. Control control = sender as Control;
Point location; // Determine the location of the control's client area relative to the form's client area.
if (control == owner)
// The form's client area is already form-relative.
location = control.Location;
else
{
// The control may be in a hierarchy, so convert to screen coordinates and then back to form coordinates.
location = owner.PointToClient(control.Parent.PointToScreen(control.Location)); // If the control has a border shift the location of the control's client area.
location += new Size((control.Width - control.ClientSize.Width) / , (control.Height - control.ClientSize.Height) / );
} // Translate the location so that we can use form-relative coordinates to draw on the control.
if (control != owner)
e.Graphics.TranslateTransform(-location.X, -location.Y); // Fire a paint event.
OnPaint(sender, e);
} private void OnPaint(object sender, PaintEventArgs e)
{
// Fire a paint event.
// The paint event will be handled in Form1.graphicalOverlay1_Paint(). if (Paint != null)
Paint(sender, e);
}
}
} namespace System.Windows.Forms
{
using System.Drawing; public static class Extensions
{
public static Rectangle Coordinates(this Control control)
{
// Extend System.Windows.Forms.Control to have a Coordinates property.
// The Coordinates property contains the control's form-relative location.
Rectangle coordinates;
Form form = (Form)control.TopLevelControl; if (control == form)
coordinates = form.ClientRectangle;
else
coordinates = form.RectangleToClient(control.Parent.RectangleToScreen(control.Bounds)); return coordinates;
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(七十五)c#Winform自定义控件-控件水印组件的更多相关文章
- (七十)c#Winform自定义控件-饼状图
		
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
 - 十五、RF操作时间控件
		
由于日期控件经常用的是readonly属性,这个属性意思是此控件为可读,明白点就是只让你看,不让你动. 解决方法就是:用js去掉这个属性,就可写了,就能输入了 导入库:DateTime #方式一 op ...
 - winform基础控件总结
		
转自:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 基础 - 常用控件 C# WinForm开发系列 - CheckBox/B ...
 - 在DevExpress程序中使用Winform分页控件直接录入数据并保存
		
一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...
 - winform窗体控件(全)
		
回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...
 - winform用户控件、动态创建添加控件、timer控件、控件联动
		
用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...
 - C#实现WinForm DataGridView控件支持叠加数据绑定
		
我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...
 - winform基本控件----按钮
		
这次来引用一个我们上课时候老师给的一个实验内容,来说一下winform程序设计中的按钮控件的使用.下面是我们老师给的实验内容. 实验目的: 掌握Winform的开发环境. 掌握窗体的创建和基本方法. ...
 - WinForm给控件加入hint文字
		
本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...
 
随机推荐
- .Net Core 三大Redis客户端对比和使用心得
			
前言 稍微复杂一点的互联网项目,技术选型都可能会涉及Redis,.NetCore的生态越发完善,支持.NetCore的Redis客户端越来越多, 下面三款常见的Redis客户端,相信大家平时或多或少用 ...
 - Codeforces 220C
			
题意略. 思路: 我们可以把 bi[ i ] 在 ai[ ] 中的位置记录下来,然后算出 i - mp[ bi[i] ] ,再将它压入一个multiset.每次我们就二分地来寻找离0最近的数字来作为答 ...
 - 懒人必备:.NetCore快速搭建ELK分布式日志中心
			
该篇内容由个人博客点击跳转同步更新!转载请注明出处! 前言 ELK是什么 它是一个分布式日志解决方案,是Logstash.Elastaicsearch.Kibana的缩写,可用于从不同的服务中收集日志 ...
 - CF_EDU51  E. Vasya and Big Integers
			
传送门:https://codeforces.com/contest/1051/problem/E 题意: 把一个数分成许多小段,每一段的值在L和R间.问有多少种分法. 思路 : 首先,需要快速处理出 ...
 - Codeforces Round #483 (Div. 2)  B. Minesweeper
			
题目地址:http://codeforces.com/contest/984/problem/B 题目大意:扫雷游戏,给你一个n*m的地图,如果有炸弹,旁边的八个位置都会+1,问这幅图是不是正确的. ...
 - lightoj 1140 - How Many Zeroes?(数位dp)
			
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
 - 牛客网暑期ACM多校训练营(第三场) C Shuffle Cards 平衡树 rope的运用
			
链接:https://www.nowcoder.com/acm/contest/141/C来源:牛客网 Eddy likes to play cards game since there are al ...
 - javascript删除数组元素的7个方法
			
在JavaScript中,除了Object之外,Array类型(数组)恐怕就是最常用的类型了.与其他语言的数组相比,JavaScript中的Array非常灵活.这种灵活性有利有弊,好处是其富有创造性, ...
 - springmvc——@InitBinder注解
			
转自http://www.cnblogs.com/douJiangYouTiao888/p/6765220.html 有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到C ...
 - (六十五)c#Winform自定义控件-图标字体
			
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...