开发环境WINXP,VC6.0用MFC实现拔码数字效果实时显示系统的分/秒时钟。这里要用到线程编程技术,具体过程可查看本人博客文章《MFC直接用AfxBeginThread(ThreadProc,this)创建线程实例》。
新建一个DataNumber的mfc对话框项目文件,对资源视图上拖一个静态文本控件IDC_STATIC_1,复制粘贴至IDC_STATIC_30,5个为一组共6组。静态文本控件属性设置按顺序布局调整如下。
1、在DataNumberDlg.h头文件类声明public:下添加代码:
CFont m_sFont,m_font;
CWinThread* pThread;
void My_Func();
static UINT My_TheradFunc(LPVOID lparam);
2、在DataNumberDlg.cpp中最后一个#include行语句下面添加代码:
UINT m_nCounterID[6][5] =
{
{
IDC_STATIC_1,
IDC_STATIC_2,
IDC_STATIC_3,
IDC_STATIC_4,
IDC_STATIC_5
},
{
IDC_STATIC_6,
IDC_STATIC_7,
IDC_STATIC_8,
IDC_STATIC_9,
IDC_STATIC_10
},
{
IDC_STATIC_21,
IDC_STATIC_22,
IDC_STATIC_23,
IDC_STATIC_24,
IDC_STATIC_25
},
{
IDC_STATIC_11,
IDC_STATIC_12,
IDC_STATIC_13,
IDC_STATIC_14,
IDC_STATIC_15
},
{
IDC_STATIC_16,
IDC_STATIC_17,
IDC_STATIC_18,
IDC_STATIC_19,
IDC_STATIC_20
},
{
IDC_STATIC_26,
IDC_STATIC_27,
IDC_STATIC_28,
IDC_STATIC_29,
IDC_STATIC_30
},
};
-
3、在DataNumberDlg.cpp中构造函数CDataNumberDlg::CDataNumberDlg(CWnd* pParent /
=NULL/)
- CDialog(CDataNumberDlg::IDD, pParent)中添加代码:
m_sFont.CreateFont( -30, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
700, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
134, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"宋体");
4、在DataNumberDlg.cpp中OnInitDialog函数中添加生成线程代码(return TRUE语前):
pThread = AfxBeginThread(My_TheradFunc,(LPVOID)this);
5、在DataNumberDlg.cpp中线程函数的定义代码:
UINT CDataNumberDlg::My_TheradFunc(LPVOID lparam)//将窗口指针赋给无类型指针
{
CDataNumberDlg *dlg = (CDataNumberDlg*)lparam; //可以调用CTheradtDlg定义的类成员函数
while (1)
{
dlg->My_Func(); //线程要执行的函数(调用CTheradtDlg定义的类成员函数)
}
return TRUE;
}
6、线程要处理的函数代码:
void CDataNumberDlg::My_Func() //处理函数在这里写
{
CTime tm;
CString start_time;
tm=CTime::GetCurrentTime();
start_time=tm.Format("%M:%S");
int j_1 ;
j_1=strlen(start_time);
CString str[30];
for ( int i=0 ; i<6 ; i++)
{
for( j_1=0 ; j_1 <5 ; j_1++)
{
GetDlgItem(m_nCounterID[i][j_1])->SetFont(&m_sFont);
str[j_1]=start_time.Mid(j_1,1);
SetDlgItemText(m_nCounterID[i][j_1],str[j_1]);
}
}
Sleep(1000);
}
运行程序结果如下:
源码下载
https://download.csdn.net/download/weixin_42350092/87671893