VTK Render into C#

前端 未结 1 576
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 19:16

I have a c++ application that uses VTK, I want to have vtkRenderWindow and put it into C# WPF project without using C# wrapper.

相关标签:
1条回答
  • 2021-01-15 19:51

    The main idea of this is to set the HWND as parent of the vtkRenderWindow

    Here is how to do that: C++ class:

    class MyRender
    {
    //attributes
    ....
    
    MyRender(HWND parent)
    {
        renderer = vtkSmartPointer<vtkRenderer>::New();
        _render = vtkSmartPointer<vtkRenderWindow>::New();
    
        _render->AddRenderer(renderer);
        interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
        interactor->SetRenderWindow(_render);
    
    //setting background
        renderer->SetBackground(0.1, 0.2, 0.4);
        _render->SetParentId(parent);
    }
    
    void Render()
    {
        interactor->Initialize();
        _render->Render();
    }
    //...more methods
    }
    

    Create the CLR class to wrap the C++ class, in this form:

    class RenderWindows_CLR
    {
        //attributes
        MyRender* renderWindow;
        RenderWindows_CLR::RenderWindows_CLR::RenderWindows_CLR(IntPtr parent)
        {
            renderWindow = new MyRender((HWND)parent.ToPointer());
        }
    
        void RenderWindows_CLR::RenderWindows_CLR::Render(IntPtr parent)
        {
            renderWindow->Render();
        }
        }
    

    How to use it from C#: Here is how to put on the place of Windows Forms panel:

    window = new RenderWindows_CLR.RenderWindows_CLR(this.panel.Handle);
    window.Render()
    

    Here is how to put on WPF:

    HwndSource source = (HwndSource)HwndSource.FromVisual(this);
    IntPtr hWnd = source.Handle;
    window = new RenderWindows_CLR.RenderWindows_CLR(hWnd);
    

    To delete the title bar, just add to the C++ DLL this (after the render window be created):

    HWND window = (HWND)_render->GetGenericWindowId();
    LONG style = GetWindowLong(window, GWL_STYLE) & ~(WS_BORDER | WS_DLGFRAME | WS_THICKFRAME);
    SetWindowLong(window,-16L, style);
    

    Hope this help.

    0 讨论(0)
提交回复
热议问题