Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c++ - VTK Render into C#

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...