I am not a professional control developer. I only need to develop some controls in my daily work. Developed on your own Winform When using a widget, you can only stare Msdn Let's take a hard look. Fortunately, some gains have finally been made. Now I will summarize these experiences and write them into a series of chapters, hoping to help my friends. Let me start today.
Development Winform Controls are not very complex, . Net It provides us with a wide range of underlying support. If you have MFC or API Gui Development experience, then learn Winform Controls may take a short time That's enough.
Self-developed Winform Controls generally have three types: Composite controls ( Composite controls ), Extended controls ( Extended controls ), Custom controls ( Custom Controls ).
Composite Control: Combines various existing controls to form a new control and centralize the functions of the centralized control.
Extended Control: A new control is derived from the existing control to add new functions or modify the control capabilities of the original control.
Custom Controls: Directly from System. Windows. Forms. Control Class. Control Class provides all the basic functions required by the control, including keyboard and mouse event processing. Custom Controls are the most flexible and powerful method, but they have high requirements for developers. Control Class Onpaint Event writingCodeYou can also rewrite Control Class Wndproc Method to handle more underlying Windows Message, so you should understand GDI + And Windows API .
This seriesArticleThis section describes how to develop custom controls.
Basic Features of controls (visualized:
1. Visualization.
2. It can interact with users, such as through the keyboard and mouse.
3. A set of attributes and methods are exposed for developers.
4. A group of events are exposed for developers.
5. Control attributes can be persisted.
6. Can be released and reusable.
This Some features are summarized by myself, which are not necessarily accurate or missing, but basically summarize the main aspects of the control.
Next we will make a simple control to enhance our perceptual knowledge. Start Vs2005 Create Classlibrary Project, named Customcontrolsample , VS Will automatically create Solution Delete the automatically generated Class1.cs File Solution Explorer Right-click Customcontrolsample Project selection Add-> classes... Add a new class and name the file Firstcontrol . Below is the code:
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Windows. forms;
Using System. componentmodel;
Using System. drawing;
Namespace Customcontrolsample
{
Public Class Firstcontrol: Control
{
Public Firstcontrol ()
{
}
// Contentalignment is an enumeration defined in the system. Drawing
// Namespace that specifies the alignment of content on a drawing
// Surface.
Private Contentalignment alignmentvalue = Contentalignment. middleleft;
[
Category ( " Alignment " ),
Description ( " Specifies the alignment of text. " )
]
Public Contentalignment textalignment
{
Get
{
ReturnAlignmentvalue;
}
Set
{
Alignmentvalue = Value;
// The invalidate method invokes the onpaint method described
// In step 3.
Invalidate ();
}
}
Protected Override Void Onpaint (painteventargs E)
{
Base . Onpaint (E );
Stringformat Style = New Stringformat ();
Style. Alignment = Stringalignment. Near;
Switch (Alignmentvalue)
{
Case Contentalignment. middleleft:
Style. Alignment = Stringalignment. Near;
Break ;
Case Contentalignment. middleright:
Style. Alignment = Stringalignment. far;
Break ;
Case Contentalignment. middlecenter:
Style. Alignment = Stringalignment. Center;
Break ;
}
// Call the drawstring method of the system. drawing class to write
// Text. Text and clientrectangle are properties inherited from
// Control.
E. Graphics. drawstring (
Text,
Font,
New Solidbrush (forecolor ),
Clientrectangle, style );
}
}
}
Later, I will write it here today. The next article will introduce how to use the control we have written.