[转]Custom Controls in Visual C# .NET
A very simple introduction to writing your first .NET control
Download source files - 1 Kb
IntroductionThis is an extremely simple introduction to creating a control, adding methods and properties, and adding event handlers.
There are two types of components, Visible components (such as a edit box, list control) and non-visible (such as timers, communication, and data access).
This article will concentrate on the creation of a visible component, however the concepts do apply to non visible controls as well.
Visible ComponentsA visible component is any class derived from either System.WinForms.Control orSystem.WinForms.RichControl.
Here is the minimum code to produce the most basic visible control that functions correctly (but doesn‘t actually doanything).
//specify the namespace in which the control resides namespace Test.Control { //specify the name spaces of other classes that are commonly referenced using System.WinForms; //the control definition and implementation public class MyControl : System.WinForms.RichControl { } }
Now, if you ask me, this is a lot more straight forward than any OCX control I have ever written (ATL or not).
The control above, since it is derived from RichControl, contains quite a bit of stock functionality including: stock properties, stock events, layout management, the ability to act as a full control container (for child controls), and more. In other words, just start writing in your business logic, all of the grunt work is taken care of.
Since the control has no paint routine - yet - it will only erase its background to its parents background color, so it is not too useful at this point. Adding a simple paint routine is also quite simple.
using System.Drawing; protected override void OnPaint(PaintEventArgs pe) { SolidBrush b = new SolidBrush(this.BackColor); pe.Graphics.FillRectangle(b,this.ClientRectangle); }
The code above will paint the control using its BackColor property. So if you add this control to a form now and modify its BackColor propery via the property sheet the control‘s color will now change as well.
Notice that using System.Drawing; has been added. This allows classes such as SolidBrush to be used without specifying the entire namespace (System.Drawing.SolidBrush).
Adding a propertyAdding a read/write or read only property to a control is also quite straight forward. In this example I am going to add a set of gradient color properties to the control. Below I am only going to show one of the properties, since the other is a mirror copy of the first, except for the variable name.
private System.Drawing.Color gradColor1; public System.Drawing.Color Gradient1 { set { this.gradColor1 = value; } get { return this.gradColor1; } }
So, first I must add a data member to hold the properties value. Since the property is a color it will be of typeSystem.Drawing.Color. Since using System.Drawing; has been added the variable can also be declared as just Color.
Next the declaration for the Gradient1 property is made. The start of the declaration looks similar to a function declaration (access modifier, return type, and name). Although the property itself is not a function it can contain two special functions nested within it: set and get. To make a read/write property both set and get must be implemented. For a read-only property just implement get. Even though the set function does not have a parameter list, it does get passed a special parameter called value. The type of the Value parameter will always be the same type as the property itself.
Since properties are set and retrieved indirectly (which is quite different from reading and writing to data members), other code can be added to the set and get functions. For instance if a property is a calculated value, the calculation can be performed within the get itself. During a set it is possible to set flags or cause the control to refresh itself, etc.
To make a control easy to use during design time, many controls group their properties and have help strings that display when the property is selected in the property browser. This can also be easily accomplished through the use of attributes. Attributes are special objects that are added above the declaration of a class, function, enum or property, and are delimited by ‘[‘ and ‘]’.
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69289.html
- 上一篇:Windows安装mapnik
- 下一篇:(WPF)附加属性