VTK Render into C#

纵饮孤独 提交于 2019-12-30 11:22:09

问题


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:


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.



来源:https://stackoverflow.com/questions/30301087/vtk-render-into-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!