I have a problem with Winapi window. I have an app, which might (but not necessarily) start in fullscreen mode, and if it does, I cannot have an empty window shown before switching to fullscreen. So, what I am doing is:
- Create window
Call
ShowWindow(HWnd, SW_HIDE); UpdateWindow(HWnd);
Read settings from file
Reset D3D renderer, adjust window, possilby switch to fullscreen, and call
SetWindowPos(HWnd, HWND_NOTOPMOST, posx, posy,sizex, sizey, SWP_SHOWWINDOW); ShowWindow(HWnd, SW_SHOW);
In fullscreen mode everything is fine, but when app starts in windowed mode there's simply no window at all - CreateWindowEx
returns proper window handle, app is running in background and everything, but there's no window to be found. What am I forgetting?
edit
Ok, some progress. It turned out SetWindowPos in D3D renderer reset routine wasn't called. I fixed that and now there's new problem - window is where it's supposed to, but it's completely transparent, nothing is displayed in client area, except for cursor.
In fullscreen everything works fine. If app starts in fullscreen and user switches to windowed everything is also fine.
edit2
OK, I managed to locate the reason why there's nothing rendered in the window - IDirect3DDevice9::BeginScene
method returns D3DERR_INVALIDCALL
, so apparently there's something wrong with resetting D3D renderer.
void VD3D9Renderer::Reset(bool windowed, ulong width, ulong height)
{
for (size_t i=0; i<Fonts.size(); i++)
Fonts[i]->Font->OnLostDevice();
if (FontMgr)
FontMgr->OnLostDevice();
for (VMap<VString, VD3D9Texture*>::iterator it = TextureMap.begin(); it != TextureMap.end(); it++)
{
VD3D9Texture* tex = it->second;
if (tex && tex->IsRenderTarget())
tex->TexturePtr->Release();
}
D3DD->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &BackBufferColor);
D3DD->GetDepthStencilSurface(&BackBufferDepthStencil);
if (BackBufferColor) BackBufferColor->Release();
if (BackBufferDepthStencil) BackBufferDepthStencil->Release();
bool change_fs = (windowed==true && D3DPP.Windowed == FALSE) || (windowed==false && D3DPP.Windowed == TRUE) ? true : false;
D3DPP.Windowed = windowed;
if (width) D3DPP.BackBufferWidth = width;
if (height) D3DPP.BackBufferHeight = height;
D3DD->Reset(&D3DPP);
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = D3DPP.BackBufferWidth;
rc.bottom = D3DPP.BackBufferHeight;
if (change_fs)
{
if(D3DPP.Windowed)
{
DWORD style = GetWindowLong(HWnd, GWL_STYLE);
DWORD exstyle = GetWindowLong(HWnd, GWL_EXSTYLE);
style = WS_CAPTION | WS_SYSMENU;
exstyle = 0;
int posx = (GetSystemMetrics(SM_CXSCREEN) - (rc.right-rc.left)) >> 1;
int posy = 0;
AdjustWindowRectEx(&rc, style, false, exstyle);
SetWindowLong(HWnd, GWL_STYLE, style);
SetWindowLong(HWnd, GWL_EXSTYLE, exstyle);
if(!SetWindowPos(HWnd, HWND_NOTOPMOST, posx, posy, rc.right, rc.bottom, SWP_SHOWWINDOW))
{
DWORD error = GetLastError();
VLog("Error in SetWindowPos: %u\n", error);
}
ShowWindow(HWnd, SW_SHOW);
}
else
{
DWORD style = GetWindowLong(HWnd, GWL_STYLE);
DWORD exstyle = GetWindowLong(HWnd, GWL_EXSTYLE);
style = WS_POPUP;
exstyle = 0;
int posx = (GetSystemMetrics(SM_CXSCREEN) - (width)) >> 1;
int posy = 0;
SetWindowLong(HWnd, GWL_STYLE, style);
SetWindowLong(HWnd, GWL_EXSTYLE, exstyle);
if(!SetWindowPos(HWnd, HWND_NOTOPMOST, posx, posy, rc.right, rc.bottom, SWP_SHOWWINDOW))
{
DWORD error = GetLastError();
VLog("Error in SetWindowPos: %u\n", error);
}
ShowWindow(HWnd, SW_SHOW);
}
}
else
{
if (rc.right > 0 && rc.bottom > 0)
{
DWORD style = GetWindowLong(HWnd, GWL_STYLE);
AdjustWindowRect(&rc, style, false);
int posx = (GetSystemMetrics(SM_CXSCREEN) - (rc.right-rc.left)) >> 1;
int posy = 0;
if(!SetWindowPos(HWnd, HWND_NOTOPMOST, posx, posy, rc.right, rc.bottom, SWP_SHOWWINDOW))
{
DWORD error = GetLastError();
VLog("Error in SetWindowPos: %u\n", error);
}
//SetWindowPos(HWnd, HWND_NOTOPMOST, posx, posy, rc.right, rc.bottom, SWP_SHOWWINDOW);
ShowWindow(HWnd, SW_SHOW);
}
}
for (VMap<VString, VD3D9Texture*>::iterator it = TextureMap.begin(); it != TextureMap.end(); it++)
{
VD3D9Texture* tex = it->second;
if (tex && tex->IsRenderTarget())
D3DD->CreateTexture(tex->Width, tex->Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, &tex->TexturePtr, NULL);
}
for (size_t i=0; i<Fonts.size(); i++)
Fonts[i]->Font->OnResetDevice();
if (FontMgr)
FontMgr->OnResetDevice();
ResetDeviceState();
}