果子狸养殖,2018北京卫视春晚节目单,茶叶网
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
install-package hzh_controls
此效果只是牛刀小试,需要注意的是,像textbox这样的控件并不起作用,请注意。
你可以向目标控件绘图,画任何你想画的东西
没什么可准备的
添加一个类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 }
如果你喜欢的话,请到 点个星星吧
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论