TLines.cpp
上传用户:wenliang_x
上传日期:2013-05-26
资源大小:19k
文件大小:2k
- #include "TLines.h"
- static const int vRange = 12; //速度变化范围
- static const int scrwth=GetSystemMetrics(SM_CXFULLSCREEN); //窗口宽度
- static const int scrhgt=GetSystemMetrics(SM_CYFULLSCREEN)+40;//窗口高度
- void TLines::Draw(HWND hwnd) //画线,整个程序写的最关键函数
- {
- int i,j; //定义两个计数器
- HDC hdc; //Windows系统类型,窗口的设备环境
- HPEN oldpen,newpen,ReverseNewPen; //Windows系统类型,画笔
- //获取该窗口的设备环境
- hdc=GetDC(hwnd);
- newpen=CreatePen(PS_SOLID,1,RGB(color.nRed,color.nGreen,color.nBlue));
- oldpen = (HPEN)SelectObject(hdc,newpen);
- //先保存原来的点,画出后NumOfPolygan组线时才擦画
- for (i=0;i<NumOfPoint;i++) {
- for (j=NumOfPolygan-1;j>0;j--) {
- pt[i].Oldx[j]=pt[i].Oldx[j-1];
- pt[i].Oldy[j]=pt[i].Oldy[j-1];
- }
- pt[i].Oldx[0]=pt[i].x;
- pt[i].Oldy[0]=pt[i].y;
- }
- //是否跑到屏幕外,反弹
- for (i=0;i<NumOfPoint;i++) {
- if (pt[i].x>=scrwth)
- { pt[i].vx=-rand()*vRange/RAND_MAX-1; ChangeColor(); }
- else if (pt[i].x<=0)
- { pt[i].vx=rand()*vRange/RAND_MAX+1; ChangeColor(); }
- if (pt[i].y>=scrhgt)
- { pt[i].vy=-rand()*vRange/RAND_MAX-1; ChangeColor(); }
- else if (pt[i].y<=0)
- { pt[i].vy=rand()*vRange/RAND_MAX+1; ChangeColor(); }
-
- pt[i].x=pt[i].x+pt[i].vx;
- pt[i].y=pt[i].y+pt[i].vy;
- }
- //画线
- MoveToEx(hdc,pt[0].x,pt[0].y,NULL);
- for (j=0;j<NumOfPoint-1;j++)
- LineTo(hdc,pt[j+1].x,pt[j+1].y);
- LineTo(hdc,pt[0].x,pt[0].y);
- Sleep(7); //延迟0.007秒,控制速度!!!!
- //擦去前NumOfPolygan的那条线
- ReverseNewPen=CreatePen(PS_SOLID,1,RGB(0,0,0));
- SelectObject(hdc,ReverseNewPen);
- MoveToEx(hdc,pt[0].Oldx[NumOfPolygan-1],pt[0].Oldy[NumOfPolygan-1],NULL);
- for (j=0;j<NumOfPoint-1;j++)
- LineTo(hdc,pt[j+1].Oldx[NumOfPolygan-1],pt[j+1].Oldy[NumOfPolygan-1]);
- LineTo(hdc,pt[0].Oldx[NumOfPolygan-1],pt[0].Oldy[NumOfPolygan-1]);
- SelectObject(hdc,oldpen);
- DeleteObject(newpen);
- DeleteObject(ReverseNewPen);
- ReleaseDC(hwnd,hdc); //释放窗口的设备环境
- }
- void TLines::ChangeColor() //改变颜色
- {
- if (rand()*10/RAND_MAX == 5) { //平均顶点撞击墙壁10次改变一次颜色
- color.nRed = rand()*255/RAND_MAX;
- color.nGreen = rand()*255/RAND_MAX;
- color.nBlue = rand()*255/RAND_MAX;
- }
- }