VTK Render into C#

本文关键字:into Render VTK | 更新日期: 2023-09-27 17:49:29

我有一个使用VTK的c++应用程序,我想要vtkRenderWindow并将其放入c# WPF项目中,而不使用c#包装器

VTK Render into C#

这样做的主要思想是将HWND设置为vtkRenderWindow的父节点

方法如下:c++类:

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
}

创建CLR类来包装c++类,格式如下:

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();
    }
    }

如何在c#中使用它:下面是如何放置在Windows窗体面板的位置:

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

下面是如何使用WPF:

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

要删除标题栏,只需在c++ DLL中添加以下内容(在创建渲染窗口之后):

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