CreateThread() 函数中的lpStartAddress参数可否为 某个类的成员函数?
#include "stdafx.h"
#include "ball.h"
Cball::Cball(HWND hCanvas,int nR,int nX,int nY,int nDelay)
{
this->hCanvas = hCanvas;
this->nR = nR;
this->nX = nX;
this->nY = nY;
this->nDelay = nDelay;
}
Cball::Cball(HWND hCanvas,int nR,int nDelay)
{
this ->hCanvas = hCanvas;
this ->nR = nR;
RECT rt;
GetClientRect (hCanvas,&rt);
this ->nX = nR;
this ->nY = rt.bottom - nR;
this ->nDelay = nDelay;
}
void Cball::setBrush(HBRUSH hBrush)
{
this ->hBrush = hBrush;
}
void Cball::start()
{
if (hThread!=NULL )return;
/*
下面这行语句这样写为什么不行?
*/
hThread = CreateThread (NULL,0,run,0,CREATE_SUSPENDED,NULL);
}
void Cball::resume()
{
ResumeThread (hThread);
}
void Cball::pause()
{
SuspendThread (hThread);
}
void Cball::stop()
{
TerminateThread (hThread,0);
}
DWORD WINAPI Cball::run(LPVOID lpParam)
{
HDC hdc;
int nXO,nYO;
HBRUSH hBrushOld;
hdc = GetDC (hCanvas);
if (hBrush!=FALSE)
hBrushOld = (HBRUSH)SelectObject (hdc,hBrush);
while(1)
{
RECT r;
GetClientRect (hCanvas,&r);
if (nX>=r.right || nX<=r.left )nXO = - nXO;
if (nY>=r.bottom || nY<=r.top)nYO = -nYO;
nX += 5 * nXO;
nY += 5 * nYO;
Ellipse (hdc,nX - nR,nY - nR,nX + nR,nY + nR);
InvalidateRect (hCanvas,&r,TRUE);
Sleep (nDelay);
}
ReleaseDC (hCanvas,hdc);
if (hBrushOld != FALSE)
SelectObject (hdc,hBrushOld);
return 0;
}