vc++6.0/Use VisualC++6.0 to create MFC basic dialog box program to make digital clock tutorial

Let's take a look at the rendering of the digital clock first

1. First, we first create a basic dialog box program, if you don’t, you can click here: Use Visual C++6.0 to create MFC project single document, multiple documents, dialog interface ;

2. Customize a dialog box class (you can also directly use the basic dialog box you just created), this class is inherited from the base class (parent class) CDialog of all our dialog boxes, you can refer to: how to in MFC Implement custom dialog box class ;

3. Add corresponding controls to the customized dialog box in 2. Here, static text is used as a digital clock display control, and its ID is set to: IDC_STATIC_Time ;

Add a title to our pop-up dialog box (DigitalClock)

4. Add a button to our main dialog, name it digital clock, remember the ID of the button at this time


5. Add a mapping response to the digital clock button, open the class wizard, and add a click response to our digital clock button

6. Create a class wizard, add a DoModal function to our mydialog class, this function allows us to click the digital clock button, pop up our digital clock dialog box, and then add the following code to the button click function we added:

mydialog dialog;//Create a dialog object

dialog.DoModa();//Pop up our digital clock dialog

Then we found that an error occurred during runtime

This is because the header file of our newly created object is not included in the .cpp file of our main dialog box. It does not know who it is. So we can add

#include "mydialog.h" is at the beginning of textDlg.cpp, so it can run.

The detailed operation process is as follows:

7. For our static text control (to use the ID of the control , the ID has been set above), bind the variable

8. Add the dialog initialization function in the mydialog.h file, add the implementation of the initialization function in the mydialog.cpp file, and set the timer

Of course, the initialization function has a return value

9. First add the timer mapping to get the OnTimer function

10. Write the editing part of our digital clock in the OnTimer function

void mydialog::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	SYSTEMTIME st;//创建系统时间对象
	GetLocalTime(&st);//获得当前系统时间
	CString strtime;//设置一个字符变量
	strtime.Format("%2d:%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond,st.wMilliseconds/10);//将系统时间格式化为字符串
	m_Time=strtime;//将格式化为字符串的时间赋值给静态文本控件所绑定的变量
	UpdateData(FALSE);//更新对话框控件的值
	CDialog::OnTimer(nIDEvent);
}

11. Finally we want to close the timer, first need to add a destroyed mapping (I have already added here)

Add the following code in the function

void mydialog::OnDestroy() 
{
	CDialog::OnDestroy();
	m
	// TODO: Add your essage handler code here
	KillTimer(1);//关闭定时器
	
}

 Finally, take a look at the results of the operation:

Guess you like

Origin blog.csdn.net/qq_25036849/article/details/108820393