mfc实现秒表小项目

项目截图:
这里写图片描述

先设计一个Timer类,用于计时,放在头文件Timer.h里面

class Timer
{
public:
    Timer();
    ~Timer();
    Timer   operator++()
    {
        s++;
        if (s >= 60)
        {
            s -= 60;
            m++;
            if (m >= 60)
            {
                m -= 60;
                h++;
                if (h >= 60)
                {
                    h -= 60;
                }
            }
        }
        return *this;
    }
    Timer   operator++(int)
    {
        Timer   old = *this;
        ++(*this);
        return old;
    }
    int     getH() const
    {
        return h;
    }
    int     getM() const
    {
        return m;
    }
    int     getS() const
    {
        return s;
    }
private:
    int h, m, s;
};

Timer::Timer()
{
    h = m = s = 0;
}

Timer::~Timer()
{
}

//---------------------------------------------------------------------
//创建一个线程回掉函数用于计时:
DWORD   WINAPI  clock1_proc(LPVOID lparam)
{
    Timer   t;
    CString s;
    while (true)
    {
        t++;
        Sleep(1000);
        s.Format(_T("%02d:%02d:%02d"), t.getH(), t.getM(), t.getS());
        ((CStatic*)lparam)->SetWindowTextW(s);
    }
}

void CClockDlg::OnBnClickedStart()//开始按钮的代码
{
    // TODO:  在此添加控件通知处理程序代码
    if (hClock1)//从暂停里恢复,hClock1表示第一个clcok的线程句柄,
    {
        ResumeThread(hClock1);
    }
    else{
        hClock1 = CreateThread(0, 0, clock1_proc, &this->hClock1Ctl, 0, NULL);//新建一个clock线程
    }

}

void CClockDlg::OnBnClickedPause()//暂停按钮的代码
{
    // TODO:  在此添加控件通知处理程序代码
    if (hClock1)
    {
        SuspendThread(hClock1);
    }
}


void CClockDlg::OnBnClickedReset()//重置按钮的代码
{
    // TODO:  在此添加控件通知处理程序代码
    if (hClock1)
    {
        TerminateThread(hClock1, 0);
        hClock1Ctl.SetWindowTextW(_T("00:00:00"));
        hClock1 = NULL;
    }
}

源码下载:https://pan.baidu.com/s/134Qn9pBb0Hq32h7A_cQqzg

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值