您的位置:资讯频道 > 技术文档 > VC/MFC

VC中添加串口控件 添加timer

来源:博客   作者:dycuswine2   时间:2008-03-27  点击:342 次
1.添加串口控件。

1)project-add to project-components and controls-Registered ActiveX Controls-Microsoft Communications Control, version 6.0-insert

2)classwizard-message maps-IDC_MSCOMM-添加消息(接到数据有响应)

3)member variables(添加变量)

2.添加timer:

1)classwizard-message maps-***Dlg-WM_TIMER

2)在OnInitDialog()中添加SetTimer(10, 1000, NULL);

注:显示系统时间的参考程序(static的ID为IDC_STATIC_TIMER)

CTime time = CTime::GetCurrentTime(); //全局函数,取出当前的时间,放到ctime的累中

int y = time.GetYear();
int mon = time.GetMonth();
int d = time.GetDay();
int h = time.GetHour();
int min = time.GetMinute();
int s = time.GetSecond();

CString sy, smon, sd, sh, smin, ss, sTime;

sy.Format("%d", y);
smon.Format("%d", mon);
sd.Format("%d", d);
if (h < 10) //使小时是双位
{
 sh.Format("0%d", h);
}
else
{
 sh.Format("%d", h);
}
if (min < 10) //使分钟是双位
{
 smin.Format("0%d", min);
}
else
{
 smin.Format("%d", min);
}
if (s < 10) //使秒是双位
{
 ss.Format("0%d", s);
}
else
{
 ss.Format("%d", s);
}

sTime = sy + "-" +smon + "-" + sd + " " + sh + ":" + smin + ":" + ss;

CStatic *pDisplayTime;
pDisplayTime = (CStatic*)GetDlgItem(IDC_STATIC_TIMER);
pDisplayTime->SetWindowText(sTime); //在文本中显示当前时间
CDialog::OnTimer(nIDEvent);