一、效果展示
二、实现方法
1、打开VS2010(或其他版本VS/VC),新建一个名称为GetSystemTime的基于对话框MFC程序,设计好程序界面:
2、设置属性:
编辑框:ID: IDC_EDIT Read only: True Align Text: Center
按钮:ID: IDC_Get_System_Time Default Button: True
3、给编辑框添加一个CEdit类型的变量m_edit。
4、双击按钮,添加按钮按下事件处理程序OnBnClickedGetSystemTime,代码如下:
void CGetSystemTimeDlg::OnBnClickedGetSystemTime()
{
// TODO: 在此添加控件通知处理程序代码
SYSTEMTIME st; //定义SYSTEMTIME变量st
CString strDate,strTime; //定义CString变量strDate和strTime
GetLocalTime(&st); //获取系统时间到st
strDate.Format(_T("%4d-%2d-%2d"),st.wYear,st.wMonth,st.wDay); //设置strDate变量的格式为年-月-日
strTime.Format(_T("%2d:%2d:%2d"),st.wHour,st.wMinute,st.wSecond); //设置strTime变量的格式为时:分:秒
m_edit.SetWindowText(strDate+_T(" ")+strTime); //设置编辑框文本,以年-月-日 时:分:秒的格式呈现给用户
}
5、编译程序,运行程序单击按钮可获得当前系统时间。
注:本文原创,转载请注明出处。
喜欢的点赞关注哦!