C#编写的clock

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyClockApp
{
    public partial class Clock : Form
    {
        public Clock()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 得到当前系统时间,并将其拼接成一个字符串
        /// </summary>
        /// <returns>数字时钟要显示的字符串</returns>
        public string GetTime()
        {
            String TimeInString = "";
            int hour = DateTime.Now.Hour;
            int min = DateTime.Now.Minute;
            int sec = DateTime.Now.Second;
            //将时,分,秒连成一个字符串
            TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString();
            TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
            TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString());
           
            return TimeInString;
        }
        /// <summary>
        /// 在窗体上画表盘时钟的图形
        /// </summary>
        /// <param name="h"></param>
        /// <param name="m"></param>
        /// <param name="s"></param>
        private void MyDrawClock(int h, int m, int s)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rect = this.ClientRectangle;

            g.Clear(Color.White);

            Pen myPen = new Pen(Color.Black, 1);
            g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 75, this.ClientRectangle.Height / 2-75, 150, 150);//画表盘

            Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点
            //计算出秒针,时针,分针的另外一个商点
            Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50)));
            Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 40)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 40)));
            Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360));
           
            //以不同颜色和宽度绘制表针
            myPen = new Pen(Color.Red, 1);
            g.DrawLine(myPen, centerPoint, secPoint);
            myPen = new Pen(Color.Green, 2);
            g.DrawLine(myPen, centerPoint, minPoint);
            myPen = new Pen(Color.Orange, 4);
            g.DrawLine(myPen, centerPoint, hourPoint);
        }
        /// <summary>
        /// 定时刷新显示时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            int h = DateTime.Now.Hour;
            int s = DateTime.Now.Second;
            int m = DateTime.Now.Minute;

            MyDrawClock(h, m, s);
            toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s);
            lblTime.Text = GetTime();
        }
        /// <summary>
        /// 若无此方法,时钟也能显示,但要等窗体显示几秒以后表盘才会显示。有了此方法窗体和表盘同时显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int h = DateTime.Now.Hour;
            int s = DateTime.Now.Second;
            int m = DateTime.Now.Minute;

            MyDrawClock(h, m, s);
            toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s);
            lblTime.Text = GetTime();
        }

    }
}

 

时间: 2024-07-30 10:16:01

C#编写的clock的相关文章

30个HTML代码编写技巧

Javascript和CSS都有许多优秀的框架,但如果你是初学者,不要急于使用它们. 本文总结了30条HTML代码编写指南,只要在编写HTML代码的过程中牢记它们,灵活运用,你一定会写出一手漂亮的代码,早日迈入专业开发者的行列. 1. 一定要闭合HTML标签 在以往的页面源代码里,经常看到这样的语句: <li>Some text here. <li>Some new text here. <li>You get the idea. 也许过去我们可以容忍这样的非闭合HTM

用Visual Basic.NET编写扑克牌游戏

visual 扑克游戏林林总总,同一种游戏各地玩法亦不尽相同.编程爱好者多喜欢编写一些本地玩法的扑克游戏.那么,编写自己的扑克游戏该从何处入手呢? 扑克游戏编程关键有两点:一是扑克牌面的绘制:二是扑克游戏规则的算法实现.初学扑克游戏编程的爱好者可从一些简单的游戏.借用一些现有资源开始.本文拟借用Windows自带的Cards.dll和简单的21点游戏为例,介绍扑克游戏编程的初步方法. 一. 扑克牌面绘制 Cards.dll支持Windows自带的游戏,如Solitaire(纸牌游戏).如果我们知

Web入门者必看的HTML代码编写的30条军规

本文总结了30条html代码编写指南,只要在编写HTML代码的过程中牢记它们,灵活运用,你一定会写出一手漂亮的代码,早日迈入专业开发者的行列. 1. 一定要闭合HTML标签 在以往的页面源代码里,经常看到这样的语句: <li>Some text here.   <li>Some new text here.   <li>You get the idea.  也许过去我们可以容忍这样的非闭合HTML标签,但在今天的标准来看,这是非常不可取的,是必须百分百避免的.一定要注意

c语言-编写的polar码译码程序为什么运行速度这么慢呢?怎么解决呢

问题描述 编写的polar码译码程序为什么运行速度这么慢呢?怎么解决呢 以下是C程序,只是译码程序,y是接收的加噪声的信息序列,p是位置矩阵,sigma是标准差,uyima是译码后序列,为什么运行速度这么慢呢 解决方案 #include #include #include #include #define pi 3.1415926 #define BITS 10 #define N (1< #define K 512//信息比特长度 #define L 1 #define log_L 0 int

《驯服烂代码:在编程操练中悟道》一第2章 按图索骥地编写代码

第2章 按图索骥地编写代码 现在,设计文档都齐备了,github也配好了,安装了JDK7和Maven,空项目已经用Maven建好了.还安装好了一个免费使用的IntelliJ IDEA 13.1 Community版,用来编程.现在就可以按照细化后的类图来编写第一个类TimeSubject了. 下面就是TimeSubject类的代码: public abstract class TimeSubject { protected Map<String, Clock> clocks = new Has

编写高性能Lua代码的方法_Lua

前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件,手机游戏<大掌门><神曲><迷失之地>等都是用Lua来写的逻辑. 所以大部分时候我们不需要去考虑性能问题.Knuth有句名言:"过早优化是万恶之源".其意思就是过早优化是不必要的,会浪费大量时间,而且容易导致代码混乱. 所以一个好的程序员在考虑优化性能前必须问自己两个问题:"我的程序真的需要优化吗?".如果答案为是,那么再

clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别【转】

转自:http://www.cnblogs.com/krythur/archive/2013/02/25/2932647.html 转自http://blog.sina.com.cn/s/blog_790f5ae10100rwd3.html   一)ANSI clock函数  1)概述:clock 函数的返回值类型是clock_t,它除以CLOCKS_PER_SEC来得出时间,一般用两次clock函数来计算进程自身运行的时间. ANSI clock有三个问题:1)如果超过一个小时,将要导致溢出.

[转]SCRUM软件开发过程----编写最好的软件

from: http://www.sawin.cn/doc/SE/SEThink/scrum.htm SCRUM软件开发过程 编写最好的软件 审校 林燕锋 [AKA] 译者 littledwarf 林燕锋 Allan bianh  sunshinezhou 胡庆培 [AKA] "The problem for engineers is that change translates into chaos, especially when a single error can potentially

在Visual J++中编写ASP COM组件

visual Java是一种以网络为中心的编程语言,许多只使用ASP脚本难以完成的任务可以用Java轻松地实现.同时,扩展ASP应用最好的方法莫过于加入COM组件.那么,可以利用Java为ASP开发组件吗?本文通过实例,说明了在Visual J++环境下开发COM组件的具体过程. 一.概述 自Java问世以来,各种开发工具.开发环境不断出现.这些环境和工具面向不同层次的用户,具有各自的优点.那么,使用Visual J++作为Java开发平台又有哪些优点呢?粗略地讲,这些优点包括: 熟悉的开发环境