项目的搭建

相关头文件的引用

在CXxxView.h中添加以下面代码引用相关头文件:

#include <vtkRenderer.h>
#include <vtkMFCWindow.h>
#include <vtkWin32OpenGLRenderWindow.h>

添加成员变量vtkRenderer, vtkMFCWindow

在类CXxxView中定义两个需要用到的成员变量:

private:
//显示的变量,vtkrender和vtkMFCWindow
vtkRenderer *pvtkRenderer;
vtkMFCWindow *pvtkMFCWindow;

CXxxView()中实例化变量vtkRenderer

CXxxView::CXxxView()
{
// TODO: 在此处添加构造代码
//实例化视图对象
this->pvtkRenderer = vtkRenderer::New();
}

CXxxView::OnInitialUpdate()中初始化变量vtkMFCWindow

void CXxxView::OnInitialUpdate()
{
CView::OnInitialUpdate(); // TODO: 在此添加专用代码和/或调用基类
//初始化的时候创建vtkMFCwindow对象
if (this->pvtkMFCWindow) delete this->pvtkMFCWindow;
this->pvtkMFCWindow = new vtkMFCWindow(this);
//将render对象添加到实现窗口vtkMFCwindow中
this->pvtkMFCWindow->GetRenderWindow()->AddRenderer(this->pvtkRenderer);
}

CXxxView::OnDraw(), 实现vtkMFCWindow的绘制

void CXxxView::OnDraw(CDC* pDC)
{
CVtkMFCDoc* pDoc = GetDocument(); // TODO: 在此处为本机数据添加绘制代码
if (this->pvtkMFCWindow)
{
if (pDC->IsPrinting())
this->pvtkMFCWindow->DrawDC(pDC);
}
}

CXxxView::OnSize(), 窗口大小重绘

void CXxxView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码
if (this->pvtkMFCWindow)
this->pvtkMFCWindow->MoveWindow(0, 0, cx, cy);
}

CXxxView::OnDestroy()中释放变量vtkRenderer

void CXxxView::OnDestroy()
{
CView::OnDestroy();
// TODO: 在此处添加消息处理程序代码
if (this->pvtkRenderer) this->pvtkRenderer->Delete();
}

CXxxView::~CXxxView()中释放变量pvtkMFCWindow

CXxxView::~CVtkMFCView()
{
if (this->pvtkMFCWindow) delete this->pvtkMFCWindow;
}
05-11 19:47