c#改寫的(vb.net)類比時鐘

來源:互聯網
上載者:User
 

ClockControl.cs

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ClockTime
{
 /// <summary>
 /// ClockControl 的摘要說明。
 /// </summary>
 public class ClockControl:System.Windows.Forms.UserControl

 {
  private DateTime dt;

  public ClockControl()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
   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+30.0F*Time.Minute/60);
   grfx.DrawPolygon(pn,new Point[]{new Point(0,150),new Point(100,0),new Point(0,-600),new Point(-100,0)});
   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,0,0,0,-800);
   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>
 /// 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()
  {
   //
   // Windows 表單設計器支援所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 表單設計器產生的程式碼
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </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>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   
   Application.Run(new Form1());
  }

  private void tmr_Tick(object sender, EventArgs e)
  {
  
   clkctrl.Time=DateTime.Now;
  }
 }
}

編譯過程:

csc /t:library clockcontrol.cs

csc /t:winexe /r:clockcontrol.dll form1.cs



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.