Generally, in the development of winform projects, the controls provided by Visual Studio can basically meet our needs. However, in some cases, the control provided by the system does not meet the requirements. If you fully use the control to provide good results, you can also achieve the effect requirement, however, it may cause bloated and inconvenient control. Therefore, it is necessary to develop flexible custom controls in projects. You can customize the control you want based on your business needs. Generally, custom controls can be divided into three types.
1. custom control. This is a new control that needs to be completely designed and developed by myself. It generally inherits from the control and overwrites the onpaint method. You also need to write, add, and process events and messages by yourself. Such controls correspond to your business to achieve good results and provide the most flexible functions. At the same time, developers have the highest requirements. Generally, you need to know some knowledge about drawing GDI + and APIs. For example, we need a label-like control, but we do not need so many attributes and methods. Then, you can develop a custom control similar to label.
The following code: directly inherits from control. Other codes are automatically generated.
[CSHARP]View plaincopyprint?
- [Toolboxitem (true)]
- Public partial class customclassifylabelitem: Control
- {
- Private color mcolorout = color. fromargb (255,137, 37 );
- Private stringformat format;
- Private color textcolor;
- Private font strformat = new font ("", 9f, fontstyle. Regular, graphicsunit. Point, (byte) (134 )));
- Public stringformat format
- {
- Get
- {
- If (format = NULL)
- {
- Format = new stringformat ();
- Format. Alignment = stringalignment. Center;
- Format. linealignment = stringalignment. Center;
- Format. formatflags = stringformatflags. nowrap;
- Format. trimming = stringtrimming. ellipsischaracter;
- }
- Return format;
- }
- }
- Public color textcolor
- {
- Get
- {
- {
- Textcolor = color. fromargb (22, 95,162 );
- }
- Return textcolor;
- }
- }
- Public customclassifylabelitem ()
- {
- Initializecomponent ();
- This. mouseenter + = new eventhandler (ucselectclassifyitem_mouseenter );
- This. mouseleave + = new eventhandler (ucselectclassifyitem_mouseleave );
- }
- Void ucselectclassifyitem_mouseleave (Object sender, eventargs E)
- {
- Strformat = new font ("", 9f, fontstyle. Regular, graphicsunit. Point, (byte) (134 )));
- Invalidate ();
- }
- Void ucselectclassifyitem_mouseenter (Object sender, eventargs E)
- {
- Strformat = new font ("", 12f, fontstyle. Regular, graphicsunit. Point, (byte) (134 )));
- Invalidate ();
- }
- Protected override void onpaint (painteventargs PE)
- {
- Base. onpaint (PE );
- Graphics graphics = PE. graphics;
- Using (solidbrush B = new solidbrush (textcolor ))
- {
- Graphics. drawstring (this. Text, strformat, B, new rectangle (0, 0, this. Width, this. Height), format );
- }
- Graphics. Dispose ();
- }
- }
Redefines events and properties and overwrites the onpaint method. After writing it, you can use it like a label. Of course, this is just a simple example. Its performance and convenience are certainly not as good as the label.
The effect is as follows.
Move the mouse to the control,
Move the mouse away from the control
2. extended controls
Of course, there is no need to write all the controls by yourself. After all, it is unrealistic to write the controls completely. The controls provided by the system can meet the needs and are easy to use. Then, the inherited features are used, it can expand its functions and achieve flexible use. For example, in the following code, a label is simply extended. When the mouse enters, the label is underlined and displayed normally when it leaves.
[CSHARP]View plaincopyprint?
- Public class extendlabel: Label
- {
- Protected override void onmouseenter (system. eventargs E)
- {
- Base. onmouseenter (E );
- This. font = new font ("", 10f, fontstyle. Underline );
- }
- Protected override void onmouseleave (system. eventargs E)
- {
- Base. onmouseleave (E );
- This. font = new font ("", 10f, fontstyle. Regular );
- }
- }
Effect
Mouse entry,
Move the mouse away,
3. combined controls
This method is relatively simple, that is, to put all your written or built-in systems on usercontrol, and then add some events and attributes to make them a whole.
For example, a textbox and a button are directly put on a usercontrol, and a control is formed.
After these controls are generated, you can see them in the toolbar and drag them to the container for use.
In this way, with custom controls, it is no longer a problem for those difficult graphics. Of course, to develop good controls, you still need to study this knowledge more, it is only possible to accumulate more experience.
Practice code above: http://download.csdn.net/detail/yysyangyangyangshan/4163509