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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…