时间控件可以使用函数ModifyStyle设置是显示日期还是时间。
可以使用函数GetTime获取时间变量,CTime有成员函数GetYear,GetMonth等获取指定格式的时间。
如
- CString strText = _T("");
- strText.Format(_T("%04d-%02d-%02d\n%02d:%02d:%02d"),
- time1.GetYear(), time1.GetMonth(), time1.GetDay(),
- time2.GetHour(), time2.GetMinute(), time2.GetSecond());
复制代码 这进而提供一个例程供大家参考学习。
例程添加两个时间变量,并进行了关联
CDateTimeCtrl m_ctrlDateTime2;
CDateTimeCtrl m_ctrlDateTime1;
程序初始化时初始化了变量
- //选择短日期显示格式
- m_ctrlDateTime1.ModifyStyle(0, DTS_SHORTDATEFORMAT, 0);
- //选择时间显示格式
- m_ctrlDateTime2.ModifyStyle(0, DTS_TIMEFORMAT, 0);
- //获得当前时间
- CTime time = CTime::GetCurrentTime();
- //设置时间
- m_ctrlDateTime1.SetTime(&time);
- m_ctrlDateTime2.SetTime(&time);
复制代码
点击按钮可以获得时间控件当前的时间
- void CGkbc8Dlg::OnButton1()
- {
- CTime time1;
- CTime time2;
- //获得日期和时间
- m_ctrlDateTime1.GetTime(time1);
- m_ctrlDateTime2.GetTime(time2);
- CString strText = _T("");
- strText.Format(_T("%04d-%02d-%02d\n%02d:%02d:%02d"),
- time1.GetYear(), time1.GetMonth(), time1.GetDay(),
- time2.GetHour(), time2.GetMinute(), time2.GetSecond());
- AfxMessageBox(strText);
- // TODO: Add your control notification handler code here
-
- }
复制代码
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料! 
|