看看帮同学画的时钟



原本是打算弄一个动画截图过来的,但是QQ的动画录制尺寸有点大,所以就这样呗!
C语言时钟源码!

#include<math.h>
#include<Windows.h>
#include<graphics.h>
#include<time.h>
#include<stdio.h>

#define PI     3.1415926536

void DrawNumTime(struct tm *Time);

typedef struct {
    int length;  //长
    int height;  //高
    int center_x;//圆心 X
    int center_y;//圆心 Y
    int center_r;//半径
    int hour_len;//时钟长度
    int min_len; //分钟长度
    int sec_len; //秒针长度
}INFO;

static INFO draw={500,700,250,220,200,180,130,95};

void main()
{
    initgraph(draw.length,draw.height);//创建画板
    int x,y,i;
    struct tm *Time;
    time_t ptm;

    HWND hWnd = GetHWnd();
    SetWindowText(hWnd, _T("简易时钟 - by 郭仲权"));

    setbkmode(TRANSPARENT);//设置文字背景透明

    IMAGE img;
    loadimage(&img,"img","img");//加载图片

    while(1){
        time(&ptm);
        Time=localtime(&ptm);//获取当前时间

        cleardevice();//清空画板
        BeginBatchDraw();//开始绘图

        setbkcolor(WHITE);//设置背景颜色为白色
        putimage(0,0,&img);//输出背景图片

        DrawNumTime(Time);//输出数字时间

        //setlinestyle(PS_SOLID,NULL,1);//设置秒针粗细

        setcolor(RED);//设置绘图颜色为红色
        setfillstyle(RED);//设置填充颜色为红色

        for(i=0;i<60;i++){//画分钟的点
            x=(int)(draw.center_x+(draw.center_r-5)*sin(i*2*PI/60));
            y=(int)(draw.center_y-(draw.center_r-5)*cos(i*2*PI/60));
            if(i%5!=0)
                fillcircle(x,y,2);    
        }

        setfillstyle(BLACK);//设置填充颜色为黑色
        setcolor(BLACK);//设置绘图颜色为黑色
        for(i=0;i<12;i++){//画时钟的点
            x=(int)(draw.center_x+(draw.center_r-5)*sin(i*2*PI/12));
            y=(int)(draw.center_y-(draw.center_r-5)*cos(i*2*PI/12));
            fillcircle(x,y,5);
        }

        if(Time->tm_hour-12>0)Time->tm_hour-=12;//判断是否为下午,下午就减去12小时

        setcolor(BLACK);//设置黑色
        //计算时钟坐标
        x=(int)(draw.center_x+draw.sec_len*sin((((Time->tm_hour)*60*60)+(Time->tm_min*60)+Time->tm_sec)*2*PI/43200));
        y=(int)(draw.center_y-draw.sec_len*cos((((Time->tm_hour)*60*60)+(Time->tm_min*60)+Time->tm_sec)*2*PI/43200));
        setlinestyle(PS_SOLID,NULL,5);//设置宽的为5的线
        line(draw.center_x,draw.center_y,x,y);//画时钟

        setcolor(GREEN);
        x=(int)(draw.center_x+draw.min_len*sin(((Time->tm_min*60)+Time->tm_sec)*2*PI/3600));
        y=(int)(draw.center_y-draw.min_len*cos(((Time->tm_min*60)+Time->tm_sec)*2*PI/3600));
        setlinestyle(PS_SOLID,NULL,3);//设置宽的为3的线
        line(draw.center_x,draw.center_y,x,y);//画时钟

        setcolor(RED);
        x=(int)(draw.center_x+draw.hour_len*sin((Time->tm_sec)*2*PI/60));
        y=(int)(draw.center_y-draw.hour_len*cos((Time->tm_sec)*2*PI/60));
        setlinestyle(PS_SOLID,NULL,1);//设置宽的为3的线
        line(draw.center_x,draw.center_y,x,y);//画时钟

        setcolor(RED);
        fillcircle(draw.center_x,draw.center_y,6);//画钟中间的圆

        EndBatchDraw();//将图像输出到窗口
        Sleep(1000);//程序延迟一秒
    }
}
void DrawNumTime(struct tm *Time){
    char str[30];
    setcolor(BLACK);
    setfont(50, 0, _T("楷体"));
    strftime(str,30,"%H:%M:%S",Time);
    outtextxy(draw.length/2-100,draw.center_y+draw.center_r+60,str);
    setfont(35, 0, _T("微软雅黑"));
    strftime((char *)str,30,"%Y-%m-%d",Time);
    outtextxy(draw.length/2-75,draw.center_y+draw.center_r+60+60,str);
}

5条评论在“看看帮同学画的时钟”

写下你最简单的想法