1 創建式程,編輯對話框資源
創建一個基于對話框的工程,添加控件,如下圖所示:


各控件ID及變量如下:


2 在頭文件中定義與線程相關變量
// 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;
}
3 對話框控件初始化
// 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);//開始按鈕生效
}


版權聲明:本文內容由互聯網用戶自發貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發現本站有涉嫌抄襲侵權/違法違規的內容, 請發送郵件至 舉報,一經查實,本站將立刻刪除。