#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);
}
你好这是我用mfc编写的一个时钟程序我不用你存话费你采纳就ok了别不采纳我可是花了很长时间来弄的源程序//这是关键代码voidCClockView::OnDraw(CDCpDC){ CClockDocpDoc=GetDocument(); ASSERT_VALID(pDoc); //TODO:adddrawcodefornativedatahere CBitmapbg; bgLoadBitmap(IDB_BITMAP1); CDCdc; dcCreateCompatibleDC(pDC); dcSelectObject(bg); pDC->BitBlt(0,0,400,400,&dc,0,0,SRCCOPY); CTimetm; tm=CTime::GetCurrentTime();//得到系统的时分秒inti=tmGetMinute()-1;//60 intj=tmGetHour();//24 intn=tmGetSecond();//60/ CStringstr; strFormat("%d:%d",j,i); MessageBox(str);/ //2460 floatmin=(float)(i-14)/60231415; floathour=(float)(j-3)/12231415;//角度 floatsec=(float)(n-14)/60231415; //原点位置intorg_x=200; intorg_y=200; //时针 inthour_ar=90;//长度 inth_x=hour_arcos(hour)+200; inth_y=hour_arsin(hour)+200; //分针 intmin_ar=140;//长度 intm_x=min_arcos(min)+200; intm_y=min_arsin(min)+200; //秒针 intsec_ar=170;//长度 ints_x=sec_arcos(sec)+200; ints_y=sec_arsin(sec)+200;CPenp1(PS_SOLID,4,RGB(100,100,100)); CPenpenOld=(CPen)pDC->SelectObject(p1); pDC->MoveTo(org_x,org_y); pDC->LineTo(m_x,m_y); CPenp2(PS_SOLID,3,RGB(200,100,100)); pDC->SelectObject(p2); pDC->MoveTo(org_x,org_y); pDC->LineTo(h_x,h_y);CPenp3(PS_SOLID,2,RGB(150,100,100)); pDC->SelectObject(p3); pDC->MoveTo(org_x,org_y); pDC->LineTo(s_x,s_y); SetTimer(1,1000,NULL);}voidCClockView::OnTimer(UINTnIDEvent){ //TODO:Addyourmessagehandlercodehereand/orcalldefault this->RedrawWindow(); CView::OnTimer(nIDEvent);}
我最近刚刚写出了能运行的代码,但是MFC的代码分布太分散,除了在CViewcpp里面,其他的源文件里也有,没办法在这里全贴出来。如果你想要全部文件的话,可以给我留个邮箱或者聊天账号之类的****。
我这里只能给你简单说一说我的思路。把客户区分成逻辑的两个区域,在左侧区域调用pDC->Ellipse()画出圆形,求出圆心坐标和半径,然后用三角函数乘以半径来定出三根表针的长度,画表针的时候定义三个不同的CPen对象,像素和RGB颜色设置不同就可以了。再调用pDC->MoveTo()和pDC->LineTo()把它们画出来。至于你说的文字和电子表上的示数,可以用pDC->TextOut()来实现。注意给它指定恰当的位置。
这里是我运行的结果。不知道是否符合你的要求。
以上就是关于在VC 6.0用C或C++语言编写一个模拟图形时钟,全部的内容,包括:在VC 6.0用C或C++语言编写一个模拟图形时钟,、用C++编写图形时钟程序、如何用vc++编写一个模拟时钟的程序,要求指针大小宽度颜色不一,旁边附加一个电子表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)