DWORD WINAPI ThreadProc(LPVOID pParam)
{
CDC* pdc;
//将传入的参数转为RECT*
RECT* rect=(RECT*)pParam;
//得到设备句柄
pdc=AfxGetApp()->m_pMainWnd->GetDC();
//设置画刷
CBrush brush(HS_VERTICAL,RGB(0,255,0));
//选入到设备中
CBrush* oldbrush=pdc->SelectObject(&brush);
int n=0;
while(n++<=2000)
{
pdc->Rectangle(rect->left,rect->top,rect->right+n,rect->bottom );
Sleep(1);
}
pdc->SelectObject(oldbrush);
pdc->ReleaseOutputDC();
return 0;
}
添加一个按钮控件IDTHREAD,该按钮事件代码如下
void CCreateThreadRectDlg::OnThread()
{
// TODO: Add your control notification handler code here
RECT rect1;
rect1.left=20; //左上角x坐标
rect1.top=100; //左上角y坐标
rect1.right=20; //右下角x坐标
rect1.bottom =rect1.top +100; //右下角y坐标
//创建第一个线程ThreadProc,相对优先级THREAD_PRIORITY_IDLE面对任何等级调整为1
AfxBeginThread((AFX_THREADPROC)ThreadProc,&rect1,THREAD_PRIORITY_IDLE);
RECT rect2;
rect2.left=rect1.left;
rect2.top=rect1.bottom +20;
rect2.right=rect1.right;
rect2.bottom =rect2.top +100;
//创建第二个线程ThreadProc,相对优先级THREAD_PRIORITY_TIME_CRITICAL
//面对任何等级调整为15,面对REALTIME等级调整为32
AfxBeginThread((AFX_THREADPROC)ThreadProc,&rect2,THREAD_PRIORITY_TIME_CRITICAL);
}
运行结果: