vc++6.0中的一个单文档程序, 在view类中的OnCreate中加入 SetTimer(1,1000,NULL); 添加WM_TIMER消息响应函数 加入 Invalidate();//重绘窗口 View类的OnDraw()加入代码如下: //CWindowDC dc(/*GetParent()*/GetDesktopWindow()); //CWindowDC *pDC=&dc; CPen pen(PS_SOLID,1,RGB(255,0,0)); CPen *pOldPen=(CPen*)pDC -> SelectObject(&pen); static const double pi=3.1415926535; //得到客户区的矩形 CRect rect; GetClientRect(&rect); //得到表盘中央位置,即圆心 int x0 = rect.Width() / 2; int y0 = rect.Height() / 2; //得到半径 int r = (x0 > y0 ? y0 : x0); //将要在循环中重复的计算提取出来,减少时间复杂度 //在看代码时请将下列算式代入比较容易理解 double rate = (double)(2.0 * pi / 60.0); double halfPi=pi/2; int zoomR=r-15; //绘制表盘 //zoomR及对zoomR进行加或减操作主要是用来控制距离圆心的距离 pDC->SetBkMode(TRANSPARENT); for(int mark=1;mark<=60;mark++) { int positionX = (int)(zoomR * sin(rate * mark)+ x0); int positionY = (int)(-zoomR * sin(rate * mark + halfPi)+ y0); if(mark % 5 ==0){ char number[2]; itoa(mark/5,number,10); //绘制数值 pDC -> TextOut(positionX-5,positionY-5,number); }else{ //绘制刻度 //pDC -> SetPixel(positionX,positionY,RGB(255,0,0)); pDC -> Ellipse(positionX-2,positionY-2,positionX+2,positionY+2); } } CTime time = CTime::GetCurrentTime(); int hour = time.GetHour(); int minute = time.GetMinute(); int second = time.GetSecond(); //绘制秒数指针 CPen secondPen(PS_SOLID,10,RGB(255,0,0)); pDC -> SelectObject(secondPen); pDC -> MoveTo(CPoint(x0,y0)); int secondX = (int)((zoomR-15) * sin(rate * second)+ x0); int secondY = (int)(-(zoomR-15) * sin(rate * second + halfPi)+ y0); pDC -> LineTo(CPoint(secondX,secondY));
//绘制分钟数指针 CPen minutePen(PS_SOLID,10,RGB(0,255,0)); pDC -> SelectObject(minutePen); pDC -> MoveTo(CPoint(x0,y0)); int minuteX = (int)((zoomR-70) * sin(rate * minute)+ x0); int minuteY = (int)(-(zoomR-70) * sin(rate * minute + halfPi)+ y0); pDC -> LineTo(CPoint(minuteX,minuteY)); //绘制小时数指针*/ double hourRate=(double)(2.0*pi/12.0); if(hour > 12) { hour -= 12; } CPen hourPen(PS_SOLID,10,RGB(0,0,255)); pDC -> SelectObject(hourPen); pDC -> MoveTo(CPoint(x0,y0)); int hourX = (int)((zoomR-140) * sin(hourRate * hour )+ x0); int hourY = (int)(-(zoomR-140) * sin(hourRate * hour + halfPi)+ y0); pDC -> LineTo(CPoint(hourX,hourY)); 程序截图如下: 然后我想对这个程序进行修改,使它除了表盘上的点和数字,以及指针外,其他都透明; 我在View类中的OnCreate加入: SetClassLong(this -> m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(NULL_BRUSH)); 结果发现客户区是透明了没错,但是表盘上的指针重绘之后,之前绘制的指针并没有消失。 有没有什么好的解决办法? |