VTK + MFC Single Document

合集下载

基于VTK的MFC应用程序开发

基于VTK的MFC应用程序开发

基于VTK的MFC应用程序开发提到MFC一般都不陌生,大部分在校学生使用最多的应该就是它了。

但是相对于Qt 平台来说,MFC的机制比较复杂。

当使用MFC平台进行VTK程序开发时,许多人可能没有头绪。

这里结合一个实例讲一下如何在MFC平台下进行VTK程序开发。

这里在MFC下实现一个基于VTK的单文档图像显示程序。

通过这个程序,主要演示两个方面:一是怎样使用CMake将MFC程序与VTK结合;二是怎样在MFC程序中调用VTK 类实现具体的功能。

这个也是基于MFC和VTK进行软件开发的基础,搭好这个框架后,以后的工作就是垒砖了,相信通过前面的学习,垒砖已经小菜一碟了。

在开始代码之前,这里提一下,相信许多人在网上查询这方面的资料的时候,有许多资料会讲到通过MFC中的开发环境(如Visual Studio 2008)设置VTK的包含路径和lib库路径等。

但是这样配置对于一般用户来说比较复杂,每次开发新的工程时候,都要查找和设置这些库,既费时费力,又不利于程序的移植(例如换到另外一台机器,如果VTK编译路径不一致的话就会找不到库)。

因此这里还是推荐大家使用CMake来管理程序。

仅仅几行脚本代码就可以实现VTK库的配置,而且便于程序移植。

现在开始一步步实现VTK和MFC的程序开发。

首先建立一个单文档的MFC工程,工程名字为vtkSDI。

这里与基于Qt的VTK程序开发稍微有点不同。

先建立工程的目的是我们需要把由MFC自动生成的类加入到CMakeLists.txt中,方便管理。

MFC单文档工程的建立比较简单,过程不再演示。

工程文件如下所示,主要的类为CvtkSDIApp,CMainFrame,CvtkSDIDoc,CvtkSDIView和CAboutDlg五个。

由于我们使用CMake来配置和生成工程,因此将工程目录下的工程文件删除,主要是.ncb,.sln,.vcproj,.user文件。

图1 MFC单文档工程的建立时所包含的文件下面编写CMakeLists.txt文件,将工程文件写入到CMakeLists.txt中并连接VTK动态库。

VTK + MFC Single Document

VTK + MFC Single Document

VTK + MFC Single Document其实很弱的,VT K自带的源代码里就有这个文档,CHM格式的。

vtkSIDIView.h//Additional or changed code is indicated in bold:// Include the required header files for the vtk classes we are using #include <vtkRenderer.h>#include <vtkWin32OpenGLRenderWindow.h>#include <vtkWin32RenderWindowInteractor.h>#include <vtkSphereSource.h>#include <vtkConeSource.h>#include <vtkGlyph3D.h>#include <vtkElevationFilter.h>#include <vtkPolyDataMapper.h>#include <vtkActor.h>#include <vtkCubeAxesActor2D.h>class CVtkSDIView : public CView{protected: // create from serialization onlyCVtkSDIView();DECLARE_DYNCREATE(CVtkSDIView)// Attributespublic:CVtkSDIDoc* GetDocument();// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CVtkSDIView)public:virtual void OnDraw(CDC* pDC); // overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lPara m);//}}AFX_VIRTUAL// Implementationprivate:void Pipeline ( void );virtual ~CVtkSDIView();vtkWin32OpenGLRenderWindow *renWin;vtkRenderer *ren;vtkWin32RenderWindowInteractor *iren;vtkSphereSource *sphere;vtkPolyDataMapper *sphereMapper;vtkElevationFilter *sphereElevation;vtkActor *sphereActor;vtkConeSource *cone;vtkGlyph3D *glyph;vtkPolyDataMapper *spikeMapper;vtkActor *spikeActor;vtkCubeAxesActor2D *sphereAxis;#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const; #endifprotected:// Generated message map functionsprotected://{{AFX_MSG(CVtkSDIView)afx_msg void OnSize(UINT nType, int cx, int cy);afx_msg BOOL OnEraseBkgnd(CDC* pDC);afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);//}}AFX_MSGDECLARE_MESSAGE_MAP()};复制代码vtkSIDIView.cpp//Additional or changed code is indicated in bold:CVtkSDIView::CVtkSDIView(){// Create the the renderer, window and interactor objects.this->ren = vtkRenderer::New();this->renWin = vtkWin32OpenGLRenderWindow::New();this->iren = vtkWin32RenderWindowInteractor::New();// Create the the objects used to form the visualisation.this->sphere = vtkSphereSource::New();this->sphereElevation = vtkElevationFilter::New();this->sphereMapper = vtkPolyDataMapper::New();this->sphereActor = vtkActor::New();this->cone = vtkConeSource::New();this->glyph = vtkGlyph3D::New();this->spikeMapper = vtkPolyDataMapper::New();this->spikeActor = vtkActor::New();this->sphereAxis = vtkCubeAxesActor2D::New();}CVtkSDIView::~CVtkSDIView(){// Delete the the renderer, window and interactor objects.this->ren->Delete();this->iren->Delete();this->renWin->Delete();// Delete the the objects used to form the visualisation.this->sphere->Delete();this->sphereElevation->Delete();this->sphereMapper->Delete();this->sphereActor->Delete();this->cone->Delete();this->glyph->Delete();this->spikeMapper->Delete();this->spikeActor->Delete();this->sphereAxis->Delete();}void CVtkSDIView::OnDraw(CDC* pDC){CVtkMDIDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if ( !this->iren->GetInitialized() ){CRect rect;this->GetClientRect(&rect);this->iren->Initialize();this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);this->ren->ResetCamera();}// Invoke the pipelinePipeline();if ( pDC->IsPrinting() ){this->BeginWaitCursor();// Obtain the size of the printer page in pixels.int cxPage = pDC->GetDeviceCaps(HORZRES);int cyPage = pDC->GetDeviceCaps(VERTRES);// Get the size of the window in pixels.int *size = this->renWin->GetSize();int cxWindow = size[0];int cyWindow = size[1];float fx = float(cxPage) / float(cxWindow);float fy = float(cyPage) / float(cyWindow);float scale = min(fx,fy);int x = int(scale * float(cxWindow));int y = int(scale * float(cyWindow));this->renWin->SetupMemoryRendering(cxWindow, cyWindow, pD C->GetSafeHdc());this->renWin->Render();HDC memDC = this->renWin->GetMemoryDC();StretchBlt(pDC->GetSafeHdc(),0,0,x,y,memDC,0,0,cxWindow, cyWindow,SRCCOPY);this->renWin->ResumeScreenRendering();this->EndWaitCursor();}else{this->renWin->Render();}}void CVtkSDIView::OnSize(UINT nType, int cx, int cy){CView::OnSize(nType, cx, cy);CRect rect;this->GetClientRect(&rect);this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top); }BOOL CVtkSDIView::OnEraseBkgnd(CDC* pDC){return TRUE;}int CVtkSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CView::OnCreate(lpCreateStruct) == -1)return -1;this->renWin->AddRenderer(this->ren);// setup the parent windowthis->renWin->SetParentId(this->m_hWnd);this->iren->SetRenderWindow(this->renWin);return0;}LRESULT CVtkSDIView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) {switch (message){//case WM_PAINT:case WM_LBUTTONDOWN:case WM_LBUTTONUP:case WM_MBUTTONDOWN:case WM_MBUTTONUP:case WM_RBUTTONDOWN:case WM_RBUTTONUP:case WM_MOUSEMOVE:case WM_CHAR:case WM_TIMER:if (this->iren->GetInitialized()){return vtkHandleMessage2(this->m_hWnd, message, wParam, lParam, this->i ren);}break;}return CView::WindowProc(message, wParam, lParam);}void CVtkSDIView::Pipeline(){// Construct the spherethis->sphere->SetRadius(1);this->sphere->SetThetaResolution(18);this->sphere->SetPhiResolution(18);this->sphere->LatLongTessellationOn();// Generate elevationsthis->sphereElevation->SetInput(this->sphere->GetOutput());this->sphereElevation->SetLowPoint(0,0,-1);this->sphereElevation->SetHighPoint(0,0,1);this->sphereElevation->SetScalarRange(-1,1);// Link the mapperthis->sphereMapper->SetInput(this->sphereElevation->GetPolyDataOut put());this->sphereMapper->SetColorModeToMapScalars();this->sphereMapper->SetScalarRange(-1,1);// Link the actorthis->sphereActor->SetMapper(this->sphereMapper);// Add it to the rendererthis->ren->AddActor(this->sphereActor);// Construct the conethis->cone->SetResolution(8);// Construct the glyphs on the spherical surfacethis->glyph->SetInput(this->sphere->GetOutput());this->glyph->SetSource(this->cone->GetOutput());this->glyph->SetVectorModeToUseNormal();this->glyph->SetScaleModeToScaleByVector();this->glyph->SetScaleFactor(0.1);// Link the mapper to the glyphthis->spikeMapper->SetInput(this->glyph->GetOutput()); // Link the actorthis->spikeActor->SetMapper(this->spikeMapper);// Add it to the rendererthis->ren->AddActor(this->spikeActor);// Add in the cube axis actorthis->sphereAxis->SetInput(this->sphereElevation->GetOutput());this->sphereAxis->SetCamera(this->ren->GetActiveCamera());// Add it to the rendererthis->ren->AddActor(this->sphereAxis);}复制代码。

MFC学习(七)单文档程序

MFC学习(七)单文档程序

MFC学习(七)单⽂档程序1 MFC单⽂档程序的主要类(1)⽂档类(Document)即应⽤程序处理的数据对象,⽂档⼀般从 MFC 中 CDocument 中派⽣。

CDocument 类⽤于相应数据⽂件的读取以及存储 Cview 类所需要观察和处理的信息。

(2)视类(View)视相当于⽂档在应⽤程序中的观察窗⼝,它确定了⽤户对⽂档的观察⽅式和⽤户编辑⽂档的⽅式。

对于图形来说视就好⽐我们进⾏绘图⼯作的画布,对图形的操作都是在视上进⾏的。

另外,视类中有⼀个重要的成员函数 OnDraw() 函数。

重载的 OnDraw() 函数要完成两件事,即调⽤相应的⽂档的函数获取⽂档数据和调⽤ GDI 图形设备接⼝的函数在视中画出⽂档数据。

(3)主窗⼝类(Main Frame Window)主窗⼝是 Windows 应⽤程序中限定其所有窗⼝范围的最外边框。

应⽤程序中的所⽤其它窗⼝都直接或间接地为主窗⼝的⼦窗⼝,视占⽂档窗⼝的客户区,⽽⽂档窗⼝⼜是主窗⼝的⼦窗⼝。

(4)⽂档模板类(Document Template)⽂档模板类⽤于协调⽂档对象、视对象、和主窗⼝对象的创建过程。

它是从类 CDocTemplate 或其派⽣类中派⽣的。

⼀个⽂档模板可以管理同⼀⽂档类型的所有⽂档。

(5)应⽤类(Application)⼀个应⽤程序有且只有⼀个应⽤类的对象,它控制上述所有的对象。

⼀个应⽤程序对象就代表⼀个应⽤程序,当⽤户启动应⽤程序,Windows 调⽤应⽤程序框架内置的 WinMain 函数,并且 WinMain 寻找⼀个由 CWinApp 派⽣的全局构造的应⽤程序对象,全局对象在应⽤程序之前构造。

(6)图形设备接⼝PC 相容机种上可以连接许多种不同的视讯设备,所以 GDI 的主要⽬的之⼀是⽀援与设备⽆关的图形。

⼀个 Windows 图形设备接⼝对象类型由⼀个 MFC 类库表⽰,这些类有⼀个共同的抽象基类:CGdiObject。

用VC++MFC做文本编辑器(单文档模式)

用VC++MFC做文本编辑器(单文档模式)

用VC++MFC做文本编辑器(单文档模式)原来做过一个用对话框实现的文本编辑器,其实用MFC模板里面的单文档模板也可以做,甚至更加方便,适合入门级的爱好者试试,现介绍方法如下:1,首先新建一个工程,选择MFC AppWizard(exe),定名字为:textview_1,程序类型选择单个文档,其他均默认完成。

2,在系统自动生成的CTextview_1Doc类里面增加一个控件,用于文本文档的暂时存放:class CTextview_1Doc : public CDocument{......public:CStringArray m_strContent;}然后在CTextview_1Doc类的Serialize函数里面增加打开、保存文本文件的程序:void CTextview_1Doc::Serialize(CArchive& ar){CString str;if (ar.IsStoring()){// TODO: add storing code hereint nLines = (int)m_strContent.GetSize();for ( int i=0; i<nLines; i++ ){str = m_strContent.GetAt( i );ar.WriteString( str ); // 将字符串集合类对象中文本保存到硬盘}}else{// TODO: add loading code herewhile ( ar.ReadString( str ) ){m_strContent.Add( str ); // 将行文本添加到字符串集合类对象中}}}然后鼠标在CTextview_1Doc 上点击右键,在弹出菜单里面选择:Add Virtual Function,在弹出的窗口中选中DeleteContents,点击“添加和编辑”按钮。

在生成的程序中添加以下代码:void CTextview_1Doc::DeleteContents(){// TODO: Add your specialized code here and/or call the base classm_strContent.RemoveAll(); // 清除集合类对象中的内容CDocument::DeleteContents();}3,在系统自动生成的CTextview_1View类里面增加一个编辑器的控件指针成员,用于在界面中生成文本编辑器:class CTextview_1View : public CView{......public:CEdit* m_ctrlEdit;}该指针成员在类建立时要设置初始化值为NULL,否则运行起来就会出错,如下:CTextview_1View::CTextview_1View(): m_ctrlEdit(NULL) //添加这一行初始化代码{// TODO: add construction code here}用第2点介绍的类似步骤,为CTextview_1View类重写其OnInitialUpdate函数,添加内容如下:void CTextview_1View::OnInitialUpdate(){CView::OnInitialUpdate();// TODO: Add your specialized code here and/or call the base classCRect rcClient;GetClientRect( rcClient ); // 获取当前视图的客户区大小// if ( m_ctrlEdit ) delete m_ctrlEdit;m_ctrlEdit = new CEdit();m_ctrlEdit->Create( ES_MULTILINE | WS_CHILD | WS_VISIBLE| WS_HSCROLL | ES_AUTOHSCROLL // 自动水平滚动| WS_VSCROLL | ES_AUTOVSCROLL , // 自动垂直滚动rcClient, this, 201); // 创建多行编辑控件CTextview_1Doc* pDoc = GetDocument(); // 获取与视图相关联的文档指针// 以下是将文档中的m_strContent内容全部赋给strCString str;int nLines = (int)pDoc->m_strContent.GetSize();for ( int i=0; i<nLines; i++ ){str = str + pDoc->m_strContent.GetAt( i );str = str + "\r\n"; // 换行}m_ctrlEdit->SetTabStops( 16 ); // 设置Tab符大小m_ctrlEdit->SetWindowText( str ); // 将文档内容传给控件}4,现在运行一下,文本编辑器就做好了。

基于VTK的MFC应用程序开发

基于VTK的MFC应用程序开发

基于VTK的MFC应用程序开发(3)分类:VTK应用示例2013-05-17 13:37 1681人阅读评论(17) 收藏举报目录(?)[+]之前介绍了基于VTK的单文档应用程序开发,并以图像重采样为例,实现了一个简单的图像重采样的应用程序。

对于多文档应用程序,与单文档应用程序基本一致,这里就不再讲述。

对话框应用程序是MFC应用程序中一个使用非常广泛的框架,本节就以医学图像可视化中常用的四视图框架程序的实现为例,讲述基于VTK的对话框应用程序开发。

1. 利用VS和CMake建立一个空的MFC对话框程序框架。

利用VS创建一个MFC对话框工程vtkDialog,删除其中的工程文件,完成CMakeLists.txt 文件,并添加相应的代码文件和链接VTK动态库,利用CMake配置完毕后,打开生成的工程文件vtkDialog.sln,编译执行,即可得到一个空的对话框程序。

其中CvtkDialogDlg为该程序的主对话框类。

2. 设计用户界面,添加相应的控件本程序需要实现的功能有(1)图像读取和管理;(2)图像切分和浏览。

一个常见的医学图像可视化程序,包括四个视图,横断面视图,矢状面视图,冠状面视图和三维视图。

因此,基于以上设计,我们添加一个树控件,MFC中对应的控件类为CTreeCtrl。

树控件是最常用的文件管理控件,能够方便的对文件进行层次化组织和管理。

四视图的实现则需要四个控件,这里我们选择CStatic控件,将其添加至对话框窗口中。

添加完毕后,为控件生成相应的Control类型的变量。

按照上述设计,需要在CStatic中显示图像。

这就需要对CStatic类继续扩展,使其支持VTK 可视化管线。

一个可行的方法是,设计一个CStatic类的子类,并在该子类中实现VTK可视化管线和处理。

3. 实现VTK图像可视化控件3.1 首先添加一个MFC类CvtkView其基类选择为CStatic,并添加至CMakeLists.txt文件中进行管理。

MFC单文档及其简介

MFC单文档及其简介

MFC是一个编程框架MFC中的各种类结合起来构成了一个应用程序框架,它的目的就是让程序员在此基础上来建立Windows下的应用程序。

MFC框架定义了应用程序的轮廓,并提供了用户接口的标准实现方法。

AppWizard可以用来生成初步的框架文件。

资源编辑器用于帮助直观的设计用户接口。

ClassWizard用来协助添加代码到框架文件,最后,通过类库实现了应用程序特定的逻辑。

MFC提供了一个Windows应用程序开发模式,对程序的控制主要是由MFC框架完成的。

而且MFC也完成了大部分的功能,预定义或实现了许多事件和消息处理。

框架或者由其本身处理事件,不依赖程序员的代码,或者调用程序员的代码来处理应用程序特定的事件。

1.S DI生成1.步骤dxq2009首先,打开VC++6.0开发环境,然后,选择”File”菜单中的“New”子菜单,在弹出的对话框中选择“MFC AppWizard(exe)”项并在“Progect name”编辑框中输入合适的工程名字Simple1,如图,它的意思是创建一个基于MFC的应用,接着进入正式的创建过程,MFC 应用程序的创建过程有6步(基于对话框)或者6步(SDI或者MDI),下面首先介绍SDI 应用的创建过程。

(1)第一步用于选择应用的结构以及语言等。

如图1,首先确定应用是否需要Doc/View Architecture Support支持,因为不使用该结构的应用不支持从磁盘文件打开文档,也没有派生于类CWnd的窗口客户区。

上面3个单选按钮用于确定创建的应用类型,包括单文档,多文档,对话框,这里选择第一个。

然后从资源列表框选择应用所使用的语言种类,单击“Next”。

图1(2)第二步为用用程序选择4项数据库支持选项之一:如图2.如果选择了数据库支持,那么单击“Data Source”按钮,选择外部的数据库表项,一般按默认即可,单击“Next”。

图2(3)第三步选择希望包含在应用中的复合文档支持项,同时判定是否启用标准的ActiveX 资源,以及是否为应用的菜单条添加额外的自动化命令等,如图4,一般安默认,单击“Next”图4(4)第四步用于选择应用所需的基本用户接口特征,以及所想使用的工具栏类型,如图5,如果想要修改应用所使用的文件名和扩展名,或者想要调整应用的用户接口和框架风格,就单击“Advanced”,然后修改,一般默认,单击“Next”。

vtk与MFC联合编程

vtk与MFC联合编程

VTK/MFC联合编程(对话框程序)2011-06-19 18:10:17| 分类:VTK | 标签:vtk mfc |字号大中小订阅1 首先,建立一个MFC对话框程序,工程名Viewer,删除掉自动生成的.ncb和.sln文件(在下一步用CMake重新生成)2 使用CMake进行配置工程的VTK环境,CMakeLists.txt文件如下,需要修改的地方:VTK路径;StdAfx,Viewer,ViewerDlg,Viewer.rc,res/Viewer.rc2 则是刚刚建立的工程ImageViewer所自动生成的源文件和资源文件cmake_minimum_required(VERSION 2.6)FIND_PACKAGE(VTK)IF(NOT VTK_DIR)MESSAGE(FATAL_ERROR "Please set VTK_DIR.")ENDIF(NOT VTK_DIR)INCLUDE(${VTK_USE_FILE})INCLUDE("D:/VTK-5.6/vtk-5.6.1/GUISupport/MFC/VTKMFCSettings.cmake")IF(VTK_MFC_DELAYLOAD_VTK_DLLS)VTK_MFC_ADD_DELAYLOAD_FLAGS(CMAKE_EXE_LINKER_FLAGSvtkMFC.dllvtkRendering.dllvtkIO.dllvtkFiltering.dllvtkCommon.dll)ENDIF(VTK_MFC_DELAYLOAD_VTK_DLLS)SET( vtkDLG_SRCSStdAfxViewerViewerDlgViewer.rcres/Viewer.rc2)ADD_EXECUTABLE(vtkDLG WIN32 ${vtkDLG_SRCS})IF(VTK_MFC_EXTRA_LIBS)TARGET_LINK_LIBRARIES(vtkDLG ${VTK_MFC_EXTRA_LIBS})ENDIF(VTK_MFC_EXTRA_LIBS)TARGET_LINK_LIBRARIES(vtkDLG vtkMFC vtkRendering vtkIO vtkFiltering vtkCommon)3 Viewer程序,直接用对话框作为显示图像的窗口,显示一个球(1) 在CViewDlg.h头文件中添加成员变量和函数,当然不要忘记先添加include,留意VTK_MFC编程的renWin和iren分别是vtkWin32OpenGLRenderWindow ,vtkWin32RenderWindowInteractor,注意区别.private:void Pipeline();vtkRenderer *ren;vtkWin32OpenGLRenderWindow *renWin;vtkWin32RenderWindowInteractor *iren;vtkSphereSource *sphere;vtkPolyDataMapper *sphereMapper;vtkActor *sphereActor;(2) 考虑VTK管道线,我们按照管道线来向Viewer程序中添加代码,理清思路pipeline:vtkSource->vtkMapper->vtkActor【->vtkProperty】【->vtkLight】【->vtkCamera】->vtkRenderer->vtkRenderWindow【->vtkRenderWindowInteractor】A 在构造函数中实例化对象CViewerDlg::CViewerDlg(CWnd* pParent /*=NULL*/): CDialog(CViewerDlg::IDD, pParent){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//实例化对象ren = vtkRenderer::New();renWin = vtkWin32OpenGLRenderWindow::New();iren = vtkWin32RenderWindowInteractor::New();sphere = vtkSphereSource::New();sphereMapper = vtkPolyDataMapper::New();sphereActor = vtkActor::New();}B OnPaint()中添加管道线void CViewerDlg::OnPaint(){ ..........................................................else{CDialog::OnPaint();if (!iren->GetInitialized()){renWin->AddRenderer(ren);renWin->SetParentId(this->m_hWnd); //注意这一步,设置绘制窗口iren->SetRenderWindow(renWin);CRect rect;GetClientRect(&rect);iren->Initialize();renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);ren->ResetCamera();}}//调用管道线pipeline()Pipeline();renWin->Render();}C 当然是该写Pipeline()函数了void CViewerDlg::Pipeline(){//创建球sphere->SetRadius(1);sphere->SetThetaResolution(18);sphere->SetPhiResolution(18);sphere->LatLongTessellationOn();sphereMapper->SetInput(sphere->GetOutput());sphereActor->SetMapper(sphereMapper);ren->AddActor(sphereActor);}D 最后销毁对象,这里是在关闭对话框的响应函数里实现的void CViewerDlg::OnClose(){// TODO: Add your message handler code here and/or call default ren->Delete();renWin->Delete();iren->Delete();sphere->Delete();sphereMapper->Delete();sphereActor->Delete();CDialog::OnClose();}E 编译运行,结果应该如下。

MFC多文档和单文档视结构

MFC多文档和单文档视结构

MFC多文档和单文档视结构★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★//这一页的代码最重要了,呵呵&#8230;&#8230;什么都在这里面呢;单文档新建:CWinApp_________docManager-&gt;docSingleTemplate 的OpenDocumentFile函数参数为空,此函数完成了大部分东西,包括新建文档类框架类等______________然后是调用CDocument就没什么意思了,当然我们要是重载了CDocument的新建函数就是调用子类虚函数。

多文档新建:CWinApp_________docManager-&gt;docMultTemplate的OpenDocumentFile函数参数为空,此函数完成了大部分东西,包括新建文档类框架类等______________然后是调用CDocument就没什么意思了,当然我们要是重载了CDocument的新建函数就是调用子类虚函数。

单文档打开:CWinApp_________docManager中经过一个打开对话框传递参数,中途还调用了APP的OpenDocumentFile,当然如果我们的APP重载了这个函数也要调用我们的但是我们的函数一定别忘记最后返回是调用父类的此函数___________docSingleTemplate的OpenDocumentFile函数参数不为空,此函数完成了大部分东西,包括新建文档类框架类等______________然后是调用CDocument就没什么意思了,当然我们要是重载了CDocument的新建函数就是调用子类虚函数。

多文档打开:CWinApp_________docManager中经过一个打开对话框传递参数,中途还调用了APP的OpenDocumentFile,当然如果我们的APP重载了这个函数也要调用我们的但是我们的函数一定别忘记最后返回是调用父类的此函数___________docMultTemplate的OpenDocumentFile函数参数不为空,此函数完成了大部分东西,包括新建文档类框架类等______________然后是调用CDocument就没什么意思了,当然我们要是重载了CDocument的新建函数就是调用子类虚函数。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VTK + MFC Single Document其实很弱的,VT K自带的源代码里就有这个文档,CHM格式的。

vtkSIDIView.h//Additional or changed code is indicated in bold:// Include the required header files for the vtk classes we are using #include <vtkRenderer.h>#include <vtkWin32OpenGLRenderWindow.h>#include <vtkWin32RenderWindowInteractor.h>#include <vtkSphereSource.h>#include <vtkConeSource.h>#include <vtkGlyph3D.h>#include <vtkElevationFilter.h>#include <vtkPolyDataMapper.h>#include <vtkActor.h>#include <vtkCubeAxesActor2D.h>class CVtkSDIView : public CView{protected: // create from serialization onlyCVtkSDIView();DECLARE_DYNCREATE(CVtkSDIView)// Attributespublic:CVtkSDIDoc* GetDocument();// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CVtkSDIView)public:virtual void OnDraw(CDC* pDC); // overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lPara m);//}}AFX_VIRTUAL// Implementationprivate:void Pipeline ( void );virtual ~CVtkSDIView();vtkWin32OpenGLRenderWindow *renWin;vtkRenderer *ren;vtkWin32RenderWindowInteractor *iren;vtkSphereSource *sphere;vtkPolyDataMapper *sphereMapper;vtkElevationFilter *sphereElevation;vtkActor *sphereActor;vtkConeSource *cone;vtkGlyph3D *glyph;vtkPolyDataMapper *spikeMapper;vtkActor *spikeActor;vtkCubeAxesActor2D *sphereAxis;#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const; #endifprotected:// Generated message map functionsprotected://{{AFX_MSG(CVtkSDIView)afx_msg void OnSize(UINT nType, int cx, int cy);afx_msg BOOL OnEraseBkgnd(CDC* pDC);afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);//}}AFX_MSGDECLARE_MESSAGE_MAP()};复制代码vtkSIDIView.cpp//Additional or changed code is indicated in bold:CVtkSDIView::CVtkSDIView(){// Create the the renderer, window and interactor objects.this->ren = vtkRenderer::New();this->renWin = vtkWin32OpenGLRenderWindow::New();this->iren = vtkWin32RenderWindowInteractor::New();// Create the the objects used to form the visualisation.this->sphere = vtkSphereSource::New();this->sphereElevation = vtkElevationFilter::New();this->sphereMapper = vtkPolyDataMapper::New();this->sphereActor = vtkActor::New();this->cone = vtkConeSource::New();this->glyph = vtkGlyph3D::New();this->spikeMapper = vtkPolyDataMapper::New();this->spikeActor = vtkActor::New();this->sphereAxis = vtkCubeAxesActor2D::New();}CVtkSDIView::~CVtkSDIView(){// Delete the the renderer, window and interactor objects.this->ren->Delete();this->iren->Delete();this->renWin->Delete();// Delete the the objects used to form the visualisation.this->sphere->Delete();this->sphereElevation->Delete();this->sphereMapper->Delete();this->sphereActor->Delete();this->cone->Delete();this->glyph->Delete();this->spikeMapper->Delete();this->spikeActor->Delete();this->sphereAxis->Delete();}void CVtkSDIView::OnDraw(CDC* pDC){CVtkMDIDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if ( !this->iren->GetInitialized() ){CRect rect;this->GetClientRect(&rect);this->iren->Initialize();this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);this->ren->ResetCamera();}// Invoke the pipelinePipeline();if ( pDC->IsPrinting() ){this->BeginWaitCursor();// Obtain the size of the printer page in pixels.int cxPage = pDC->GetDeviceCaps(HORZRES);int cyPage = pDC->GetDeviceCaps(VERTRES);// Get the size of the window in pixels.int *size = this->renWin->GetSize();int cxWindow = size[0];int cyWindow = size[1];float fx = float(cxPage) / float(cxWindow);float fy = float(cyPage) / float(cyWindow);float scale = min(fx,fy);int x = int(scale * float(cxWindow));int y = int(scale * float(cyWindow));this->renWin->SetupMemoryRendering(cxWindow, cyWindow, pD C->GetSafeHdc());this->renWin->Render();HDC memDC = this->renWin->GetMemoryDC();StretchBlt(pDC->GetSafeHdc(),0,0,x,y,memDC,0,0,cxWindow, cyWindow,SRCCOPY);this->renWin->ResumeScreenRendering();this->EndWaitCursor();}else{this->renWin->Render();}}void CVtkSDIView::OnSize(UINT nType, int cx, int cy){CView::OnSize(nType, cx, cy);CRect rect;this->GetClientRect(&rect);this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top); }BOOL CVtkSDIView::OnEraseBkgnd(CDC* pDC){return TRUE;}int CVtkSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CView::OnCreate(lpCreateStruct) == -1)return -1;this->renWin->AddRenderer(this->ren);// setup the parent windowthis->renWin->SetParentId(this->m_hWnd);this->iren->SetRenderWindow(this->renWin);return0;}LRESULT CVtkSDIView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) {switch (message){//case WM_PAINT:case WM_LBUTTONDOWN:case WM_LBUTTONUP:case WM_MBUTTONDOWN:case WM_MBUTTONUP:case WM_RBUTTONDOWN:case WM_RBUTTONUP:case WM_MOUSEMOVE:case WM_CHAR:case WM_TIMER:if (this->iren->GetInitialized()){return vtkHandleMessage2(this->m_hWnd, message, wParam, lParam, this->i ren);}break;}return CView::WindowProc(message, wParam, lParam);}void CVtkSDIView::Pipeline(){// Construct the spherethis->sphere->SetRadius(1);this->sphere->SetThetaResolution(18);this->sphere->SetPhiResolution(18);this->sphere->LatLongTessellationOn();// Generate elevationsthis->sphereElevation->SetInput(this->sphere->GetOutput());this->sphereElevation->SetLowPoint(0,0,-1);this->sphereElevation->SetHighPoint(0,0,1);this->sphereElevation->SetScalarRange(-1,1);// Link the mapperthis->sphereMapper->SetInput(this->sphereElevation->GetPolyDataOut put());this->sphereMapper->SetColorModeToMapScalars();this->sphereMapper->SetScalarRange(-1,1);// Link the actorthis->sphereActor->SetMapper(this->sphereMapper);// Add it to the rendererthis->ren->AddActor(this->sphereActor);// Construct the conethis->cone->SetResolution(8);// Construct the glyphs on the spherical surfacethis->glyph->SetInput(this->sphere->GetOutput());this->glyph->SetSource(this->cone->GetOutput());this->glyph->SetVectorModeToUseNormal();this->glyph->SetScaleModeToScaleByVector();this->glyph->SetScaleFactor(0.1);// Link the mapper to the glyphthis->spikeMapper->SetInput(this->glyph->GetOutput()); // Link the actorthis->spikeActor->SetMapper(this->spikeMapper);// Add it to the rendererthis->ren->AddActor(this->spikeActor);// Add in the cube axis actorthis->sphereAxis->SetInput(this->sphereElevation->GetOutput());this->sphereAxis->SetCamera(this->ren->GetActiveCamera());// Add it to the rendererthis->ren->AddActor(this->sphereAxis);}复制代码。

相关文档
最新文档