一个定时器,时间到了弹出提示框

对话框界面上一个Combox,一个计时按钮,一个停止按钮

OnInitDialog代码:

BOOL CSetTimerDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	//WS_EX_TOOLWINDOW不显示任务栏图标,但需要去掉WS_EX_APPWINDOW
	//WS_EX_TOPMOST置顶
	//WS_EX_LAYERED设置透明
	ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_LAYERED);

	SetLayeredWindowAttributes(RGB(255, 0, 255), 0x80, LWA_COLORKEY | LWA_ALPHA);
	//设置下拉框
	m_comBox.InsertString(0,_T("1"));
	m_comBox.InsertString(1,_T("5"));
	m_comBox.InsertString(2,_T("10"));
	m_comBox.InsertString(3,_T("15"));
	m_comBox.InsertString(4,_T("30"));
	m_comBox.InsertString(5,_T("60"));

	//设置倒计时初始值
	SetDlgItemText(IDC_STATIC1,_T("00"));
	SetDlgItemText(IDC_STATIC2,_T("00"));

	SetDlgItemText(IDC_STATIC3,_T("设置计时时间:"));
	SetDlgItemText(IDC_STATIC4,_T("分钟"));

	//设置按钮
	SetDlgItemText(IDC_BUTTON1,_T("计时开始"));
	SetDlgItemText(IDC_BUTTON2,_T("计时结束"));
	GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}



点击计时按钮的代码:

void CSetTimerDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here

	//获取ComBox的值
	CString str;
	m_comBox.GetWindowText(str);
	if (str.IsEmpty())
	{
		MessageBox(_T("请输入定时时间!"));
		return;
	}

	
	for (int i = 0; i < str.GetLength(); i++)
	{
		if (str.GetAt(i) < '0' || str.GetAt(i) > '9')
		{
			MessageBox(_T("时间必须为整数!"));
			return;
		}
	}
	

    //将字符串转换成整型
	m_iTime = _ttoi(str);
	if (m_iTime < 1)
	{
		MessageBox(_T("定时时间不能小于1分钟!"));
		return;
	}
	if (m_iTime > 60)
	{
		MessageBox(_T("定时时间不能大于60分钟!"));
		return;
	}
	
	minute = m_iTime;
	second = 0;

	m_strMin.Format(_T("%d"), minute);
	m_strSec.Format(_T("%d"), second);
	if (m_strMin.GetLength() < 2)
	{
		m_strMin.Insert(0,'0');
	}
	if (m_strSec.GetLength() < 2)
	{
		m_strSec.Insert(0,'0');
	}
	SetDlgItemText(IDC_STATIC1,m_strMin);
	SetDlgItemText(IDC_STATIC2,m_strSec);

	SetTimer(1,1000,NULL);

	GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
		
}


停止计时的代码:

void CSetTimerDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	
	KillTimer(1);
	SetDlgItemText(IDC_STATIC1,_T("00"));
	SetDlgItemText(IDC_STATIC2,_T("00"));
	minute = 0;
	second = 0;
	
	GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
	
	
}


OnTimer代码:

void CSetTimerDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if (0 == second)
	{
		second = 60;
		//minute--;
	}
	if (60 == second)
	{
		minute--;
	}
	if (0 > minute)
	{
		minute = 0;
	}

    second--;
	
	m_strSec.Format(_T("%d"), second);
	m_strMin.Format(_T("%d"), minute);
	if (m_strSec.GetLength() < 2)
	{
		m_strSec.Insert(0,'0');
	}
	if (m_strMin.GetLength() < 2)
	{
		m_strMin.Insert(0,'0');
	}
	SetDlgItemText(IDC_STATIC2,m_strSec);
	SetDlgItemText(IDC_STATIC1,m_strMin);

	if ((0 == second) && (0 == minute))
	{
		KillTimer(1);
		//MessageBox(_T("时间到"));
		ShowTipWindow(_T("时间到"));
		GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	    GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
	}

	CDialogEx::OnTimer(nIDEvent);
}

提示框渐入渐出的效果参考: http://blog.csdn.net/segen_jaa/article/details/7848598

你可能感兴趣的:(一个定时器,时间到了弹出提示框)