创建一个基于对话框的工程,添加控件,如下图所示:
各控件ID及变量如下:
// Ch13Demo2Dlg.h
typedef struct Threadinfo{
CProgressCtrl *progress;//进度条对象
int speed;//进度条速度
int pos;//进度条位置
} thread,*lpthread;
class CCh13Demo2Dlg : public CDialog
{
……
protected:
HICON m_hIcon;
thread thread1;//线程1的结构
thread thread2;//线程2的结构
thread thread3;//线程3的结构
HANDLE hThread1;//线程1线程句柄
HANDLE hThread2;//线程2线程句柄
HANDLE hThread3;//线程3线程句柄
// Ch13Demo2Dlg.cpp
DWORD WINAPI ThreadFun(LPVOID pthread)//线程入口函数
{
lpthread temp=(lpthread)pthread;//进度条结构体
temp->progress->SetPos(temp->pos);
while(temp->pos<20)
{
Sleep(temp->speed);//设置速度
temp->pos++;//增加进度
temp->progress->SetPos(temp->pos);//设置进度条的新位置
if(temp->pos==20)
{
temp->pos=0;//进度条满则归0
}
}
return true;
}
// Ch13Demo2Dlg.cpp
BOOL CCh13Demo2Dlg::OnInitDialog()
{
BOOL CCh13Demo2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
……
m_progress1.SetRange(0,20);//设置进度条范围
m_progress2.SetRange(0,20);//设置进度条范围
m_progress3.SetRange(0,20);//设置进度条范围
GetDlgItem(IDC_PAUSE1)->EnableWindow(FALSE);//停止按钮无效
GetDlgItem(IDC_PAUSE2)->EnableWindow(FALSE);//停止按钮无效
GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按钮无效
return TRUE;
}
// Ch13Demo2Dlg.cpp
DWORD WINAPI ThreadFun(LPVOID pthread)//线程入口函数
{
lpthread temp=(lpthread)pthread;//进度条结构体
temp->progress->SetPos(temp->pos);
while(temp->pos<20)
{
Sleep(temp->speed);//设置速度
temp->pos++;//增加进度
temp->progress->SetPos(temp->pos);//设置进度条的新位置
if(temp->pos==20)
{
temp->pos=0;//进度条满则归0
}
}
return true;
}
void CCh13Demo2Dlg::OnStar1()
{
// TODO: Add your control notification handler code here
DWORD ThreadID;
DWORD code;
//生成线程参数
thread1.progress=&m_progress1;//进度条对象
thread1.speed=100;//速度
thread1.pos=0;//初始位置
if(!GetExitCodeThread(hThread1,&code)||(code!=STILL_ACTIVE))
{
hThread1=CreateThread(NULL,0,ThreadFun,&thread1,0,&ThreadID);//创建并开始线程
}
GetDlgItem(IDC_PAUSE1)->EnableWindow(TRUE);//停止按钮生效
GetDlgItem(IDC_STAR1)->EnableWindow(FALSE);//开始按钮无效
}
void CCh13Demo2Dlg::OnStar2()
{
// TODO: Add your control notification handler code here
DWORD ThreadID;
DWORD code;
//生成线程
thread2.progress=&m_progress2;//线程结构
thread2.speed=200;
thread2.pos=0;
if(!GetExitCodeThread(hThread2,&code)||(code!=STILL_ACTIVE))
{
hThread2=CreateThread(NULL,0,ThreadFun,&thread2,0,&ThreadID);//创建线程
}
GetDlgItem(IDC_PAUSE2)->EnableWindow(TRUE);//停止按钮生效
GetDlgItem(IDC_STAR2)->EnableWindow(FALSE);//开始按钮无效
}
void CCh13Demo2Dlg::OnStar3()
{
// TODO: Add your control notification handler code here
DWORD ThreadID;
DWORD code;
//生成线程
thread3.progress=&m_progress3;//线程结构
thread3.speed=200;
thread3.pos=0;
if(!GetExitCodeThread(hThread3,&code)||(code!=STILL_ACTIVE))
{
hThread3=CreateThread(NULL,0,ThreadFun,&thread3,0,&ThreadID);//创建线程
}
GetDlgItem(IDC_PAUSE3)->EnableWindow(TRUE);//停止按钮生效
GetDlgItem(IDC_STAR3)->EnableWindow(FALSE);//开始按钮无效
}
void CCh13Demo2Dlg::OnPause1()
{
// TODO: Add your control notification handler code here
DWORD code;
if(GetExitCodeThread(hThread1,&code))
if(code==STILL_ACTIVE)//如果当前线程还活动
{
TerminateThread(hThread1,0);//前些终止线程
CloseHandle(hThread1);//销毁线程句柄
}
GetDlgItem(IDC_PAUSE1)->EnableWindow(FALSE);//停止按钮无效
GetDlgItem(IDC_STAR1)->EnableWindow(TRUE);//开始按钮生效
}
void CCh13Demo2Dlg::OnPause2()
{
// TODO: Add your control notification handler code here
DWORD code;
if(GetExitCodeThread(hThread2,&code))
if(code==STILL_ACTIVE)
{
TerminateThread(hThread2,0);
CloseHandle(hThread2);
}
GetDlgItem(IDC_PAUSE2)->EnableWindow(FALSE);//停止按钮无效
GetDlgItem(IDC_STAR2)->EnableWindow(TRUE);//开始按钮生效
}
void CCh13Demo2Dlg::OnPause3()
{
// TODO: Add your control notification handler code here
DWORD code;
if(GetExitCodeThread(hThread3,&code))
if(code==STILL_ACTIVE)
{
TerminateThread(hThread3,0);
CloseHandle(hThread2);
}
GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按钮无效
GetDlgItem(IDC_STAR3)->EnableWindow(TRUE);//开始按钮生效
}