对于控制进度条:
创建一个全局结构体Threadinfo,存储进度条对象的信息
typedef struct Threadinfo
{
CProgressCtrl *progress; //进度条对象
int speed; //进度条速度
int pos; //进度条位置
}
这样用比较方便
开始进程吧,
(1)创建一个进程句柄
HANDLE hThread; //线程的线程句柄
(2)定义全局线程入口函数,这是一个回调函数
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)使用API函数CreateThread函数创建线程,使用API函数TerminateThread终止线程,并销毁线程句柄
if (!GetExitCodeThread(hThread1,&code) || (code!=STILL_ACTIVE))
{
hThread1 = CreateThread(NULL,0,ThreadFun,&thread1,0,&ThreadID);
//创建并开始线程
}
此时线程已经创建,并且运行线程函数
if (GetExitCodeThread(hThread1,&code)) //如果当前线程 活动
{
if (code == STILL_ACTIVE)
{
TerminateThread(hThread1,0);
//终止线程
CloseHandle(hThread1); //销毁线程句柄
m_progress.SetPos(0);
}
}
要点说明
CreateThread: 创建一个新的进程,返回已建进程的句柄
SuspendThread: 用于挂起指定线程,终止线程的执行
ResumeThread: 将进程挂起状态计数值减1
ExitThread :由线程自身完成终止执行的操作
TerminateThread: 强行终止某一进程的操作
GetThreadPriority: 获取一个进程的优先级
SetThreadPriority: 设定一个进程所在进程中的相对优先级大小
PostThreadMessage :用于向线程发布消息,即将消息放到指定的消息队列中,并且立即返回。