I have been using C # for a while, but I have never designed a custom control. I just finished the design of a custom control. I think I have some skills and knowledge to make a summary.
Some things are not easy to find from msdn, such as setting the description of some attributes. Here we can make some exchanges for this.
I designed a smooth progress bar control, which makes it easy to find some ready-made controls on the Internet. However, I wrote one myself to learn the design of the control.
This control is inherited from the usercontrol class. The principle is very simple. It is to fill the color in a certain area with a brush.
The design control must have some attributes. The most important attribute in the progress bar design is the minimum, maximum, and current value. the attribute must be described in the vs designer.
Take the minimum value as an example. when selecting this attribute in the vs designer, you need to have its descriptive text and its columns. to achieve these two aspects, you can add such a line of text before the attribute:
[Description ("getting or setting the minimum range of the current progress bar"), category ("behavior")]
In this way, the attribute in the vs designer will look the same as other attributes. of course, you can also change the Chinese character of the topic to English, but I do not know what English is used here, but the appearance is represented by appearance.
In addition to descriptive text and columns, you can also have default values, which are available in many attributes. With defaultvalue (object value), you can apply the default values.
In the attribute designer of Vs, such attributes often exist. For example, a font editor is displayed after you click a font. For such an editor that exists in the vs designer, we can use it directly.
For example, edit a font attribute.
[Description ("font used to display text in the control"), category ("appearance")]
Public override Font font
{
Get {return m_labelfont ;}
Set
{
M_labelfont = value;
This. invalidate ();
}
}
Defines its return value as font, so that in the vs designer, the font editor is the same as the font of other controls. similar to this, there are also the color attribute and borderstyle attribute.
Since this control inherits from the usercontrol class, it will inevitably inherit some attributes that we do not want to appear in the designer. for example, if you do not want the tabstop attribute to be applied to the progress bar, you must make it invisible in the designer.
[Browsable (false)]
Public new bool tabstop
{
Get {return base. tabstop ;}
}
The browsable (bool value) statement can make an attribute visible or hidden.
Some inherited attributes need to be rewritten, whether it is to change it or hide it. override or new is required for rewriting. Some parent class attributes use virtual, but some do not. if the virtual attribute is not used, the new attribute must be used in the subclass.
There are also some inherited events that are unnecessary. To Hide events, I have also found some materials, which are rare on the Internet, it took only half a day to find a related statement in msdn.
For example, if you do not need the progress bar for button operations, you need to hide the event in the designer.
[Browsable (false)]
Public new event eventhandler keydown; // www.elivn.com
The preceding is a hidden statement, followed by an event. In this case, it is easier than a property member. However, after searching the network for a long time, nothing is found.
The operations on this drawing part are not detailed here. here are some basic things for creating controls. however, the drawing operations are mainly put together, for example, in the paint event. at the end of the event, draw the border.
When you assign values to the current value of the progress bar, you need to trigger a drawing operation inside the progress bar to trigger the paint event. here we need to execute a method invalidate (), which declares that the current region is invalid, and then triggers the paint event.
The area of the progress bar is relatively small, so it does not consume graphics resources. However, if the required graphic resources are large and the drawing operations are triggered frequently, it cannot be declared that all areas are invalid, as long as the region that has been updated is declared invalid. after obtaining the update region, you can use the invalidate (rectangle rect) method to update only some regions.
For graphical controls, many operations require graphics class plotting. I have not studied this huge class in depth, nor can I talk about it here. Otherwise, I will say it will be over.
The progress bar class also has some descriptions, such as an icon and a default attribute. In this class, I still use the progress bar icon in. net.
[Toolboxbitmap (typeof (progressbar), defaultproperty ("value")]
After adding a smooth progress bar to the vs designer, the progress bar icon displayed on the tool panel will be a familiar progress bar icon. of course, we can also use the address path or the icon in the resource to replace it.
The relative statement is toolboxbitmap (PATH)
I have just started to design controls, and I have not found many things. I hope my colleagues in the research will sponsor them. Thank you.