Interface study notes (3) MFC timer

The timer is not as troublesome as you think, as long as you understand what a timer is and what its three parameters are. Recommend these articles in the blog garden:
Use MFC to write a small timer program (DelmoreZhu)
MFC commonly used class: Timer Timer (chicken pecks rice)

Introduction to SetTimer

MFC does not have a built-in clock. You need to set what the clock looks like. The timer refers to how often to perform a custom operation. But MFC comes with two functions, SetTimer and KillTimer. No additional header file is needed, just use it directly.
SetTimer(1, 1000, NULL); The
timer is not a control, so there are no general settings. You cannot see the ID in the class wizard. Its ID is set by yourself, which is its first parameter. You can set a number. It can be a general ID name, like ID_TIMER1.
The second parameter is the interval time. The unit is milliseconds, 1s=1000ms. The second parameter means that the function of the third parameter is executed every so many times.
Yes, the third parameter is the function operation. If it is NULL, it is the OnTimer function, which can be selected in the class wizard or added to the dialog class by yourself.

OnTimer function

void CSudoku2Dlg::OnTimer(UINT_PTR nIDEvent);

In the class wizard, select "Message" -> WM_TIMER -> "Add Handler" -> "Edit Code", there will be this function.
Insert picture description here

Example

The setting for the following example is: the display time of the edit box, the unit is second, with a check box and a pause button. The text of the pause button can be switched between "Pause" and "Resume". After clicking "New Game", the timing has started. The check box only determines whether to display or not, and it is 0 when it is not displayed. I don’t know what will happen if I don’t KillTimer after SetTimer. You can read other people’s blogs about putting KillTimer in OnDestroy.

// 勾选框
void CSudoku2Dlg::OnBnClickedCheck1()      
{
    
    
	// TODO: 在此添加控件通知处理程序代码
	if (BST_CHECKED == IsDlgButtonChecked(IDC_CHECK1))  //你要知道勾选框是动作 不是状态
	{
    
    
		// 勾选
		CString cstr("显示计时");
		MessageBox(cstr);
		SetTimer(1,1000,NULL);   //计时器的三个参数:ID,间隔,函数,NULL就是OnTimer函数
	}
	else {
    
    
		KillTimer(1);
		CString t;                       //加值类型CString变量也行
		t = "0";
		SetDlgItemText(IDC_EDIT1, t);   //CString类型的0就是空格吗  嗯不是
	}
}

Pause button:
Define a global variable in the .h file:

bool suspend = FALSE;
void CSudoku2Dlg::OnBnClickedButton8()          // 暂停
{
    
    
	// TODO: 在此添加控件通知处理程序代码
	suspend = !suspend;
	if (suspend)
	{
    
    
		int a = GetTickCount();
		int b = a - starttime;
		starttime = b;
		spendtime /= 1000;
		CString tt;
		tt.Format(_T("%ld"), spendtime);
		GetDlgItem(IDC_BUTTON8)->SetWindowText(L"继续");
		KillTimer(1);
		//CString cstr("已暂停");
		//MessageBox(cstr);
		UpdateData(FALSE);
	}
	else
	{
    
    
		int a = GetTickCount();
		int b = a - starttime;
		starttime = b;
		GetDlgItem(IDC_BUTTON8)->SetWindowText(L"暂停");
		//CString cstr("已继续");
		//MessageBox(cstr);
		SetTimer(1, 1000, NULL);
	}

}

Custom function:

void CSudoku2Dlg::OnTimer(UINT_PTR nIDEvent)     //计时器操作
{
    
    
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	int current= GetTickCount();
	spendtime = current - starttime;
	spendtime /= 1000;
	CString tt;
	tt.Format(_T("%ld"), spendtime);
	SetDlgItemText(IDC_EDIT82, tt);
	CDialogEx::OnTimer(nIDEvent);
}

Other bloggers’ blogs say that when multiple timers are used, switch is used to distinguish operations in this function.
It's already very specific, and let's talk a little bit more to get the time spent. Define the global variables starttime and spendtime, and add at the end of your "new game" and "replay" button functions:

 starttime = GetTickCount();
 ///SetTimer(1,1000,NULL);   
 OnBnClickedCheck1();      // 还可以这样

After judging the game passed:

	CString cstr("已正确完成,用时");   
	CString cstr2("ms");
	spendtime = GetTickCount() - starttime;
	CString t;
	t.Format(_T("%ld"),spendtime);
	CString aa = cstr + t+cstr2;
	MessageBox(aa);
	SetDlgItemText(IDC_EDIT1, t);
	KillTimer(1);

Guess you like

Origin blog.csdn.net/qq_43144103/article/details/105866392