第一个有窗口的程序,写于大一的暑假
2010-02-05 20:55
423 查看
在最早学习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());
}
}
}
几经周折,我找到了窗体设计演练,跟着帮助走,制作了一个秒表控件。(注意是控件)
通过学习发现,控件和窗口是不同的,出于代码继承性和重用性考虑,一般先写具有特定功能的控件,然后再把控件添加到窗口中去。可以由几个小控件来组成一个大的控件。
第一个程序(还只是控件)做的是秒表。(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());
}
}
}
相关文章推荐
- 我的第一个win32窗口程序------------记录
- 第一个窗口程序
- MASM写的第一个Win32 窗口程序
- WIN32汇编-第一个窗口程序
- Qt-第一个白窗口程序
- win32汇编第一个窗口程序
- 视觉学习三 ——第一个Qt窗口程序
- (Delphi)第一个Windows 32 API的窗口程序
- 【DirectX11-Tutorial】编写第一个窗口程序creating a window
- windows下32位汇编语言学习笔记 第四章 第一个窗口程序 (windows的消息机制)
- 第四章 第一个窗口程序
- 你的第一个Windows程序——窗口消息
- 用VC写的第一个窗口程序 Hello World!
- Go语言Windows程序设计(2)--第一个窗口程序
- (一)第一个窗口程序
- 第一个手写Win32窗口程序
- [C语言](一)第一个Windows 32 API的窗口程序
- 第一个Win32窗口程序,纪念自己的Windows编程之旅
- 窗口程序你的第一个Windows程序
- [DirectX开发学习日记]第一个DirectX窗口程序细节-(03)