本人想自己做个小的时钟,请教一下大家!

解决方案 »

  1.   

    // CZJJ_ColockCtrl::OnDraw - Drawing functionvoid CZJJ_ColockCtrl::OnDraw(
    CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    // TODO: Replace the following code with your own drawing code. pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); CPen pen(PS_SOLID,6,RGB(0,122,122));
    pdc->SelectObject(&pen);
    pdc->Rectangle(rcBounds); //显示Colock中的3,6,9,12
    pdc->TextOut(rcBounds.right/2-8,rcBounds.top+rcBounds.bottom/12,"12");
    pdc->TextOut(rcBounds.right/2-4,rcBounds.bottom-rcBounds.bottom/12-17,"6");
    pdc->TextOut(rcBounds.left+rcBounds.right/12,rcBounds.bottom/2-4,"9");
    pdc->TextOut(rcBounds.right-rcBounds.right/12-8,rcBounds.bottom/2-4,"3");
      
    CTime time = CTime::GetCurrentTime();
    int nTime_s = time.GetSecond();
    int nTime_m = time.GetMinute();
    int nTime_h = time.GetHour();

    int nLength_S,nLength_M,nLength_H;
    if(rcBounds.right < rcBounds.bottom)
    {
    nLength_S = rcBounds.right/2 - rcBounds.right/7;
    nLength_M = rcBounds.right/2 - rcBounds.right/6;
    nLength_H = rcBounds.right/2 - rcBounds.right/5;
    }
    else
    {
    nLength_S = rcBounds.bottom/2 - rcBounds.bottom/7;
    nLength_M = rcBounds.bottom/2 - rcBounds.bottom/6;
    nLength_H = rcBounds.bottom/2 - rcBounds.bottom/5;
    } CPoint ptOrigin,ptEndS,ptEndM,ptEndH;
    //Origin Point
    ptOrigin.x = rcBounds.right/2;
    ptOrigin.y = rcBounds.bottom/2; CPen pen4(PS_SOLID,4,RGB(255,200,40));
    pdc->SelectObject(&pen4);
    pdc->Rectangle(rcBounds.right/2-1,rcBounds.bottom/2+1,rcBounds.right/2+1,rcBounds.bottom/2-1);  //Second
    ptEndS.x = (long)(nLength_S*cos(((nTime_s-15)*360/60)*PI/180) + ptOrigin.x);
    ptEndS.y = (long)(nLength_S*sin(((nTime_s-15)*360/60)*PI/180) + ptOrigin.y);
    //Minute
    ptEndM.x = (long)(nLength_M*cos(((nTime_m-15)*360/60)*PI/180) + ptOrigin.x);
    ptEndM.y = (long)(nLength_M*sin(((nTime_m-15)*360/60)*PI/180) + ptOrigin.y);
    //Hour
    if(nTime_h>=12)
    {
    ptEndH.x = (long)(nLength_H*cos(( ((nTime_h-12)*5-15+ nTime_m/15 )*360/60)*PI/180) + ptOrigin.x);
    ptEndH.y = (long)(nLength_H*sin(( ((nTime_h-12)*5-15+ nTime_m/15 )*360/60)*PI/180) + ptOrigin.y);
    }
    else
    {
    ptEndH.x = (long)(nLength_H*cos(( (nTime_h*5-15+ nTime_m/15 )*360/60)*PI/180) + ptOrigin.x);
    ptEndH.y = (long)(nLength_H*sin(( (nTime_h*5-15+ nTime_m/15 )*360/60)*PI/180) + ptOrigin.y);
    }
    //Draw 
    CPen pen1(PS_SOLID,4,RGB(0,0,255));
    pdc->SelectObject(&pen1);
    pdc->MoveTo(ptOrigin);
    pdc->LineTo(ptEndH); CPen pen2(PS_SOLID,2,RGB(255,0,255));
    pdc->SelectObject(&pen2);
    pdc->MoveTo(ptOrigin);
    pdc->LineTo(ptEndM); CPen pen3(PS_SOLID,1,RGB(255,0,0));
    pdc->SelectObject(&pen3);
    pdc->MoveTo(ptOrigin);
    pdc->LineTo(ptEndS);}
    //有点乱,以前写的,不知道能否有帮助