(七十五)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 ...
随机推荐
- Rest构建分布式 SpringCloud微服务架构项目
一.开发环境:jdk 1.8.Maven 3.x.IDEA 2019.1.4.SpringBoot 2.0.7.spring Cloud 最新的稳定版 Finchley SR2 搭配 ...
- WPF注册热键后处理热键消息(非winform方式)
由于最近在做wpf版的截图软件,在处理全局热键的时候,发现国内博客使用的都是winform窗体的键盘处理方式,此方式需要使用winform的动态库,如此不协调的代码让我开始在github中寻找相关代码 ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- Mybatis基于注解实现多表查询
对应的四种数据库表关系中存在四种关系:一对多,多对应,一对一,多对多.在前文中已经实现了xml配置方式实现表关系的查询,本文记录一下Mybatis怎么通过注解实现多表的查询,算是一个知识的补充. 同样 ...
- Delphi - cxGrid字段类型设定为ComboBox 并实现动态加载Item
cxGrid设定字段类型为ComboBox 在cxGrid中选中需要设定的字段: 单击F11调出属性控制面板,在Properties下拉选项中选中ComboBox,完成字段类型的设定. cxGrid ...
- Storm 系列(五)—— Storm 编程模型详解
一.简介 下图为 Strom 的运行流程图,在开发 Storm 流处理程序时,我们需要采用内置或自定义实现 spout(数据源) 和 bolt(处理单元),并通过 TopologyBuilder 将它 ...
- JavaScript 数据结构与算法之美 - 桶排序、计数排序、基数排序
1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...
- DEVOPS基础
转自:http://www.scrumcn.com/agile/scrum-knowledge-library/agile-development.html#tab-id-7 DevOps是一组过程. ...
- ccpc网赛 hdu6703 array(权值线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6703 大意:给一个n个元素的数组,其中所有元素都是不重复的[1,n]. 两种操作: 将pos位置元素+1e7 查 ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...