第一个有窗口的程序,写于大一的暑假

 

在最早学习C++的时候,老师只是让我们使用编写控制台程序,目的是熟悉C++的语法规则。控制台程序的没有图形界面,相对落后,也不实用。于是,我尝试使用Visual Studio 2008自带的帮助,编写一些易操作的程序。

 

几经周折,我找到了窗体设计演练,跟着帮助走,制作了一个秒表控件。(注意是控件)

 

通过学习发现,控件和窗口是不同的,出于代码继承性和重用性考虑,一般先写具有特定功能的控件,然后再把控件添加到窗口中去。可以由几个小控件来组成一个大的控件。

 

第一个程序(还只是控件)做的是秒表。(stopwatch)然后,想把发现了控件与程序的差异,于是就想把控件添加进一个form,觉得这样就成了一个程序。但是,很遗憾地失败了。于是,利用复制、粘贴功能,将代码复制到窗体程序。好歹也利用了代码的“继承和重用”。哈哈

 

主界面上有四个按钮,start,record,clear,end。按start 开始计时,按record,按一次记录一次时间。按clear清除记录的时间。按end停止计时。

 

一些细节处理:

首先,限制了如果不按start,那么record等键按了没有反应。

 

bug:

其次,由于使用的是扫描系统时间进行计时,所以,如果在计时过程中,调整了系统时间,会出现计时的错误。代码 如下:

 

首先是Form1.cs的内容:

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

namespace StopWatchWindows
{
    public partial class Form1 : Form
    {
        //-------开始时间------------
        private int starthour;
        private int startminute;
        private int startsecond;
        private int startmillisecond;
        //---------------------------

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Start_Click(object sender, EventArgs e)
        {
            Immediate.Enabled = true;
            Immediate.Interval = 100;//扫描的时间间隔为100毫秒
            this.timerecord.Text = "";
            //记时起点
            starthour = DateTime.Now.Hour;
            startminute = DateTime.Now.Minute;
            startsecond = DateTime.Now.Second;
            startmillisecond = DateTime.Now.Millisecond;
        }

        private void Record_Click(object sender, EventArgs e)
        {
            if (Immediate.Enabled == true)
            {
                this.timerecord.Text += "/n";//换行
                //计算时间
                int rechour;
                int recminute;
                int recsecond;
                int recmillisecond;
                int outhour, outminute, outsecond, outmillisecond;
                rechour = DateTime.Now.Hour;
                recminute = DateTime.Now.Minute;
                recsecond = DateTime.Now.Second;
                recmillisecond = DateTime.Now.Millisecond;
                outmillisecond = recmillisecond - startmillisecond;
                if (outmillisecond < 0)
                {
                    outmillisecond += 1000;
                    recsecond--;
                }
                outsecond = recsecond - startsecond;
                if (outsecond < 0)
                {
                    outsecond += 60;
                    recminute--;
                }
                outminute = recminute - startminute;
                if (outminute < 0)
                {
                    outminute += 60;
                    rechour--;
                }
                outhour = rechour - starthour;

                {//输出时间
                    if (outhour == 0)
                        this.timerecord.Text += "00";
                    else
                        this.timerecord.Text += outhour;
                    this.timerecord.Text += ":";
                    //输出小时
                    if (outminute < 10)
                        if (outminute == 0) this.timerecord.Text += "00";
                        else this.timerecord.Text += "0" + outminute;
                    else this.timerecord.Text += outminute;
                    this.timerecord.Text += ":";
                    //输出分钟
                    if (outsecond < 10)
                        if (outsecond == 0) this.timerecord.Text += "00";
                        else this.timerecord.Text += "0" + outsecond;
                    else this.timerecord.Text += outsecond;
                    this.timerecord.Text += ":";
                    //输出秒
                    if (outmillisecond < 100)
                        if (outmillisecond < 10) this.timerecord.Text += "00" + outmillisecond;
                        else this.timerecord.Text += "0" + outmillisecond;
                    else this.timerecord.Text += outmillisecond;
                    //输出毫秒
                }
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            this.timerecord.Text = "";
        }

        private void End_Click(object sender, EventArgs e)
        {
            if (Immediate.Enabled == true)
            {
                Immediate.Enabled = false;
                this.timepresent.Text = "End";
                this.timerecord.Text += "/n";
                //计算时间
                int rechour;
                int recminute;
                int recsecond;
                int recmillisecond;
                int outhour, outminute, outsecond, outmillisecond;
                rechour = DateTime.Now.Hour;
                recminute = DateTime.Now.Minute;
                recsecond = DateTime.Now.Second;
                recmillisecond = DateTime.Now.Millisecond;
                outmillisecond = recmillisecond - startmillisecond;
                if (outmillisecond < 0)
                {
                    outmillisecond += 1000;
                    recsecond--;
                }
                outsecond = recsecond - startsecond;
                if (outsecond < 0)
                {
                    outsecond += 60;
                    recminute--;
                }
                outminute = recminute - startminute;
                if (outminute < 0)
                {
                    outminute += 60;
                    rechour--;
                }
                outhour = rechour - starthour;

                {//输出时间
                    if (outhour == 0)
                        this.timerecord.Text += "00";
                    else
                        this.timerecord.Text += outhour;
                    this.timerecord.Text += ":";
                    //输出小时
                    if (outminute < 10)
                        if (outminute == 0) this.timerecord.Text += "00";
                        else this.timerecord.Text += "0" + outminute;
                    else this.timerecord.Text += outminute;
                    this.timerecord.Text += ":";
                    //输出分钟
                    if (outsecond < 10)
                        if (outsecond == 0) this.timerecord.Text += "00";
                        else this.timerecord.Text += "0" + outsecond;
                    else this.timerecord.Text += outsecond;
                    this.timerecord.Text += ":";
                    //输出秒
                    if (outmillisecond < 100)
                        if (outmillisecond < 10) this.timerecord.Text += "00" + outmillisecond;
                        else this.timerecord.Text += "0" + outmillisecond;
                    else this.timerecord.Text += outmillisecond;
                    //输出毫秒
                }
            }
        }

        private void Immediate_Tick_1(object sender, EventArgs e)
        {
            this.timepresent.Text = "";
            //计算时间
            int rechour;
            int recminute;
            int recsecond;
            int recmillisecond;
            int outhour, outminute, outsecond, outmillisecond;
            rechour = DateTime.Now.Hour;
            recminute = DateTime.Now.Minute;
            recsecond = DateTime.Now.Second;
            recmillisecond = DateTime.Now.Millisecond;
            outmillisecond = recmillisecond - startmillisecond;
            if (outmillisecond < 0)
            {
                outmillisecond += 1000;
                recsecond--;
            }
            outsecond = recsecond - startsecond;
            if (outsecond < 0)
            {
                outsecond += 60;
                recminute--;
            }
            outminute = recminute - startminute;
            if (outminute < 0)
            {
                outminute += 60;
                rechour--;
            }
            outhour = rechour - starthour;

            {//输出时间
                if (outhour == 0)
                    this.timepresent.Text += "00";
                else
                    this.timepresent.Text += outhour;
                this.timepresent.Text += ":";
                //输出小时
                if (outminute < 10)
                    if (outminute == 0) this.timepresent.Text += "00";
                    else this.timepresent.Text += "0" + outminute;
                else this.timepresent.Text += outminute;
                this.timepresent.Text += ":";
                //输出分钟
                if (outsecond < 10)
                    if (outsecond == 0) this.timepresent.Text += "00";
                    else this.timepresent.Text += "0" + outsecond;
                else this.timepresent.Text += outsecond;
                this.timepresent.Text += ":";
                //输出秒
                if (outmillisecond < 100)
                    if (outmillisecond < 10) this.timepresent.Text += "00" + outmillisecond;
                    else this.timepresent.Text += "0" + outmillisecond;
                else this.timepresent.Text += outmillisecond;
                //输出毫秒
            }
        }
    }
}

 

 

然后是

program.cs的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace StopWatchWindows
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值