(七十五)c#Winform自定义控件-控件水印组件
2024-10-10 20:32:31
官网
前提
入行已经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/c++程序员必须要掌握开源项目
- tooltip
- mysql时间处理
- 删除一个目录和其各级子目录下的.svn文件
- COM简单应用示例
- [笔记]Practical Lessons from Predicting Clicks on Ads at Facebook
- PHP处理密码的几种方式【转载】
- junit4X系列--Builder、Request与JUnitCore
- 【ROM修改教程】添加高级电源重启菜单(安卓4.0.4官方ROM)
- tensorflow 使用 1 常量,变量
- C# this扩展方法
- 关于Idea里设置Terminal为Git/bin/bash.exe中文乱码的问题的终极解决方案
- Android:Unable to find explicit activity class
- 【hive】 hive 加载数据
- 二进制安装mysql
- HierSort(希尔)————Java
- Javascript如何实现GPU加速?
- Individual Project Records
- 基于matplotlib的数据可视化 - 笔记
- RESTful API的十个最佳实践
热门文章
- 一个最简单的通过自定义注解形式实现AOP的例子
- shift键复选dataGrid的记录时多余的文本总被选择了。
- [Python] 数据结构--实现顺序表、链表、栈和队列
- Flink 源码解析 —— TaskManager 处理 SubmitJob 的过程
- in和exists过程对比
- J farm
- 【Offer】[64] 【求1+2+...+n】
- dropwizard-core模块和应用启动分析
- java中public,private,protected和default的区别
- NLP(十七)利用tensorflow-serving部署kashgari模型