270 likes | 355 Views
AppWizard 编程实现( VC++ ). 一、 使用 AppWizard 建立一个新的 MDI 程序. 第一步 : 启动 VC ++,打开菜单 <File> 并选择 <New> ,出现对话框 第二步 :在对话框上 Projects 选项卡中选择 MFC AppWizard (.exe) 选项。在右边的 Project Name 栏中键入项目名称***,并在 Location 栏中键入待生成的项目位置,单击 OK 按钮,出现 MFC AppWizard_step 1 对话框
E N D
一、 使用AppWizard建立一个新的MDI程序 第一步:启动VC++,打开菜单<File>并选择<New>,出现对话框 第二步:在对话框上Projects选项卡中选择MFC AppWizard (.exe)选项。在右边的Project Name栏中键入项目名称***,并在Location栏中键入待生成的项目位置,单击OK按钮,出现MFC AppWizard_step 1 对话框 第三步:在对话框中选择Multiple documents项,用于生成多文档界面,单击Next按钮,可得MFC AppWizard_step 2 of 6 第四步:使用缺省项,继续单击Next按钮,在最后一步出现MFC AppWizard_step 6 of 6 第五步:AppWizard自动生成了C***App,C***Doc,C***View,CChildFrame,CMainFrame类,单击Finish按钮,AppWizard最后给出新生成项目的一些信息,单击OK按钮,完成MDI建构
二、连接OpenGL函数库 需要在所创建项目中加入的三个函数库 opengl32.lib glu32.lib glaux.lib 连接库文件 打开Project菜单,选择<Add to Project>,从中找到<File>选项,出项Insert Files into Project 对话框 找到VC安装目录,如c:\Program Files\Microsoft Visual Studio\Vc98,选取Lib目录,从中选取opengl32.lib,glu32.lib,glaux.lib库文件,单击OK,加入到应用程序库项目中。
在所创建的视图类头文件中添加下面三个头文件在所创建的视图类头文件中添加下面三个头文件 #include <gl\gl.h>#include <gl\glu.h>#include <gl\glaux.h>
// Attributes • public: • CGraphic1Doc* GetDocument(); • public: • HGLRC hglrc; //RC句柄
// Implementation • public: • virtual ~CGraphic1View(); • #ifdef _DEBUG • virtual void AssertValid() const; • virtual void Dump(CDumpContext& dc) const; • #endif • protected: • void myinit(); //自定义初始化函数 • void display(); //自定义的绘图函数
WM_CREAT • WM_DESTORY • WM_SIZE
设置像素格式参数 • PIXELFORMATDESCRIPTOR pfd={ • sizeof(PIXELFORMATDESCRIPTOR), //sizeof this pfd • 1, PFD_DRAW_TO_WINDOW| //support window PFD_SUPPORT_OPENGL| //support OpenGL PFD_DOUBLEBUFFER, //double buffered PFD_TYPE_RGBA, //RGBA type 24, //24-bit color 0,0,0,0,0,0, //color bits ignored 0, //no alpha buffer 0, //shift bit ignored 0, //no accumulation buffer
0,0,0,0, //accum bits ignored • 32, //32-bit depth-buffer • 0, //no stencil buffer • 0, //no auxiliary buffer • PFD_MAIN_PLANE, //main layer • 0, //reserved • 0,0,0 //layer masks ignored • };
int CGraphicView::OnCreate(LPCREATESTRUCT lpCreateStruct) • { • if (CView::OnCreate(lpCreateStruct) == -1) • return -1; • // TODO: Add your specialized creation code here • //设置好像素格式后,添加下列语句 • //获取当前DC句柄 • CClientDC clientdc(this); • //返回最佳匹配的象素格式 • int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd); • //设置象素格式 • BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd); • //create rendering context • hglrc=wglCreateContext(clientdc.m_hDC); • return 0; • }
void CGraphicView::OnSize(UINT nType, int cx, int cy) • { • CView::OnSize(nType, cx, cy); • // TODO: Add your message handler code here • int width=cx; • int height=cy; • CClientDC clientdc(this); • wglMakeCurrent (clientdc.m_hDC,hglrc); • glViewport(0,0,width,height);//重新设置视口 • wglMakeCurrent(NULL,NULL); • }
void CGraphicView::OnDestroy() • { • CView::OnDestroy(); • // TODO: Add your message handler code here • wglDeleteContext(hglrc); • }
void CGraphicView::OnDraw(CDC* pDC) • { • CGraphicDoc* pDoc = GetDocument(); • ASSERT_VALID(pDoc); • // TODO: add draw code for native data here • wglMakeCurrent(pDC->m_hDC,hglrc);//设置一个线程的当前绘图描述表 • display(); • wglMakeCurrent(pDC->m_hDC,NULL);//释放绘图描述表 • //所使用的设备描述表,并忽略设备描述表句柄pDC-//>m_hDC • SwapBuffers(pDC->m_hDC);//交换前后缓冲区 • }
void CGraphicView::display() • { myinit(); int ii; double *coordX=new double[100]; double *coordY=new double [100]; float scaleX=0.05f; float scaleY=0.05f; glPushMatrix(); //sin function glColor3f(0.0f,0.0f,1.0f); glPushMatrix(); glTranslatef(-0.9f,0.6f,0); glPointSize(2.0);//点尺寸大小 glBegin(GL_POINTS); int numPnt=25; for(ii=0;ii<=numPnt-1;ii++) { coordX[ii]=ii*2*PI/numPnt; coordY[ii]=2*sin(2*coordX[ii]); glVertex2d(coordX[ii]*scaleX,coordY[ii]*scaleY); } glEnd(); glPopMatrix();glPopMatrix(); delete coordX; delete coordY; //强制绘图,不驻留缓存 glFlush(); • }
void CFirstOpenGLView::myinit() • { • //设置背景色 • //glColor4f(0.0,0.5f,0.7f,0.0);//BLUE • glColor4f(1.0,1.0,1.0,0.0);//WHITE • //清除颜色缓冲区 • glClear(GL_COLOR_BUFFER_BIT); • //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); • }
//drawing lines • glColor3f(1.0f,0.0f,0.0f); glPushMatrix(); • glTranslatef(-0.55f,0.6f,0); glLineWidth(3.0);//线宽 • glLineStipple(1,0x1F83);//线型 • glBegin(GL_LINES); • glVertex2d(0.0*scaleX,0.0*scaleY); • glVertex2d(5.0*scaleX,0.0*scaleY); • glVertex2d(0.0*scaleX,0.8*scaleY); • glVertex2d(5.0*scaleX,0.8*scaleY); • glVertex2d(1.0*scaleX,-0.5*scaleY); • glVertex2d(1.0*scaleX,1.3*scaleY); • glVertex2d(4.0*scaleX,-0.5*scaleY); • glVertex2d(4.0*scaleX,1.3*scaleY); • // • glVertex2d(0.0*scaleX,-0.5*scaleY); • glVertex2d(0.0*scaleX,-1.3*scaleY); • glVertex2d(0.0*scaleX,-0.5*scaleY); • glVertex2d(2.2*scaleX,-0.5*scaleY); • glVertex2d(2.2*scaleX,-0.5*scaleY); • glVertex2d(2.2*scaleX,-1.3*scaleY); • glVertex2d(0.0*scaleX,-1.3*scaleY); • glVertex2d(2.2*scaleX,-1.3*scaleY);
// glVertex2d(2.8*scaleX,-0.5*scaleY); glVertex2d(2.8*scaleX,-1.3*scaleY); glVertex2d(2.8*scaleX,-0.5*scaleY); glVertex2d(5.0*scaleX,-0.5*scaleY); glVertex2d(5.0*scaleX,-0.5*scaleY); glVertex2d(5.0*scaleX,-1.3*scaleY); glVertex2d(2.8*scaleX,-1.3*scaleY); glVertex2d(5.0*scaleX,-1.3*scaleY); // glVertex2d(0.0*scaleX,-1.8*scaleY); glVertex2d(5.0*scaleX,-1.8*scaleY); glVertex2d(1.0*scaleX,-2.3*scaleY); glVertex2d(1.0*scaleX,-1.3*scaleY); glVertex2d(4.0*scaleX,-2.3*scaleY); glVertex2d(4.0*scaleX,-1.3*scaleY);
// glVertex2d(0.0*scaleX,-2.3*scaleY); glVertex2d(0.0*scaleX,-3.1*scaleY); glVertex2d(0.0*scaleX,-2.3*scaleY); glVertex2d(2.2*scaleX,-2.3*scaleY); glVertex2d(2.2*scaleX,-2.3*scaleY); glVertex2d(2.2*scaleX,-3.1*scaleY); glVertex2d(0.0*scaleX,-3.1*scaleY); glVertex2d(2.2*scaleX,-3.1*scaleY); // glVertex2d(2.8*scaleX,-2.3*scaleY); glVertex2d(2.8*scaleX,-3.1*scaleY); glVertex2d(2.8*scaleX,-2.3*scaleY); glVertex2d(5.0*scaleX,-2.3*scaleY); glVertex2d(5.0*scaleX,-2.3*scaleY); glVertex2d(5.0*scaleX,-3.1*scaleY); glVertex2d(2.8*scaleX,-3.1*scaleY); glVertex2d(5.0*scaleX,-3.1*scaleY); glEnd(); glPopMatrix();
void CFirstOpenGLView::BresenhamCircle(int R) • { • int x,y,delta,delta1,delta2,direction; • delta1=0;delta2=0; • float scaleX=0.05f; • float scaleY=0.05f; • glColor3f(0.0f,0.0f,1.0f); • glPushMatrix(); • glTranslatef(-0.9f,0.6f,0); • glPointSize(5.0);//点尺寸大小
x=0; y=R; delta=2*(1-R); • while(y>=0) • { • glBegin(GL_POINTS); • glVertex2d(x*scaleX,y*scaleY); • glEnable(GL_POINT_SMOOTH); • glEnd();
if(delta<0) • { • delta1+=(2*(delta+y)-1); • if(delta1<=0)direction=1; • else direction=2; • } • else if(delta>0) • { • delta2+=2*(delta-x)-1; • if(delta2<=0)direction=2; • else direction=3; • } • else • direction=2;
switch(direction) • { • case 1: x++; delta+=2*x+1; • break; • case 2: x++; y--; • delta+=2*(x-y+1); break; • case 3: y--; • delta+=(-2*y+1); • break; • }//switch • }//while • glPopMatrix(); • glFlush(); • }
上机考试题目(任选其一) • 上机实现Cohen-Sutherland线段裁剪算法 • 上机实现一种区域填充算法 • 上机完成生成圆弧的各种算法