| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5684 人关注过本帖
标题:求助模拟时钟转动程序用C++编写
只看楼主 加入收藏
xiaosa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-8-31
收藏
 问题点数:0 回复次数:7 
求助模拟时钟转动程序用C++编写

要求:
能模拟机械钟表行走,还要准确的利用数字显示日期和时间,在屏幕上显示一个活动的时钟,
按任意键时程序退出
星期六就需要!!请帮帮忙~~谢谢大家!
搜索更多相关主题的帖子: 时钟 钟表 模拟 
2007-08-31 23:51
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
用JAVA写吧,
简单一点.

2007-09-01 09:47
天使梦魔
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:37
帖 子:564
专家分:2754
注 册:2007-8-29
收藏
得分:0 
一看就是毕业作业的
2007-09-01 10:02
xiaosa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-8-31
收藏
得分:0 

不是毕业设计,是课程设计,才刚大二呢,就 要用C++啊.

2007-09-01 11:40
百年不亮
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:789
专家分:0
注 册:2006-4-14
收藏
得分:0 
赶紧学图形编程去吧
2007-09-01 11:59
xiaosa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-8-31
收藏
得分:0 
用MFC编写,会自动生成很多代码,这里为主要部分,还要绘图的
    此源代码为主要函数部分:
    // clock2Dlg.cpp : implementation file
    #include "stdafx.h"
    #include "clock2.h"
    #include "clock2Dlg.h"
    #include "math.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About
    class CAboutDlg : public CDialog
    {
    public:
        CAboutDlg();
    // Dialog Data
        //{{AFX_DATA(CAboutDlg)
        enum { IDD = IDD_ABOUTBOX };
        //}}AFX_DATA
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CAboutDlg)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //}}AFX_VIRTUAL
    // Implementation
    protected:
        //{{AFX_MSG(CAboutDlg)
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
    };
    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
        //{{AFX_DATA_INIT(CAboutDlg)
        //}}AFX_DATA_INIT
    }
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CAboutDlg)
        //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
        //{{AFX_MSG_MAP(CAboutDlg)
            // No message handlers
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // CClock2Dlg dialog
    CClock2Dlg::CClock2Dlg(CWnd* pParent /*=NULL*/)
        : CDialog(CClock2Dlg::IDD, pParent)
    {
        //{{AFX_DATA_INIT(CClock2Dlg)
            // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    void CClock2Dlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CClock2Dlg)
            // NOTE: the ClassWizard will add DDX and DDV calls here
        //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CClock2Dlg, CDialog)
        //{{AFX_MSG_MAP(CClock2Dlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_WM_TIMER()
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // CClock2Dlg message handlers
    BOOL CClock2Dlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
        //定时器:
        SetTimer(1,1000,NULL);
        // Add "About..." menu item to system menu.
        // IDM_ABOUTBOX must be in the system command range.
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
        ASSERT(IDM_ABOUTBOX < 0xF000);
        CMenu* pSysMenu = GetSystemMenu(FALSE);
        if (pSysMenu != NULL)
        {
            CString strAboutMenu;
            strAboutMenu.LoadString(IDS_ABOUTBOX);
            if (!strAboutMenu.IsEmpty())
            {
                pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
            }
        }
        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);            // Set big icon
        SetIcon(m_hIcon, FALSE);        // Set small icon
        // TODO: Add extra initialization here
        return TRUE;  // return TRUE  unless you set the focus to a control
    }
    void CClock2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
        if ((nID & 0xFFF0) == IDM_ABOUTBOX)
        {
            CAboutDlg dlgAbout;
            dlgAbout.DoModal();
        }
        else
        {
            CDialog::OnSysCommand(nID, lParam);
        }
    }
    // If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.
    void CClock2Dlg::OnPaint()
    {
        if (IsIconic())
        {
            CPaintDC dc(this); // device context for painting
            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;
            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
        }
        else
        {
            CDialog::OnPaint();
        }
        //下面输入时钟程序代码:
        CDC *pDC=this->GetDC();
        CRect rc;
        GetClientRect(&rc);//
        int xStart=rc.right/2;                                    //计算中心点
        int yStart=rc.bottom/2;
        CTime time=CTime::GetCurrentTime();                    //得到当前的时间
        CString strDigits;
        int i,x,y;
        CSize size;
        CPen Pen(PS_SOLID,5,RGB(0,0,0));
        CPen *pOldPen=pDC->SelectObject(&Pen);
        pDC->Ellipse(5,5,rc.right-5,rc.bottom-5);                   //画圆盘
        double Radians;                                       //定义角度
        pDC->SetTextColor(RGB(0,0,0));                        //开始写入数字1-12
        for(i=1;i<=12;i++)
        {
            strDigits.Format("%d",i);
            size=pDC->GetTextExtent(strDigits,strDigits.GetLength());
            Radians=(double)i*6.28/12.0;
            x=xStart-(size.cx/2)+(int)((double)(xStart-20)*sin(Radians));//数字水平位置
            y=yStart-(size.cy/2)-(int)((double)(yStart-20)*cos(Radians));//数字垂直位置
            pDC->TextOut(x,y,strDigits);
        }
        //开始绘制时针
        Radians=(double)time.GetHour()+(double)time.GetMinute()/60.00+(double)time.GetSecond()/3600.00;
        Radians*=6.28/12.0;
        CPen HourPen(PS_SOLID,5,RGB(0,0,0));                  //定义时针线宽
        pDC->SelectObject(HourPen);
        pDC->MoveTo(xStart,yStart);
        pDC->LineTo(xStart+(int)((double)(xStart/2)*sin(Radians)),yStart-(int)((double) (yStart/2)*cos(Radians)));
        //开始绘制分针
        Radians=(double)time.GetMinute()+(double)time.GetSecond()/60.00;
        Radians*=6.28/60.0;
        CPen MinutePen(PS_SOLID,3,RGB(0,0,0));      //定义分针线宽
        pDC->SelectObject(MinutePen);
        pDC->MoveTo(xStart,yStart);
        pDC->LineTo(xStart+(int)((double)(xStart*2/3)*sin(Radians)),yStart-(int)((double)              (yStart*2/3)*cos(Radians)));
        //开始绘制秒针
        Radians=(double)time.GetSecond();
        Radians*=6.28/60.0;
        CPen SecondPen(PS_SOLID,1,RGB(0,0,0));     //定义秒针线宽
        pDC->SelectObject(SecondPen);
        pDC->MoveTo(xStart,yStart);
        pDC->LineTo(xStart+(int)((double)(xStart*4/5)*sin(Radians)),yStart-(int)((double)(yStart*4/5)*cos(Radians)));
        pDC->SelectObject(pOldPen);
                                                 //时钟代码结束
    }
    // The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CClock2Dlg::OnQueryDragIcon()
    {
        return (HCURSOR) m_hIcon;
    }
    //有关定时器:
    void CClock2Dlg::OnTimer(UINT nIDEvent)
    {
        InvalidateRect(NULL,true);
        UpdateWindow();
        CDialog::OnTimer(nIDEvent);
    }
2008-01-11 19:16
wqhai123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-4-20
收藏
得分:0 
让时钟的指针走起来,获取系统时间,设立坐标系,秒针没隔一秒重画一次,分针每隔60秒重画一次,
2008-01-14 09:04
honey00
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2011-4-1
收藏
得分:0 
不知道这个程序里面是哪段代码让时钟走起来的?
2011-04-01 14:31
快速回复:求助模拟时钟转动程序用C++编写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089428 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved