# Open VTK-9.2.6 as child window inside existing c++ Windows application

**URL:** https://discourse.vtk.org/t/open-vtk-9-2-6-as-child-window-inside-existing-c-windows-application/11761
**Category:** Support
**Tags:** code
**Created:** [June 19, 2023, 11:26am UTC](https://discourse.vtk.org/t/open-vtk-9-2-6-as-child-window-inside-existing-c-windows-application/11761 "2023-06-19T11:26:57Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ElenaChu](https://discourse.vtk.org/user_avatar/discourse.vtk.org/elenachu/32/7290_2.png) [@ElenaChu](https://discourse.vtk.org/u/ElenaChu)
#### Post date: [June 19, 2023, 11:26am UTC](https://discourse.vtk.org/t/open-vtk-9-2-6-as-child-window-inside-existing-c-windows-application/11761/1 "2023-06-19T11:26:57Z")

</div>

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](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](https://stackoverflow.com/questions/72998500/how-to-add-a-win32-vtk-client-window-inside-any-existing-app-already-having-a-ma) . 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
