欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-13 11:31:13
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......

前提

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

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

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

欢迎前来交流探讨: 企鹅群568015492 

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

nuget

install-package hzh_controls

目录

用处及效果

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

你可以向目标控件绘图,画任何你想画的东西

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

准备工作

没什么可准备的

开始

添加一个类graphicaloverlay ,继承component

代码比较少,一次全上了,主要就是用控件的paint事件搞事情,逻辑比较简单

  1 using system;
  2 using system.componentmodel;
  3 using system.drawing;
  4 using system.windows.forms;
  5 
  6 namespace hzh_controls.controls
  7 {
  8     [defaultevent("paint")]
  9     public partial class graphicaloverlay : component
 10     {
 11         public event eventhandler<painteventargs> paint;
 12         
 13         public graphicaloverlay()
 14         {
 15             initializecomponent();
 16         }
 17 
 18         public graphicaloverlay(icontainer container)
 19         {
 20             container.add(this);
 21 
 22             initializecomponent();
 23         }
 24         private control owner;      
 25         public control owner
 26         {
 27             get { return owner; }
 28             set
 29             {
 30                 // the owner form cannot be set to null.
 31                 if (value == null)
 32                     throw new argumentnullexception();
 33 
 34                 // the owner form can only be set once.
 35                 if (owner != null)
 36                     throw new invalidoperationexception();
 37 
 38                 // save the form for future reference.
 39                 owner = value;
 40 
 41                 // handle the form's resize event.
 42                 owner.resize += new eventhandler(form_resize);
 43 
 44                 // handle the paint event for each of the controls in the form's hierarchy.
 45                 connectpainteventhandlers(owner);
 46             }
 47         }
 48 
 49         private void form_resize(object sender, eventargs e)
 50         {
 51             owner.invalidate(true);
 52         }
 53 
 54         private void connectpainteventhandlers(control control)
 55         {
 56             // connect the paint event handler for this control.
 57             // remove the existing handler first (if one exists) and replace it.
 58             control.paint -= new painteventhandler(control_paint);
 59             control.paint += new painteventhandler(control_paint);
 60 
 61             control.controladded -= new controleventhandler(control_controladded);
 62             control.controladded += new controleventhandler(control_controladded);
 63 
 64             // recurse the hierarchy.
 65             foreach (control child in control.controls)
 66                 connectpainteventhandlers(child);
 67         }
 68 
 69         private void control_controladded(object sender, controleventargs e)
 70         {
 71             // connect the paint event handler for the new control.
 72             connectpainteventhandlers(e.control);
 73         }
 74 
 75         private void control_paint(object sender, painteventargs e)
 76         {
 77             // as each control on the form is repainted, this handler is called.
 78 
 79             control control = sender as control;
 80             point location;
 81 
 82             // determine the location of the control's client area relative to the form's client area.
 83             if (control == owner)
 84                 // the form's client area is already form-relative.
 85                 location = control.location;
 86             else
 87             {
 88                 // the control may be in a hierarchy, so convert to screen coordinates and then back to form coordinates.
 89                 location = owner.pointtoclient(control.parent.pointtoscreen(control.location));
 90 
 91                 // if the control has a border shift the location of the control's client area.
 92                 location += new size((control.width - control.clientsize.width) / 2, (control.height - control.clientsize.height) / 2);
 93             }
 94 
 95             // translate the location so that we can use form-relative coordinates to draw on the control.
 96             if (control != owner)
 97                 e.graphics.translatetransform(-location.x, -location.y);
 98 
 99             // fire a paint event.
100             onpaint(sender, e);
101         }
102 
103         private void onpaint(object sender, painteventargs e)
104         {
105             // fire a paint event.
106             // the paint event will be handled in form1.graphicaloverlay1_paint().
107 
108             if (paint != null)
109                 paint(sender, e);
110         }
111     }
112 }
113 
114 namespace system.windows.forms
115 {
116     using system.drawing;
117 
118     public static class extensions
119     {
120         public static rectangle coordinates(this control control)
121         {
122             // extend system.windows.forms.control to have a coordinates property.
123             // the coordinates property contains the control's form-relative location.
124             rectangle coordinates;
125             form form = (form)control.toplevelcontrol;
126 
127             if (control == form)
128                 coordinates = form.clientrectangle;
129             else
130                 coordinates = form.rectangletoclient(control.parent.rectangletoscreen(control.bounds));
131 
132             return coordinates;
133         }
134     }
135 }

 

最后的话

如果你喜欢的话,请到  点个星星吧