//Win32 Application: #include<windows.h> #include<math.h> #include<stdlib.h> LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM); int cxClient,cyClient; HPEN hpen; #define TWO_PI (2*3.1415926) WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpcmdLine,int nShowCmd) { HWND hwnd; MSG msg; WNDCLASS wc; TCHAR szAppname[] = TEXT("随机圆"); HBRUSH hbrush; LOGPEN logpen; PAINTSTRUCT ps; HDC hdc; double radius,i; POINT pt; pt.x = 1; pt.y =2; 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(msg.message==WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { hdc = GetDC(hwnd); SetBkMode(hdc,OPAQUE); //不透明,用背景色填充。 logpen.lopnColor = RGB(rand()%256,rand()%256,rand()%256); logpen.lopnStyle = PS_DASH; logpen.lopnWidth = pt; // lopnWidth是个POINT结构体。 hpen = CreatePenIndirect(&logpen); SelectObject(hdc,hpen); SetViewportOrgEx(hdc,rand()%cxClient,rand()%cyClient,NULL); radius = _hypot(cxClient/2,cyClient/2); for(i=0.0; i<TWO_PI; i+=TWO_PI/360) { MoveToEx(hdc,0,0,NULL); LineTo(hdc,(int)(cos(i)*radius),(int)(sin(i)*radius)); } logpen.lopnColor = RGB(rand()%256,rand()%256,rand()%256); hpen = CreatePenIndirect(&logpen); DeleteObject(SelectObject(hdc,hpen)); for(i=0.0; i<TWO_PI; i+=TWO_PI/360) { MoveToEx(hdc,0,0,NULL); LineTo(hdc,(int)(cos(i)*radius),(int)(sin(i)*radius)); } ReleaseDC(hwnd,hdc); } } 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: DeleteObject(hpen); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wparam,lparam); }

评论