Go to windowed mode in Direct3D 9

930 Views Asked by At

I'm making a Direct3D app, and I can easily go from Windowed to Fullscreen mode using IDirect3DDevice9::Reset with new presentation parameters. However, when I use the same trick to go from fullscreen to windowed mode, the window has now lost its borders.

If I try doing SetWindowLong to set the window style to WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU (and then SetWindowPos with SWP_FRAMECHANGED), the window now gets its border, but the direct3d device no longer works. Resetting the device again doesn't work, and instead of Reset(), doing Release() then SetWindowLong() then CreateDevice() again, of course fails, as my managed resources are dependent on my device.

How do I make IDirect3DDevice9::Reset to go back to windowed mode, while creating a bordered window?

1

There are 1 best solutions below

0
On

First, you need to change the properties of the window:

SetWindowLongPtr(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);

if (new_pos_size)
{
    // if you want new position (pos_x, pos_y) and size (width, height)
    UINT flags = SWP_FRAMECHANGED | SWP_SHOWWINDOW;
    SetWindowPos(hWnd, HWND_NOTOPMOST, pos_x, pos_y, width, height, flags);
}
else
{
    UINT flags = SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_SHOWWINDOW;
    SetWindowPos(hWnd, 0, 0, 0, 0, 0, flags);
}

Next you have to release any resources you created in default pool - D3DPOOL_DEFAULT (it's better to use D3DPOOL_MANAGED if possible). If you don't, IDirect3DDevice9::Reset will fail.
Then you can reset the device and finally recreate any resources if needed. Make sure you set up D3DPRESENT_PARAMETERS for IDirect3DDevice9::Reset correctly.