使用C++在VS2019中制作一个GUI来展示倒计时的完整代码

时间: 2023-06-26 17:08:09 浏览: 58
以下是使用C++和MFC库在VS2019中制作倒计时GUI的完整代码: ``` // CountdownTimerDlg.h #pragma once #include "afxwin.h" class CCountdownTimerDlg : public CDialogEx { public: CCountdownTimerDlg(CWnd* pParent = nullptr); // standard constructor virtual ~CCountdownTimerDlg(); // Dialog Data #ifdef AFX_DESIGN_TIME enum { IDD = IDD_COUNTDOWN_TIMER_DIALOG }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support DECLARE_MESSAGE_MAP() private: int m_hours; int m_minutes; int m_seconds; CStatic m_display; bool m_timer_running; UINT_PTR m_timer_id; void UpdateDisplay(); public: virtual BOOL OnInitDialog(); afx_msg void OnBnClickedStartButton(); afx_msg void OnBnClickedStopButton(); afx_msg void OnTimer(UINT_PTR nIDEvent); }; // CountdownTimerDlg.cpp #include "stdafx.h" #include "CountdownTimer.h" #include "CountdownTimerDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CCountdownTimerDlg dialog CCountdownTimerDlg::CCountdownTimerDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_COUNTDOWN_TIMER_DIALOG, pParent), m_hours(0), m_minutes(0), m_seconds(0), m_timer_running(false), m_timer_id(0) { } CCountdownTimerDlg::~CCountdownTimerDlg() { } void CCountdownTimerDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_DISPLAY, m_display); } BEGIN_MESSAGE_MAP(CCountdownTimerDlg, CDialogEx) ON_BN_CLICKED(IDC_START_BUTTON, &CCountdownTimerDlg::OnBnClickedStartButton) ON_BN_CLICKED(IDC_STOP_BUTTON, &CCountdownTimerDlg::OnBnClickedStopButton) ON_WM_TIMER() END_MESSAGE_MAP() // CCountdownTimerDlg message handlers BOOL CCountdownTimerDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // Set the timer interval to 1 second SetTimer(m_timer_id, 1000, NULL); return TRUE; } void CCountdownTimerDlg::OnBnClickedStartButton() { // Get the user input for hours, minutes, and seconds m_hours = GetDlgItemInt(IDC_HOURS_EDIT); m_minutes = GetDlgItemInt(IDC_MINUTES_EDIT); m_seconds = GetDlgItemInt(IDC_SECONDS_EDIT); // Start the timer m_timer_running = true; } void CCountdownTimerDlg::OnBnClickedStopButton() { // Stop the timer m_timer_running = false; } void CCountdownTimerDlg::OnTimer(UINT_PTR nIDEvent) { if (m_timer_running) { // Decrement the remaining time if (m_seconds > 0) { m_seconds--; } else if (m_minutes > 0) { m_minutes--; m_seconds = 59; } else if (m_hours > 0) { m_hours--; m_minutes = 59; m_seconds = 59; } else { // Time's up! m_timer_running = false; } UpdateDisplay(); } CDialogEx::OnTimer(nIDEvent); } void CCountdownTimerDlg::UpdateDisplay() { CString display_text; display_text.Format(_T("%02d:%02d:%02d"), m_hours, m_minutes, m_seconds); m_display.SetWindowText(display_text); } ``` 在这个代码中,我们创建了一个名为CCountdownTimerDlg的对话框类。在OnInitDialog函数中,我们设置了定时器间隔为1秒,并在OnTimer函数中处理了倒计时逻辑。用户通过在编辑框中输入小时、分钟、秒数来指定倒计时时间,并通过点击开始和停止按钮来启动和停止倒计时。最后,我们在UpdateDisplay函数中更新了GUI中的显示内容。

相关推荐

最新推荐

recommend-type

VS2019如何创建C++项目的实现示例

主要介绍了VS2019如何创建C++项目的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

如何使用visual studio2019创建简单的MFC窗口(使用C++)

主要介绍了如何使用visual studio2019创建简单的MFC窗口(使用C++),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

VS2019中CMake项目如何指定c++语言标准

主要介绍了VS2019中CMake项目如何指定c++语言标准,需要的朋友可以参考下
recommend-type

vscode使用官方C/C++插件无法进行代码格式化问题

官方的C/C++插件是支持使用.clang-format配置文件进行自定义风格代码格式化的,无需另外安装clang-format插件。 但是使用clang-format -style=llvm -dump-config > .clang-format导出的默认配置文件进行格式化的时候...
recommend-type

C++定时器Timer在项目中的使用方法

主要给大家介绍了关于C++定时器Timer在项目中的基本使用方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。