如何在不刷新文本框的情况下创建计时器 [英] How to create the timer without refreshing the text box
问题描述
我在我的应用程序中使用Timer。我使用 SetTimer(1,999,NULL)
显示当前系统时间和当前运行的应用程序时间。我创建了一个文本框和开始按钮。
当我在文本框中输入整数值(例如10)并按开始按钮时,消息框应在10秒后弹出。为此,我创建了另一个 SetTimer(2,m_Sec * 1000,NULL)
。一切都很好但是当我每秒执行一次应用程序时,文本框会刷新。
我不能在该文本框中输入值。
我必须做出哪些改变。
这是我的代码。
m_Sec
是文本框的 UINT
变量。
I am using the Timer in my application. I displayed the current system time and currently running application time using the SetTimer(1,999,NULL)
. I created one text box and start button.
When I enter the integer value in the text box (ex: 10) and press the start button, the message box should pop-up after 10 seconds. For this I created another SetTimer(2,m_Sec*1000,NULL)
. Everything was fine but when I execute the application for every sec the text box is refreshed.
I can''t enter the value in that text box.
What changes I have to make.
Here is my code.
m_Sec
is the UINT
variable of the text box.
// TickCounterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TickCounter.h"
#include "TickCounterDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTickCounterDlg dialog
CTickCounterDlg::CTickCounterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTickCounterDlg::IDD, pParent)
, m_Comp(_T(""))
, m_App(_T(""))
, m_Sec(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTickCounterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMP_TIME, m_CompTime);
DDX_Control(pDX, IDC_APP_TIME, m_AppTime);
DDX_Text(pDX, IDC_COMP_TIME, m_Comp);
DDX_Text(pDX, IDC_APP_TIME, m_App);
DDX_Text(pDX, IDC_SEC, m_Sec);
}
BEGIN_MESSAGE_MAP(CTickCounterDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_TIMER()
ON_BN_CLICKED(IDC_START, &CTickCounterDlg::OnBnClickedStart)
END_MESSAGE_MAP()
// CTickCounterDlg message handlers
BOOL CTickCounterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CompTime = CTime::GetTickCount();
SetTimer(1, 999, NULL);
SetTimer(2,10000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTickCounterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<wparam>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTickCounterDlg::OnQueryDragIcon()
{
return static_cast<hcursor>(m_hIcon);
}
void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
CTime CurTime=CTime::GetTickCount();
CTimeSpan Difference=CurTime-CompTime;
unsigned int compHour,compMin,compSec;
unsigned int curSec,curMin,curHour;
compHour=Difference.GetHours();
compMin=Difference.GetMinutes();
compSec=Difference.GetSeconds();
curHour=CurTime.GetHour();
curMin=CurTime.GetMinute();
curSec=CurTime.GetSecond();
m_Comp.Format(L"This computer current Time is %d hours, %d minutes %d seconds", curHour, curMin, curSec);
m_App.Format(L"This application has been running for %d hours, %d minutes %d seconds", compHour, compMin, compSec);
}
if(nIDEvent==2)
{
MessageBox(L"hai");
KillTimer(2);
}
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CTickCounterDlg::OnBnClickedStart()
{
// TODO: Add your control notification handler code here
UpdateData();
SetTimer(2,m_Sec*1000,NULL);
}
推荐答案
UpdateData(False)将刷新所有控件(并用成员变量中保存的内容填充它们) )
你可以在OnTimer或更好的开头添加UpdateData(true),在你要更新的控件上调用SetWindowText而不是调用UpdateData!
希望这有助于
The UpdateData(False) will refresh all controls (and fill them with the content held in the member variable)
You could add UpdateData(true) at the beginning of OnTimer or better, Call SetWindowText on the controls you want to update rather than calling UpdateData!
Hope this helps
好的,你需要这样做:
OK you need to do this:
void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
CTime CurTime=CTime::GetTickCount();
CTimeSpan Difference=CurTime-CompTime;
unsigned int compHour,compMin,compSec;
unsigned int curSec,curMin,curHour;
compHour=Difference.GetHours();
compMin=Difference.GetMinutes();
compSec=Difference.GetSeconds();
curHour=CurTime.GetHour();
curMin=CurTime.GetMinute();
curSec=CurTime.GetSecond();
CString strComp,strApp;
strComp.Format(L"This computer current Time is %d hours, %d minutes %d seconds", curHour, curMin, curSec);
m_CompTime.SetWindowText(strComp);
strApp.Format(L"This application has been running for %d hours, %d minutes %d seconds", compHour, compMin, compSec);
m_appTime.SetWindowText(strApp);
}
if(nIDEvent==2)
{
MessageBox(L"hai");
KillTimer(2);
}
CDialog::OnTimer(nIDEvent);
}
使用 SetDlgItemText
,这是Dialogbox特定的功能! 链接 [ ^ ]
Use SetDlgItemText
, it''s Dialogbox specific function! Link[^]
这篇关于如何在不刷新文本框的情况下创建计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!