转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
c#改写的(vb.net)模拟时钟         ★★★★

c#改写的(vb.net)模拟时钟

作者:闵涛 文章来源:闵涛的学习笔记 点击数:665 更新时间:2009/4/23 19:00:03

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


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台