Clockcontrol. CS
Using system;
Using system. Windows. forms;
Using system. drawing;
Using system. Drawing. drawing2d;
Namespace clocktime
{
/// <Summary>
/// Summary of clockcontrol.
/// </Summary>
Public class clockcontrol: system. Windows. Forms. usercontrol
{
Private datetime DT;
Public clockcontrol ()
{
//
// Todo: add the constructor logic here
//
This. resizeredraw = true;
This. Enabled = false;
}
Public datetime time
{
Set
{
Graphics grfx = This. creategraphics ();
Pen Pn = new pen (this. backcolor );
Initializecoordinates (grfx );
If (Dt. Hour! = Value. hour)
{
Drawhourhand (grfx, PN );
}
If (Dt. Minute! = Value. Minute)
{
Drawhourhand (grfx, PN );
Drawminutehand (grfx, PN );
}
If (Dt. Second! = Value. Second)
{
Drawminutehand (grfx, PN );
Drawsecondhand (grfx, PN );
}
If (Dt. millisecond! = Value. millisecond)
{
Drawsecondhand (grfx, PN );
}
Dt = value;
Pn = new pen (forecolor );
Drawhourhand (grfx, PN );
Drawminutehand (grfx, PN );
Drawsecondhand (grfx, PN );
Grfx. Dispose ();
}
Get
{Return DT;
}
}
Protected override void onpaint (painteventargs E)
{
Graphics grfx = E. graphics;
Pen Pn = new pen (forecolor );
Solidbrush BR = new solidbrush (forecolor );
Initializecoordinates (grfx );
Drawdots (grfx, Br );
Drawhourhand (grfx, PN );
Drawsecondhand (grfx, PN );
Drawminutehand (grfx, PN );
}
Protected virtual void initializecoordinates (Graphics grfx)
{
If (this. width = 0 | this. Height = 0) return;
Grfx. translatetransform (this. width/2, this. Height/2 );
Single finches = math. Min (this. width/grfx. dpix, this. Height/grfx. dpiy );
Grfx. scaletransform (finches * grfx. dpix/2000, finches * grfx. dpiy/2000 );
}
Protected virtual void drawdots (Graphics grfx, brush BR)
{
Int I, isize;
For (I = 0; I <= 59; I ++)
{
If (I % 5 = 0)
{
Isize = 100;
}
Else
Isize = 30;
Grfx. fillellipse (BR, 0-isize/2,-900-isize/2, isize, isize );
Grfx. rotatetransform (6 );
}
}
Protected virtual void drawhourhand (Graphics grfx, pen PN)
{
Graphicsstate GS = grfx. Save ();
Grfx. rotatetransform (360.0f * time. hour/12 + 30366f * time. Minute/60 );
Grfx. drawpolygon (Pn, new point [] {New Point (0,150), new point (600), new point (0,-), new point )});
Grfx. Restore (GS );
}
Protected virtual void drawminutehand (Graphics grfx, pen PN)
{
Graphicsstate GS = grfx. Save ();
Grfx. rotatetransform (360.0f * time. Minute/60 + 6.0f * time. Second/60 );
Grfx. drawpolygon (Pn, new point [] {New Point (0,200), new point (50,0), new point (0,-800), new point (-50,0 )});
Grfx. Restore (GS );
}
Protected virtual void drawsecondhand (Graphics grfx, pen PN)
{
Graphicsstate GS = grfx. Save ();
Grfx. rotatetransform (360.0f * time. Second/60 + 6.0f * time. millisecond/1000 );
Grfx. drawline (Pn, 800, 0 );
Grfx. Restore (GS );
}
}
}
Form1.cs
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using clocktime;
Namespace clocktime
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. componentmodel. icontainer components;
Private system. Windows. Forms. Timer TMR;
Private clocktime. clockcontrol clkctrl;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. components = new system. componentmodel. Container ();
This. clkctrl = new clocktime. clockcontrol ();
This. TMR = new system. Windows. Forms. Timer (this. components );
This. suspendlayout ();
//
// Clkctrl
//
This. clkctrl. backcolor = system. Drawing. color. Black;
This. clkctrl. Dock = system. Windows. Forms. dockstyle. Fill;
This. clkctrl. Enabled = false;
This. clkctrl. forecolor = system. Drawing. color. White;
This. clkctrl. Location = new system. Drawing. Point (0, 0 );
This. clkctrl. Name = "clkctrl ";
This. clkctrl. size = new system. Drawing. Size (292,273 );
This. clkctrl. tabindex = 0;
This. clkctrl. Time = new system. datetime (2005, 6, 24, 10, 19, 26, 62 );
//
// TMR
//
This. TMr. Enabled = true;
This. TMr. interval = 1000;
This. TMr. Tick + = new system. eventhandler (this. tmr_tick );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. backcolor = system. Drawing. systemcolors. window;
This. clientsize = new system. Drawing. Size (292,273 );
This. Controls. Add (this. clkctrl );
This. forecolor = system. Drawing. systemcolors. windowtext;
This. Name = "form1 ";
This. Text = "clockcontrol ";
This. resumelayout (false );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
Private void tmr_tick (Object sender, eventargs E)
{
Clkctrl. Time = datetime. now;
}
}
}
Compilation process:
CSC/T: Library clockcontrol. CS
CSC/T: winexe/R: clockcontrol. dll form1.cs