I am trying to create a vtkRenderWindow as child window in existing c++ Windows Application using the VTK-9.2.6 version. As base code I used an old example from VTK/Examples/GUI/Win32/SimpleCxx/Win32Cone.cxx as written in some old VTKUsersGuide https://vtk.org/wp-content/uploads/2021/08/VTKUsersGuide.pdf. It looks as in 9.2 version the folder GUI/Win32/ doesn’t exist and I can’t find what is wrong with my code.
vtkNew renderWindow;
vtkNew renderer;
renderWindow->SetWindowName(“Sphere”);
renderWindow->AddRenderer(renderer);
renderWindow->SetParentId(hWnd);
SetParentId doesn’t create a window inside the window application. Tried to use casting window to specific vtkWin32OpenGLRenderWindow as suggested in this answer: c++ - How to add a WIN32 VTK client window inside any existing app already having a main window parent? - Stack Overflow . still the window was not created.
My code example:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND ewin;
//static myVTKApp* theVTKApp;
static UINT width;
static UINT height; switch (message)
{
case WM_CREATE:
{
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = “aa”;
if (!RegisterClassEx(&wc))
return NULL;
ewin = CreateWindowEx(0,
"button", "Exit",
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS ,
20, 400, 400, 60,
hWnd,
(HMENU)2,
GetModuleHandle(NULL),
NULL);
StartVtk(hWnd);
}
break;
case WM_COMMAND:
{
...
}
break;
case WM_PAINT:
{
...
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void StartVtk(HWND hWnd)
{
vtkNew colors;
// Create a sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(5.0);
sphereSource->SetPhiResolution(100);
sphereSource->SetThetaResolution(100);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("Cornsilk").GetData());
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetWindowName("Sphere");
renderWindow->AddRenderer(renderer);
renderWindow->SetParentId(hWnd);
vtkRenderWindow* rp = renderWindow.GetPointer();
vtkWin32OpenGLRenderWindow* rw = nullptr;
if (static_cast<vtkOpenGLRenderWindow*>(rp)) {
rw = static_cast<vtkWin32OpenGLRenderWindow*>(rp);
rw->SetParentId(hWnd);
}
else {
}
auto a = renderWindow->GetGenericParentId();
HWND pHandle = (HWND)(void*)rw->GetGenericParentId();
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(0, 0.4, 1);
renderWindow->SetSize(400, 400);
renderWindow->Render();
try
{
renderWindowInteractor->Start();
}
catch (...)
{
}
}
renderWindowInteractor->Start();
- From this line code never returns. Stuck at
void vtkWin32RenderWindowInteractor::StartEventLoop();
as the window wasn’t created and no messages are being received
Even without adding interactor, the vtkRenderWindow doesn’t render itself.
Thanks for help,
Elena