首页 技术 正文
技术 2022年11月14日
0 收藏 550 点赞 2,152 浏览 3555 个字

官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 (七十五)c#Winform自定义控件-控件水印组件

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

此效果只是牛刀小试,需要注意的是,像textbox这样的控件并不起作用,请注意。

你可以向目标控件绘图,画任何你想画的东西,此效果只是向控件覆盖一个半透明随机颜色

(七十五)c#Winform自定义控件-控件水印组件

准备工作

没什么可准备的

开始

添加一个类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 点个星星吧

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898