C++控制台时钟

c++控制台时钟。可以写几个头文件。这里没有写成头文件的形式。
c++11编译会出现点问题,建议用 vc6.0 。左边不太完整。自己可以改一下

#include<iostream>
#include<windows.h>
#include<ctime>
#include<cmath>
#include<conio.h>
#include<stdlib.h>
using namespace std;
# define PI 3.1415926535898 
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow();
HBRUSH hBrush;
HWND hwnd = GetConsoleWindow();
HDC hdch = GetDC(hwnd);
HDC hdcm = GetDC(hwnd);
HDC hdcs = GetDC(hwnd);
HDC hdc1 = GetDC(hwnd);
HDC hdc2 = GetDC(hwnd);
HDC hdc3 = GetDC(hwnd);
HDC hdc = GetDC(hwnd);
HDC hdce = GetDC(hwnd);//句柄
typedef struct
{
    double x; double y;
} point;
class Clock
{
public:
    void Drawingline(HDC hdc, point p1, point p2);
    point pp;//初始化用的
    void set_clock()
    {
        HFONT font; 
        HBRUSH hBrush1;
        font = CreateFont(15, 7, 0, 0, FW_BOLD, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, TEXT("微软雅黑")); //设置字体样式
        hBrush1 = CreateSolidBrush(RGB(0, 255, 0));//设置画刷颜色
        SelectObject(hdc, font);
        SelectObject(hdce, hBrush1);
        SetBkColor(hdc, RGB(0, 255, 0)); //改变文本框背景色
        SetTextColor(hdc, RGB(0, 0, 255)); //改变文本框中字体颜色
        Ellipse(hdce, 110, 10, 490, 390);//圆或者椭圆
        char *str1 = "12";
        char *str2 = "3";
        char *str3 = "6";
        char *str4 = "9";
        char *str5 = "1";
        char *str6 = "2";
        char *str7 = "4";
        char *str8 = "5";
        char *str9 = "7";
        char *str10 = "8";
        char *str11 = "10";
        char *str12 = "12";
        TextOutA(hdc, 295, 10, str1, sizeof(str1));//12
        TextOutA(hdc, 112, 192, str4, sizeof(str4));//9
        TextOutA(hdc, 475, 192, str2, sizeof(str2)-2);//3
        TextOutA(hdc, 295, 378, str3, sizeof(str3)-1);//6
        TextOutA(hdc, 385 ,36, str5, sizeof(str5)-2);//1
        TextOutA(hdc, 452, 102, str6, sizeof(str6)-2);//2
        TextOutA(hdc, 450, 285, str7, sizeof(str7)-2);//4
        TextOutA(hdc, 385, 350, str8, sizeof(str8)-2);//5
        DeleteObject(font);
    }
};
void Clock::Drawingline(HDC hdc, point p1, point p2)
{
    MoveToEx(hdc, p1.x, p1.y, NULL);//起点
    LineTo(hdc, p2.x, p2.y);//终点
}
class Hour :public Clock
{
public:
    point move(int i, point p2)
    {
        p2.x = 130 * cos(i * 30 * PI / 180 - PI / 2) + 300;//p1(400,400)
        p2.y = 130 * sin(i * 30 * PI / 180 - PI / 2) + 200;
        return p2;
    }
    HPEN setpenh()
    {
        HPEN hPenh;
        HDC hdch = GetDC(hwnd);
        hPenh = CreatePen(PS_INSIDEFRAME, 5, RGB(255, 0, 0)); //设置画笔颜色
        return hPenh;
    }
    HPEN hfugai()
    {
        HPEN hPenhfugai;
        hPenhfugai = CreatePen(PS_INSIDEFRAME, 5, RGB(0, 255, 0));
        return hPenhfugai;
    }
    int get_hour()
    {
        SYSTEMTIME sysTime;
        ZeroMemory(&sysTime, sizeof(sysTime));
        GetLocalTime(&sysTime);
        return datetimeh=sysTime.wHour % 12;//例如k=h.get_hour_num()
    }
private:
    int datetimeh;
};
class Minute :public Clock
{
public:
    point move(int i, point p2)
    {
        p2.x = 150 * cos(i*6*PI / 180 - PI / 2) + 300;
        p2.y = 150 * sin(i*6*PI / 180 - PI / 2) + 200;
        return p2;
    }
    HPEN setpenm()
    {
        HPEN hPenm; HDC hdcm = GetDC(hwnd);
        hPenm = CreatePen(PS_INSIDEFRAME, 2, RGB(255, 0, 0)); //设置画笔颜色
        return hPenm;
    }
    HPEN fugai()
    {
        HPEN hPenmfugai;
        hPenmfugai = CreatePen(PS_INSIDEFRAME, 2, RGB(0, 255, 0));
        return hPenmfugai;
    }
    int get_minute()
    {
        SYSTEMTIME sysTime;
        ZeroMemory(&sysTime, sizeof(sysTime));
        GetLocalTime(&sysTime);
        return datetimem = sysTime.wMinute;
    }
private:
    int datetimem;
};
class Second :public Clock
{
public:
    point move(int i, point p2)
    {
        p2.x = 170 * cos(i * 6 * PI / 180 - PI / 2) + 300;
        p2.y = 170 * sin(i * 6 * PI / 180 - PI / 2) + 200;
        return p2;
    }
    HPEN  setpens()
    {
        HPEN hPens; HDC hdcs = GetDC(hwnd);
        hPens= CreatePen(PS_INSIDEFRAME, 1, RGB(255, 0, 0)); //设置画笔颜色
        return hPens;
    }
    HPEN fugai()
    {
        HPEN hPensfugai;
        hPensfugai = CreatePen(PS_INSIDEFRAME, 1, RGB(0, 255, 0));
        return hPensfugai;
    }
    int get_seconds()
    {
        SYSTEMTIME sysTime;
        ZeroMemory(&sysTime, sizeof(sysTime));
        GetLocalTime(&sysTime);
        return datetimes = sysTime.wSecond;
    }
private:
    int datetimes;
};
int main()
{
    system("color Ff");//调制控制台颜色
    Hour h;
    Minute m;
    Second s;
    SelectObject(hdch, h.setpenh());
    SelectObject(hdcs, s.setpens());
    SelectObject(hdcm, m.setpenm());
    SelectObject(hdc1, s.fugai());
    SelectObject(hdc2, m.fugai());
    SelectObject(hdc3, h.hfugai());
    Clock c;
    c.set_clock();
    point p1, p2;
    p1.x = 300; p1.y = 200;//初始点
    p2.x = 300; p2.y = 100;
    point p,pp,ppp;
    SYSTEMTIME sysTime0;//初始化时针秒针分针
    ZeroMemory(&sysTime0, sizeof(sysTime0));
    GetLocalTime(&sysTime0);
    int ss = sysTime0.wSecond;
    int mm = sysTime0.wMinute;
    int hh = sysTime0.wHour % 12;
    ppp = h.move(hh, p2);
    h.Drawingline(hdch, p1, ppp);
    pp = m.move(mm, p2);
    m.Drawingline(hdcm, p1, pp);    
    p = s.move(ss, p2);
    s.Drawingline(hdcs, p1, p);
    while (!_kbhit())
    {
        if (ss != s.get_seconds())
        {
            s.Drawingline(hdc1, p1, p);//清除前面的秒针
            ss = s.get_seconds();
            p = s.move(ss, p2);
            s.Drawingline(hdcs, p1, p);
        }
        Sleep(1000);
        if (mm == m.get_minute())   m.Drawingline(hdcm, p1, pp);
        if (mm != m.get_minute())
        {
            m.Drawingline(hdc2, p1, pp);
            mm = m.get_minute();
            pp = m.move(mm, p2);
            m.Drawingline(hdcm, p1, pp);
        }
        if (hh == h.get_hour()) h.Drawingline(hdch, p1, ppp);
        if (hh != h.get_hour())
        {
            h.Drawingline(hdc3, p1, ppp);
            hh = h.get_hour();
            ppp = h.move(hh, p2);
            h.Drawingline(hdch, p1, ppp);
        }
    }
    c.Drawingline(hdch, p1, p2);
    p2.x = 100; p2.y = 200;
    Sleep(1000);
    c.Drawingline(hdcs, p1, p2);
    DeleteObject(hBrush);
    ReleaseDC(hwnd, hdc);
    getchar();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013077144/article/details/51360283