小弟初学windows编程,在这里碰见了一个问题,void DrawRectangle(HWND hwnd)
{ HBRUSH hBrush,h2,h3;
HDC hdc;
RECT rect;
LOGBRUSH logb;
logb.lbColor = RGB(rand()%256,rand()%256,rand()%256);
logb.lbHatch = NULL;
logb.lbStyle = BS_HATCHED;
if(cxClient == 0|| cyClient == 0)
return ;
SetRect(&rect,rand()%cxClient, rand()%cyClient,rand()%cxClient,
rand()%cyClient);
hBrush = CreateHatchBrush(HS_DIAGCROSS,RGB(rand()%256,rand()%256,rand()%256));////h2=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
/////我的本意是在这里初始化h2和h3后就可以实现多种不同矩形的随机,但是初始化后随机几秒钟后就停止了,
////如果不用这两个画刷,就能够一直随机并显示,想知道为什么, hdc = GetDC(hwnd);
FillRect(hdc, &rect, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
这是windows程序设计上的一个例题,小弟初学,请各位大虾指教

解决方案 »

  1.   

    ////h2=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
    /////我的本意是在这里初始化h2和h3后就可以实现多种不同矩形的随机,但是初始化后随机几秒钟后就停止了,
    ////如果不用这两个画刷,就能够一直随机并显示,想知道为什么,==============
    你的问题描述不是很清楚,可以是GDI资源没有释放吧
      

  2.   

    // only once !
    if(!hBrush) hBrush= CreateHatchBrush(HS_DIAGCROSS,RGB(rand()%256,rand()%256,rand()%256));if(!h2) h2=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
      

  3.   

    h2=CreateSolidBrush以后释放了没有?