// ex17Dlgcpp : implementation file
//
#include "stdafxh"
#include "ex17h"
#include "ex17Dlgh"
#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()
/////////////////////////////////////////////////////////////////////////////
// CEx17Dlg dialog
CEx17Dlg::CEx17Dlg(CWnd pParent /=NULL/)
: CDialog(CEx17Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEx17Dlg)
m_time1 = _T("");
m_time2 = _T("");
m_beep = FALSE;
m_min = 0;
m_sec = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEx17Dlg::DoDataExchange(CDataExchange pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx17Dlg)
DDX_Text(pDX, IDC_TIME1, m_time1);
DDX_Text(pDX, IDC_TIME2, m_time2);
DDX_Check(pDX, IDC_CHECK1, m_beep);
DDX_Text(pDX, IDC_EDIT1, m_min);
DDX_Text(pDX, IDC_EDIT2, m_sec);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEx17Dlg, CDialog)
//{{AFX_MSG_MAP(CEx17Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx17Dlg message handlers
BOOL CEx17Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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;
strAboutMenuLoadString(IDS_ABOUTBOX);
if (!strAboutMenuIsEmpty())
{
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
m_speed=1000;//设立初始定时器周期为1秒
m_pos=50;//滑块处于中间位置
m_normal=TRUE;
m_time=0;
CSliderCtrl pSlider=(CSliderCtrl )GetDlgItem(IDC_SLIDER1);
pSlider->SetRange(1,100);
pSlider->SetPos(m_pos);
CheckRadioButton(IDC_RADIO1,IDC_RADIO1,IDC_RADIO1);
OnRadio1();
return TRUE; // return TRUE unless you set the focus to a control
}
void CEx17Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAboutDoModal();
}
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 CEx17Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dcGetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rectWidth() - cxIcon + 1) / 2;
int y = (rectHeight() - cyIcon + 1) / 2;
// Draw the icon
dcDrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window
HCURSOR CEx17Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEx17Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(m_normal==TRUE)
{
m_time++;
m_time2Format("%d分%d秒",(int)(m_time/60),m_time-((int)(m_time/60))60);
UpdateData(FALSE);
}
else
{
if(m_time>=0)
{
m_time1Format("%d分%d秒",(int)(m_time/60),m_time-((int)(m_time/60))60);
m_time--;
UpdateData(FALSE);
}
else
{
if(m_beep==TRUE)//若蜂鸣器响复选框被选中
::Beep(1000,500);
KillTimer(m_nTimer);//倒讲时结束,停止当前定时器
GetDlgItem(IDC_CHECK1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}
}
CDialog::OnTimer(nIDEvent);
}
void CEx17Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CEx17Dlg::OnRadio1()
{
// TODO: Add your control notification handler code here
m_normal=FALSE;
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}
void CEx17Dlg::OnRadio2()
{
// TODO: Add your control notification handler code here
m_normal=TRUE;
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}
void CEx17Dlg::OnClear()
{
// TODO: Add your control notification handler code here
KillTimer(m_nTimer);
m_time=0;
GetDlgItem(IDC_CHECK1)->EnableWindow(TRUE);
UpdateData(FALSE);
}
void CEx17Dlg::OnStart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
GetDlgItem(IDC_CHECK1)->EnableWindow(FALSE);
m_nTimer=SetTimer(1,m_speed,NULL);
if(m_normal==FALSE)
{
m_time=m_min60+m_sec;
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}
}
1这是用windows api写的程序。所以要求是纯c的话就没有办法了
2其中定时用了两种方法。一种是用取消息。另一种是延时队列。这里只使用了取消息的方法。延时队列由于我机器上是vc60,CreateTimerQueue在本人机器上无法使用,需要新的sdk,所以没有加以验证,但取消息的方式是可行的。
3稍稍验证了下,基本满足要求。
#include "stdafxh"
#include "MFCFrame1h"
#include "MFCFrame1Doch"
#include "MFCFrame1Viewh"
#include "PointDialogh"
#include "mathh"
GLUquadricObj objCylinder = gluNewQuadric();
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View
IMPLEMENT_DYNCREATE(CMFCFrame1View, CView)
BEGIN_MESSAGE_MAP(CMFCFrame1View, CView)
//{{AFX_MSG_MAP(CMFCFrame1View)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_COMMAND(IDM_ZIXUAN, OnZixuan)
ON_WM_TIMER()
ON_COMMAND(IDM_ChangDirect, OnChangDirect)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View construction/destruction
CMFCFrame1View::CMFCFrame1View()
{
// TODO: add construction code here
this->m_GLPixelIndex = 0;
this->m_hGLContext = NULL;
Angle1=00;
Angle2=300;
Timer=0;
x=00;
z=00;
juli=400;
}
CMFCFrame1View::~CMFCFrame1View()
{
}
BOOL CMFCFrame1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
csstyle |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View drawing
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View printing
BOOL CMFCFrame1View::OnPreparePrinting(CPrintInfo pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMFCFrame1View::OnBeginPrinting(CDC /pDC/, CPrintInfo /pInfo/)
{
// TODO: add extra initialization before printing
}
void CMFCFrame1View::OnEndPrinting(CDC /pDC/, CPrintInfo /pInfo/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View diagnostics
#ifdef _DEBUG
void CMFCFrame1View::AssertValid() const
{
CView::AssertValid();
}
void CMFCFrame1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMFCFrame1Doc CMFCFrame1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCFrame1Doc)));
return (CMFCFrame1Doc)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View message handlers
BOOL CMFCFrame1View::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(this->m_GLPixelIndex==0)
{
this->m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
int CMFCFrame1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
HWND hWnd = this->GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(this->SetWindowPixelFormat(hDC)==FALSE)
{
return 0;
}
if(this->CreateViewGLContext(hDC)==FALSE)
{
return 0;
}
return 0;
}
BOOL CMFCFrame1View::CreateViewGLContext(HDC hDC)
{
this->m_hGLContext = wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{//创建失败
return FALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{//选为当前RC失败
return FALSE;
}
return TRUE;
}
void CMFCFrame1View::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext = NULL;
}
}
void CMFCFrame1View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei width,height;
GLdouble aspect;
width = cx;
height = cy;
if(cy==0)
{
aspect = (GLdouble)width;
}
else
{
aspect = (GLdouble)width/(GLdouble)height;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(400,aspect,50,10000);
}
void CMFCFrame1View::OnDraw(CDC pDC)
{
CMFCFrame1Doc pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CPaintDC dc(this);
glClearColor(10,10,10,10);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(00,00,100,00,00,00,00,10,00);
glRotatef(-900,10,00,00);/返回原坐标/
glTranslatef(-30,00,00);
SwapBuffers(dcm_pshdc);
glDrawBuffer (GL_BACK);
glFlush();
}
void CMFCFrame1View::OnZixuan()
{
// TODO: Add your command handler code here
Timer=1;
SetTimer(1,100,NULL);
}
void CMFCFrame1View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CPaintDC dc(this);
if (Timer==1)
{ Angle1=Angle1-1;
Angle2=Angle2-1;
glClearColor(10,10,10,10);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);//
glLoadIdentity();
gluLookAt(00,200,0000000001,00,00,00,00,10,00);
glPushMatrix();
glColor3f(07,07,07);
glTranslatef(00,00,11);
glRotatef(-900,10,00,00);
glutSolidCone(50,00,600,600); /底盘/
glPopMatrix();
glPushMatrix();
glColor3f(00,00,00);
glTranslatef(39,099,10); /刻度/
glRotatef(900,00,10,00);
gluCylinder(objCylinder, 005, 005, 08, 9999, 9);
glPopMatrix();
glPushMatrix();
glColor3f(00,00,00);
glTranslatef(-47,099,10); /刻度/
glRotatef(900,00,10,00);
gluCylinder(objCylinder,005, 005, 08, 9999, 9);
glPopMatrix();
glPushMatrix();
glColor3f(00,00,00);
glTranslatef(00,099,-29); /刻度/
glRotatef(1800,00,10,00);
gluCylinder(objCylinder,005, 005, 08, 9999, 9);
glPopMatrix();
glPushMatrix();
glColor3f(00,00,00);
glTranslatef(00,099,58); /刻度/
glRotatef(1800,00,10,00);
gluCylinder(objCylinder,005, 005,08, 9999, 9);
glPopMatrix();
glPushMatrix();
glColor3f(00,10,00);
glRotatef(450,00,10,00);
glTranslatef(-067,099,07); /时针/
glRotatef(Angle1/129600,00,10,00);
gluCylinder(objCylinder, 007, 002, 25, 9999, 9);
glPopMatrix();
glPushMatrix();
glColor3f(10,00,00);
glTranslatef(00,099,10); /分针/
glRotatef(Angle2/360,00,10,00);
gluCylinder(objCylinder, 005, 002, 35, 9999, 9);
glPopMatrix();
glPushMatrix();
glColor3f(00,00,05);
glTranslatef(00,099,10); /秒针/
glRotatef(Angle1,00,10,00);
gluCylinder(objCylinder, 007, 002, 45, 9999, 9);
glPopMatrix();
SwapBuffers(dcm_pshdc);
glDrawBuffer (GL_BACK);
glFlush();
}
else if(Timer==2)
{ glClearColor(10,10,10,10);
glClear(GL_COLOR_BUFFER_BIT);
if (juli>120)
{
glMatrixMode(GL_MODELVIEW);/建立了从世界坐标系到观察坐标系的转换矩阵/
glLoadIdentity();
gluLookAt(00,80,juli,00,00,00,00,10,00);
juli=juli-01;
glPushMatrix();
glColor3f(00,00,00);
glRotatef(-900,10,00,00);
glutWireCone(400,00,300,300); /画高度为0的圆锥/
glPopMatrix();
glPushMatrix();
glColor3f(10,00,10);
glLineWidth(40);
glTranslatef(40,10,00);
glutWireOctahedron(); /画八面体/
glLineWidth(10);
glPopMatrix();
glPushMatrix();
glColor3f(10,00,10);
glTranslatef(00,11,00);
glRotatef(Angle2,00,10,00);
gluCylinder(objCylinder, 10, 10, 100, 9999, 9); /画壶/
glPopMatrix();
}
else if(juli<=120)
{
Angle2=Angle2+001;
if (Angle2==3600)
Angle2=00;
glMatrixMode(GL_MODELVIEW);/建立了从世界坐标系到观察坐标系的转换矩阵/
glLoadIdentity();
x=120sin(Angle2);
z=120cos(Angle2);
gluLookAt(x,50,z,00,00,00,00,10,00);
glPushMatrix();
glColor3f(00,00,00);
glRotatef(-900,10,00,00);
glutWireCone(400,00,300,300); /画高度为0的圆锥/
glPopMatrix();
glPushMatrix();
glColor3f(10,00,10);
glLineWidth(40);
glTranslatef(40,10,00);
glutWireOctahedron(); /画八面体/
glLineWidth(10);
glPopMatrix();
glPushMatrix();
glColor3f(10,00,10);
glTranslatef(00,11,00);
gluCylinder(objCylinder, 10, 10, 100, 9999, 9);
glPopMatrix();
}
SwapBuffers(dcm_pshdc);
glDrawBuffer (GL_BACK);
glFlush();
}
CView::OnTimer(nIDEvent);
}
void CMFCFrame1View::OnChangDirect()
{
// TODO: Add your command handler code here
Timer=2;
SetTimer(1,100 ,NULL);
}
以上就是关于c++ 模拟时钟全部的内容,包括:c++ 模拟时钟、用C语言编写一个模拟windows自带的电子时钟程序、在VC 6.0用C或C++语言编写一个模拟图形时钟,等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)