c++visual-studiomfcvtk

VTK window in MFC tab control


I have developed a MFC application which contains tabs .I need to link the vtk window inside this tab. How is it possible? I have referred this link " https://gitlab.kitware.com/vtk/vtk/blob/ee46f867e9bc052f38c3e51534a93aa087af9efd/Examples/GUI/Win32/SampleMFC/vtkMFCRenderView.cpp" but couldn't understand the logic.


Solution

  • In your header:

    #include <vtkMFCWindow.h>
    #include <vtkRenderer.h>
    #include <vtkImageActor.h>
    
    class CYourView : public CView
    {
    ...
    ...
    protected:
        vtkMFCWindow* m_pvtkMFCWindow;
        vtkRenderer* m_pvtkRenderer;
        vtkImageActor* m_pvtkImageActor;
    ...
    };
    

    in your implementation file (cpp):

    #include <vtkWin32OpenGLRenderWindow.h>
    
     CYourView::CYourView()
     {
        // TODO: add construction code here
     
        m_pvtkMFCWindow = NULL;
        m_pvtkRenderer = vtkRenderer::New();
        m_pvtkImageActor = vtkImageActor::New();
     }
     
     CYourView::~CYourView()
     {
        if(NULL != m_pvtkMFCWindow)
            delete m_pvtkMFCWindow;
     }
     
    void CYourView::OnDraw(CDC* pDC)
    {
        CYourDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        if (!pDoc)
            return;
    
        // TODO: add draw code for native data here
    
        if(NULL != m_pvtkMFCWindow && pDC->IsPrinting())
                m_pvtkMFCWindow->DrawDC(pDC);
    }
    
    void CYourView::OnInitialUpdate()
    {
        CView::OnInitialUpdate();
    
        // TODO: Add your specialized code here and/or call the base class
    
        m_pvtkMFCWindow = new vtkMFCWindow(this);
        m_pvtkMFCWindow->GetRenderWindow()->AddRenderer(m_pvtkRenderer);
        m_pvtkRenderer->SetBackground(0.0, 0.0, 0.5);
        if(NULL != GetDocument()->m_pvtkBMPReader)
        {
            //setup renderer
            m_pvtkImageActor->GetMapper()->SetInputConnection(GetDocument()->m_pReader->GetOutputPort());
            m_pvtkRenderer->AddActor(m_pvtkImageActor);
    //      m_pvtkRenderer->ResetCamera();
        }
    }
    
    void CYourView::OnDestroy()
    {
        if(NULL != m_pvtkRenderer)
            m_pvtkRenderer->Delete();
        if(NULL != m_pvtkImageActor)
            m_pvtkImageActor->Delete();
    
        CView::OnDestroy();
    
        // TODO: Add your message handler code here
    }
    
    BOOL CYourView::OnEraseBkgnd(CDC* pDC)
    {
        // TODO: Add your message handler code here and/or call default
    
        return TRUE;
    //  return CView::OnEraseBkgnd(pDC);
    }
    
    void CYourView::OnSize(UINT nType, int cx, int cy)
    {
        CView::OnSize(nType, cx, cy);
    
        // TODO: Add your message handler code here
    
        if(NULL != m_pvtkMFCWindow)
            m_pvtkMFCWindow->MoveWindow(0, 0, cx, cy);
    }
    

    this example is for SDI/MDI case, but you easily adapt on CDialog based, if you need it.

    P.S. In your stdafx.h file, you need to insert:

    #define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
    #define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)