//Win32 Application #include<windows.h> #include<stdlib.h> LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM); void RandRECT(HWND,int,int); int cxClient,cyClient; WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpcmdLine,int nShowCmd) { HWND hwnd; MSG msg; WNDCLASS wc; TCHAR szAppname[] = TEXT("随机矩形"); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); wc.hInstance = hInstance; wc.lpfnWndProc = WinProc; wc.lpszClassName = szAppname; wc.lpszMenuName = NULL; wc.style = CS_HREDRAW|CS_VREDRAW; if(!RegisterClass(&wc)) { MessageBox(NULL,TEXT("警告框"),TEXT("窗口类注册失败!"),MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname,szAppname,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(WM_QUIT==msg.message) break; if(WM_PAINT==msg.message) ValidateRec(hwnd,NULL); TranslateMessage(&msg); DispatchMessage(&msg); } else RandRECT(hwnd,cxClient,cyClient); } return msg.wParam; } LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam, LPARAM lparam) { switch(message) { case WM_SIZE: cxClient = LOWORD(lparam); cyClient = HIWORD(lparam); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wparam,lparam); } void RandRECT(HWND hwnd,int x, int y) { RECT rect; HDC hdc; HBRUSH hbrush; HPEN hpen; int a,b,c,d; if(x==0||y==0) return; hdc = GetDC(hwnd); hpen = CreatePen(PS_SOLID,10,RGB(rand()%256,rand()%256,rand()%256)); SelectObject(hdc,hpen); SetRect(&rect,a=rand()%x,b=rand()%y,c=rand()%x,d=rand()%y); SelectObject(hdc,GetStockObject(NULL_BRUSH)); Rectangle(hdc,a,b,c,d); for(int i=0; i<5000000; i++); hbrush = CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256)); FillRect(hdc,&rect,hbrush); for(i=0; i<50000000; i++); //延时 ReleaseDC(hwnd,hdc); DeleteObject(hbrush); DeleteObject(hpen); }

评论