1 custom controls differ from user controls
In WinForm,
User control: Inherited from UserControl, primarily used to develop Container controls, Container controls can add other controls controls
Custom control: Inherits from control, primarily for developing the most basic classes of Windows controls, such as the Text,button control
2 several ways to develop your own controls [1]
Composite control (Composite controls): Combines existing controls to form a new control to meet the needs of the user.
Extended control (Extended controls): It is to derive a new control based on existing controls, add new functionality, or modify existing functionality to meet user needs.
Custom controls: It is the most flexible and powerful way to derive directly from the System.Windows.Forms.Control class, which means that it is entirely up to you to design and implement a completely new control, but the requirements for developers are the highest. To implement a custom control, you must write code for the OnPaint event of the control class to implement the custom control's drawing work in the OnPaint event. You can also override the WndProc method of the control class to handle the underlying Windows messages. Therefore, to implement a custom control, the requirements for developers are high, requiring developers to understand GDI + and Windows API knowledge.
3 Example: Clock User control[1]
Source
Steps:
1. Create a new Windows Control Library project (derived from UserControl)
2. Add a Timer control and set properties (Enable=true, interval=1000) and events (Ticker=time1_tick)
1 void Timer1_Tick (object sender, EventArgs e) {this. Time = DateTime.Now; 5}
3. Rewrite the OnPaint event to draw the user interface
Figure 1 Overriding the OnPaint event to draw the user interface
1#region Draw Clock2Privatevoid Userclock_paint (Objectsender, PaintEventArgs e)3{4 Graphics DC =E.graphics;5 Pen pn =NewPen (ForeColor);6 SolidBrush br =NewSolidBrush (ForeColor);7Initcoordinates (DC);8Drawdots (DC, BR);9Drawhourhand (DC, PN);10Drawsecondhand (DC, PN);11Drawminutehand (DC, PN);12}1314PublicvoidInitcoordinates (Graphics DC)15{16if (This. Width = =0 | |This. Height = =0)Return;DC. TranslateTransform (This. Width/2,This. Height/2);DC. ScaleTransform (This. HEIGHT/250F,This. Width/250F);19}20PublicvoidDrawdots (Graphics DC, Brush Brush)21st{22IntIsize;23for (int i =0; I <=59; i++)24{25if (i%5 = =0)26{Isize =15;28}29Else30{Isize =5;32}DC. FillEllipse (Brush,-isize/2,-100-isize/2, Isize, isize);DC. RotateTransform (6);35}36}37PublicVirtualvoidDrawhourhand (Graphics Grfx, Pen PN)38{GraphicsState GS =Grfx. Save ();Grfx. RotateTransform (360.0F * Time.hour/12 +30.0F * Time.minute/60);Grfx. DrawLine (PN,0,0,0,-50);42Grfx. Restore (GS);43}44PublicVirtualvoidDrawminutehand (Graphics Grfx, Pen PN)45{GraphicsState GS =Grfx. Save ();Grfx. RotateTransform (360.0F * Time.minute/60 +6.0F * Time.second/60);Grfx. DrawLine (PN,0,0,0,-70);49Grfx. Restore (GS);50}51PublicVirtualvoid Drawsecondhand (Graphics Grfx, Pen pn) 52 {53 graphicsstate gs = Grfx. Save (); 54 Grfx. RotateTransform (360.0f * Time.second/60); 55 Grfx. DrawLine (PN, 0, 0, 0,-< Span style= "COLOR: #800080" >100); 56 Grfx. Restore (GS); 57 }58 #endregion View Code
4. Build the user control
5. Testing the user control
Create the WinForm application, add the tab "User control" to toolbox, and drag the DLL file into the custom control that you generated in step 4th. Then drag the user control "Usercontrolclock" in Toolbox to the interface "Form1" as shown in.
C # Custom Controls vs User controls