VOID Thread (PVOID pvoid)
{HBRUSH hBrush ;
HDC hdc ;
int xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue ;
while (TRUE)
{
if (cxClient != 0 || cyClient != 0)
{
xLeft = rand () % cxClient ;
xRight = rand () % cxClient ;
yTop = rand () % cyClient ;
yBottom = rand () % cyClient ;
iRed = rand () & 255 ;
iGreen = rand () & 255 ;
iBlue = rand () & 255 ;
hdc = GetDC (hwnd) ;
hBrush = CreateSolidBrush (RGB (iRed, iGreen, iBlue)) ;
SelectObject (hdc, hBrush) ;
Rectangle (hdc, min (xLeft, xRight), min (yTop, yBottom),
max (xLeft, xRight), max (yTop, yBottom)) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
}
}
LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch (message)
{
case WM_CREATE:
_beginthread (Thread, 0, NULL) ;
return 0 ;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
中包含了表头档案PROCESS.H,这个档案定义一个名为_beginthread 的函数
,它启动一个新的执行绪。只有定义了_MT 识
别字,才会申明这个函式,这是/MT 旗标的另一个结果。
由CreateWindow 传回的hwnd 值被储存
在一个整体变数中,因此cxClient 和cyClient 值也可以由视窗讯息处理程式
的WM_SIZE 讯息获得。
视窗讯息处理程数以最容易的方法呼叫_beginthread——简单地以执行绪
函数的位址(称为Thread)作为第一个参数,其他参数使用0,执行绪函数传
回VOID并有一个参数,该参数是一个指向VOID的指针。
在RNDRCTMT中的Thread
函数不使用这个参数。
对程序中的所有执行绪来说,所有的静态变量都是一样的。
这就是视窗讯息处理程式设定整体的cxClient 和cyClient 变量并由Thread 函式使用的方式。