文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>VC 制作标准圆盘时钟实例

VC 制作标准圆盘时钟实例

时间:2010-10-22  来源:耿郎

第一步:新建一个MFC单文档的应用程序,工程名称为Clock.

第二步:在生成的CClockView类添加OnInitialUpdate()虚函数;并于函数内设置定时器;具体代码如下:

void CClockView::OnInitialUpdate()      //添加虚函数
{
 CView::OnInitialUpdate();
 
 // TODO: Add your specialized code here and/or call the base class
 SetTimer( 1, 1000, NULL );
}


第三步:设置定时器响应函数。为CClockView类添加WM_TIMER消息及OnTime()的消息响应函数,函数体代码为:

void CClockView::OnTimer(UINT nIDEvent)    //添加定时器响应消息
{
 // TODO: Add your message handler code here and/or call default

 Invalidate(TRUE);  //使窗口无效
  UpdateWindow();    //使窗口立即重绘,刷新窗口
 CView::OnTimer(nIDEvent);
}

 

第四步:在OnDraw()函数中添加如下代码:

RECT Rect; 
GetClientRect( &Rect );   //得到客户区矩形
        
// 得到圆心坐标
int nCenterX = Rect.right / 2;
int nCenterY = Rect.bottom / 2;
        
// 获取当前时间
CTime Time = CTime::GetCurrentTime();
        
CString strDigits;
int i, x, y;
CSize size;     
pWnd->GetMenuID() =ID_TYPE_ANALOG;
// 设置圆盘钟的外圈为黄色
CPen Pen( PS_SOLID, 5, RGB( 255, 255, 0 ) );            
CPen *pOldPen = pDC->SelectObject( &Pen );               
pDC->Ellipse(nCenterX+5-nCenterY,5,nCenterX+nCenterY-5,Rect.bottom - 5);     //画圆
double Radians;                 
// 时间数字为红色
pDC->SetTextColor( RGB( 255, 0, 0 ) );                       
for(i=1; i<=12; i++ ){
                                
        // 格式化时钟对象
        strDigits.Format( "%d", i );
                                
        size =pDC->GetTextExtent( strDigits,     //获得所选字体中指定字符串的高度和宽度
        strDigits.GetLength() );
                                
        Radians = (double) i * 6.28 / 12.0;    //表示角度  6.28=2*PAI                               
        x=nCenterX-( size.cx / 2 )+(int)((double)(nCenterY-5)*sin(Radians)  );
        y=nCenterY-( size.cy / 2 )-(int)((double)(nCenterY-5)*cos(Radians)  );          
        pDC->TextOut( x, y, strDigits );                             
}
                        
        // Calculate the radians for the hour hand.
Radians =(double) Time.GetHour() +
        (double) Time.GetMinute() / 60.0 +
        (double) Time.GetSecond() / 3600.0;
Radians *= 6.28 / 12.0;
                        
// 时针为绿色
CPen HourPen( PS_SOLID, 5, RGB( 0, 255, 0 ) );
                        
// Select the newly-created CPen object into the DC.
pDC->SelectObject( &HourPen );
                        
// Move to the center of the clock, then draw the hour hand line.
pDC->MoveTo( nCenterX, nCenterY );
pDC->LineTo(
nCenterX + (int) ( (double) ( nCenterY / 3 ) *sin( Radians ) ),
nCenterY - (int) ( (double) ( nCenterY / 3 ) *cos( Radians ) ) );
// Calculate the radians for the minute hand.
Radians = (double) Time.GetMinute() +
         (double) Time.GetSecond() / 60.0;
        Radians *= 6.28 / 60.0;
                        
// 分针为蓝色
CPen MinutePen( PS_SOLID, 3, RGB( 0, 0, 255 ) );
                        
// Select the newly-created CPen object into the DC.
pDC->SelectObject( &MinutePen );
// Move the to center of the clock, then draw the minute hand line.
pDC->MoveTo( nCenterX, nCenterY );
pDC->LineTo(
        nCenterX + (int) ( (double) (
        ( nCenterY * 2 ) / 3 ) * sin( Radians ) ),
        nCenterY - (int) ( (double) (
        ( nCenterY * 2 ) / 3 ) * cos( Radians ) ) );
                        
// Calculate the radians for the second hand.
Radians = (double) Time.GetSecond();
Radians *= 6.28 / 60.0;
                        
// 秒针为黑色
CPen SecondPen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
                        
// Select the newly-created CPen object into the DC.
pDC->SelectObject( &SecondPen );
                        
// Move the to center of the clock, then draw the second hand line.
pDC->MoveTo( nCenterX, nCenterY );
pDC->LineTo(
        nCenterX + (int) ( (double) (
        ( nCenterY * 4 ) / 5 ) * sin( Radians ) ),
        nCenterY - (int) ( (double) (
        ( nCenterY * 4 ) / 5 ) * cos( Radians ) ) );
                        
        // Select the old CPen object back into the DC.
        pDC->SelectObject( pOldPen );


 第五步:运行即可得到如下界面:

 

 

 

当框拉的较大时,线条的锯齿较明显,不知有什么好方法 可 消除  ?

 

 

 

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载