在VC 6.0用C或C++语言编写一个模拟图形时钟,

需要在vc6.0编译通过~~~~~~~~~~~~~~…^w^~~... 需要在vc6.0编译通过~~~~~~~~~~~~~~…^w^~~ 展开
 我来答
百度网友f3b3e02
2013-04-08 · TA获得超过2156个赞
知道小有建树答主
回答量:821
采纳率:90%
帮助的人:583万
展开全部
#include "stdafx.h"
#include "MFCFrame1.h"

#include "MFCFrame1Doc.h"
#include "MFCFrame1View.h"
#include "PointDialog.h"
#include "math.h"
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=0.0;
Angle2=30.0;
Timer=0;
x=0.0;
z=0.0;
juli=40.0;
}

CMFCFrame1View::~CMFCFrame1View()
{
}

BOOL CMFCFrame1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= (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(40.0,aspect,5.0,1000.0);

}

void CMFCFrame1View::OnDraw(CDC* pDC)
{
CMFCFrame1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CPaintDC dc(this);

glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0);

glRotatef(-90.0,1.0,0.0,0.0);/*返回原坐标*/
glTranslatef(-3.0,0.0,0.0);

SwapBuffers(dc.m_ps.hdc);
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(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);/**/
glLoadIdentity();
gluLookAt(0.0,20.0,0.000000001,0.0,0.0,0.0,0.0,1.0,0.0);

glPushMatrix();
glColor3f(0.7,0.7,0.7);
glTranslatef(0.0,0.0,1.1);
glRotatef(-90.0,1.0,0.0,0.0);
glutSolidCone(5.0,0.0,60.0,60.0); /*底盘*/
glPopMatrix();

glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(3.9,0.99,1.0); /*刻度*/
glRotatef(90.0,0.0,1.0,0.0);
gluCylinder(objCylinder, 0.05, 0.05, 0.8, 9999, 9);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(-4.7,0.99,1.0); /*刻度*/
glRotatef(90.0,0.0,1.0,0.0);
gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(0.0,0.99,-2.9); /*刻度*/
glRotatef(180.0,0.0,1.0,0.0);
gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(0.0,0.99,5.8); /*刻度*/
glRotatef(180.0,0.0,1.0,0.0);
gluCylinder(objCylinder,0.05, 0.05,0.8, 9999, 9);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,1.0,0.0);
glRotatef(45.0,0.0,1.0,0.0);
glTranslatef(-0.67,0.99,0.7); /*时针*/
glRotatef(Angle1/129600,0.0,1.0,0.0);
gluCylinder(objCylinder, 0.07, 0.02, 2.5, 9999, 9);
glPopMatrix();

glPushMatrix();
glColor3f(1.0,0.0,0.0);
glTranslatef(0.0,0.99,1.0); /*分针*/
glRotatef(Angle2/360,0.0,1.0,0.0);
gluCylinder(objCylinder, 0.05, 0.02, 3.5, 9999, 9);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,0.0,0.5);
glTranslatef(0.0,0.99,1.0); /*秒针*/
glRotatef(Angle1,0.0,1.0,0.0);
gluCylinder(objCylinder, 0.07, 0.02, 4.5, 9999, 9);
glPopMatrix();

SwapBuffers(dc.m_ps.hdc);
glDrawBuffer (GL_BACK);
glFlush();
}

else if(Timer==2)

{ glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);

if (juli>12.0)
{
glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/
glLoadIdentity();
gluLookAt(0.0,8.0,juli,0.0,0.0,0.0,0.0,1.0,0.0);
juli=juli-0.1;

glPushMatrix();
glColor3f(0.0,0.0,0.0);
glRotatef(-90.0,1.0,0.0,0.0);
glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/
glPopMatrix();

glPushMatrix();
glColor3f(1.0,0.0,1.0);
glLineWidth(4.0);
glTranslatef(4.0,1.0,0.0);
glutWireOctahedron(); /*画八面体*/
glLineWidth(1.0);
glPopMatrix();

glPushMatrix();
glColor3f(1.0,0.0,1.0);
glTranslatef(0.0,1.1,0.0);
glRotatef(Angle2,0.0,1.0,0.0);
gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/
glPopMatrix();

}

else if(juli<=12.0)
{
Angle2=Angle2+0.01;
if (Angle2==360.0)
Angle2=0.0;

glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/
glLoadIdentity();

x=12.0*sin(Angle2);
z=12.0*cos(Angle2);
gluLookAt(x,5.0,z,0.0,0.0,0.0,0.0,1.0,0.0);

glPushMatrix();
glColor3f(0.0,0.0,0.0);
glRotatef(-90.0,1.0,0.0,0.0);
glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/
glPopMatrix();

glPushMatrix();
glColor3f(1.0,0.0,1.0);
glLineWidth(4.0);
glTranslatef(4.0,1.0,0.0);
glutWireOctahedron(); /*画八面体*/
glLineWidth(1.0);
glPopMatrix();

glPushMatrix();
glColor3f(1.0,0.0,1.0);
glTranslatef(0.0,1.1,0.0);
gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9);
glPopMatrix();

}
SwapBuffers(dc.m_ps.hdc);
glDrawBuffer (GL_BACK);
glFlush();
}
CView::OnTimer(nIDEvent);
}

void CMFCFrame1View::OnChangDirect()
{
// TODO: Add your command handler code here
Timer=2;
SetTimer(1,100 ,NULL);
}
a1012144015
2016-01-06 · TA获得超过6415个赞
知道大有可为答主
回答量:9038
采纳率:40%
帮助的人:1348万
展开全部
简单模拟图形时钟代码如下:

graphics.hVC上编译通过

给TC上编译没问题VC行graphics.hVC上编译通过

#include"graphics.h"
#include"math.h"
#include"dos.h"
#define pi 3.1415926
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y)
void init() /*划时钟边框函数*/
{
int i,l,x1,x2,y1,y2;
setbkcolor(1);
circle(300,240,200);
circle(300,240,205);
circle(300,240,5);
for(i=0;i<60;i++) /*划钟点上短线*/
{
if(i%5==0)
l=15;
else
l=5;
x1=200*sin(i*6*pi/180)+300;
y1=200*cos(i*6*pi/180)+240;
x2=(200-l)*sin(i*6*pi/180)+300;
y2=(200-l)*cos(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y,i,k=1;
int gdriver=9,gmode=2;
unsigned char h,m,s;
int o,p,q;
float n;
struct time t[1];
struct date d[1];
initgraph(&gdriver,&gmode,"c:\\tc");
initgraph(&gdriver,&gmode,"c:\\tc");
for(i=0;i<=6;i++)
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,i); /*控制输出字符字体方向大小*/
cleardevice();
settextjustify(1,1); /*指定坐标上输出字符串*/
outtextxy(300,80,"12") ;
outtextxy(300,390,"6");
outtextxy(140,230,"9");
outtextxy(460,230,"3");
outtextxy(380,100,"1");
outtextxy(220,100,"11");
outtextxy(430,160,"2");
outtextxy(430,310,"4");
outtextxy(380,370,"5");
outtextxy(220,370,"7");
outtextxy(160,160,"10");
outtextxy(160,310,"8");
}
init();
setwritemode(1); /*设置画线输出模式*/
if(k!=0)
{
getdate(d); /*获得系统日期函数*/
o=d[0].da_year;
p=d[0].da_mon;
q=d[0].da_day;
gettime(t); /*获得系统时间函数*/
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec;
}
setcolor(7); /*设置时针颜色*/
n=(float)h+(float)m/60;
d(150,n,30); /*画出时针*/
setcolor(14); /*设置分针颜色*/
d(170,m,6); /*画出分针*/
setcolor(4); /*设置秒针颜色*/
d(190,s,6); /*画出秒针*/
while(!kbhit()) /*控制程序按下任意键退出*/
{
while(t[0].ti_sec==s)
gettime(t);
gotoxy(44,18); /*使光标移动指定坐标*/
printf("\b\b\b\b\b\b\b\b\b"); /*退格使表示时间字符串断变化*/
sound(400); /*按给定频率打开PC扬声器*/
delay(70); /*断程序执行时间70毫秒*/
sound(200);
delay(30);
nosound(); /*按给定频率关闭PC扬声器*/
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if(t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if(t[0].ti_hour!=h)
{

setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
if(s<10) /*用字符形式输出时间*/
{ if(m<10)
printf("%u:0%u:0%u",h,m,s);
else
printf("%u:%u:0%u",h,m,s);
}
else
{ if(m<10)
printf("%u:0%u:%u",h,m,s);
else
printf("%u:%u:%u",h,m,s);
}
gotoxy(34,19); /*指定坐标上输出日期*/
printf("%d年%d月%d日",o,p,q);
printf("\b\b\b\b\b\b\b\b\b");
}
getch();
closegraph();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
緗虞帱
推荐于2018-02-28 · TA获得超过578个赞
知道小有建树答主
回答量:298
采纳率:0%
帮助的人:343万
展开全部

把图片改为rar然后解压有原码,DX实现(点查看大图,然后图片另存为)。

本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
稽鑫衡紫玉
2019-03-24 · TA获得超过4047个赞
知道大有可为答主
回答量:3194
采纳率:25%
帮助的人:268万
展开全部
graphics.h在VC上编译通不过的
给你一个,这个在TC上编译没问题,VC不行的,graphics.h在VC上编译通不过的
#include"graphics.h"
#include"math.h"
#include"dos.h"
#define
pi
3.1415926
#define
X(a,b,c)
x=a*cos(b*c*pi/180-pi/2)+300
#define
Y(a,b,c)
y=a*sin(b*c*pi/180-pi/2)+240
#define
d(a,b,c)
X(a,b,c);Y(a,b,c);line(300,240,x,y)
void
init()
/*划时钟边框函数*/
{
int
i,l,x1,x2,y1,y2;
setbkcolor(1);
circle(300,240,200);
circle(300,240,205);
circle(300,240,5);
for(i=0;i<60;i++)
/*划钟点上的短线*/
{
if(i%5==0)
l=15;
else
l=5;
x1=200*sin(i*6*pi/180)+300;
y1=200*cos(i*6*pi/180)+240;
x2=(200-l)*sin(i*6*pi/180)+300;
y2=(200-l)*cos(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int
x,y,i,k=1;
int
gdriver=9,gmode=2;
unsigned
char
h,m,s;
int
o,p,q;
float
n;
struct
time
t[1];
struct
date
d[1];
initgraph(&gdriver,&gmode,"c:\\tc");
initgraph(&gdriver,&gmode,"c:\\tc");
for(i=0;i<=6;i++)
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,i);
/*控制输出字符的字体,方向,大小*/
cleardevice();
settextjustify(1,1);
/*在指定坐标上输出字符串*/
outtextxy(300,80,"12")
;
outtextxy(300,390,"6");
outtextxy(140,230,"9");
outtextxy(460,230,"3");
outtextxy(380,100,"1");
outtextxy(220,100,"11");
outtextxy(430,160,"2");
outtextxy(430,310,"4");
outtextxy(380,370,"5");
outtextxy(220,370,"7");
outtextxy(160,160,"10");
outtextxy(160,310,"8");
}
init();
setwritemode(1);
/*设置画线的输出模式*/
if(k!=0)
{
getdate(d);
/*获得系统日期函数*/
o=d[0].da_year;
p=d[0].da_mon;
q=d[0].da_day;
gettime(t);
/*获得系统时间函数*/
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec;
}
setcolor(7);
/*设置时针颜色*/
n=(float)h+(float)m/60;
d(150,n,30);
/*画出时针*/
setcolor(14);
/*设置分针颜色*/
d(170,m,6);
/*画出分针*/
setcolor(4);
/*设置秒针颜色*/
d(190,s,6);
/*画出秒针*/
while(!kbhit())
/*控制程序按下任意键退出*/
{
while(t[0].ti_sec==s)
gettime(t);
gotoxy(44,18);
/*使光标移动到指定坐标*/
printf("\b\b\b\b\b\b\b\b\b");
/*退格,使表示时间的字符串不断变化*/
sound(400);
/*按给定的频率打开PC扬声器*/
delay(70);
/*中断程序的执行,时间为70毫秒*/
sound(200);
delay(30);
nosound();
/*按给定的频率关闭PC扬声器*/
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if(t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if(t[0].ti_hour!=h)
{
setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
if(s<10)
/*用字符的形式输出时间*/
{
if(m<10)
printf("%u:0%u:0%u",h,m,s);
else
printf("%u:%u:0%u",h,m,s);
}
else
{
if(m<10)
printf("%u:0%u:%u",h,m,s);
else
printf("%u:%u:%u",h,m,s);
}
gotoxy(34,19);
/*在指定坐标上输出日期*/
printf("%d年%d月%d日",o,p,q);
printf("\b\b\b\b\b\b\b\b\b");
}
getch();
closegraph();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友594d205f21
2013-04-07 · TA获得超过271个赞
知道小有建树答主
回答量:769
采纳率:0%
帮助的人:342万
展开全部
VC6.0下没有graphics.h 绘图有点麻烦哦
不过可以下载一个easyx
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式