🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

WinAPI ShowWindow() doesn't work as expected, why?

Started by
1 comment, last by Shaarigan 3 years, 11 months ago

I've run into this seemingly strange issue while doing some OS side setup. After creating a window I call ShowWindow() and UpdateWindow() as per MSDN. However if I do that the window doesn't update and just remains white until I resize it. The weird thing is that if I don't call the ShowWindow() right after the create method, than it works as expected. It's better illustrated as an example and I'll also post some actual code:

Situation:
HWND mainWindow = CreateWindow(...);
ShowWindow(window, SW_SHOWNORMAL);
UpdateWindow(window);

HWND childWindow = CreateWindow(...);
ShowWindow(childWindow, SW_SHOWNORMAL);
UpdateWindow(childWindow);

The above just shows a blank window and I have to resize it in order for it to start drawing.

However if I use this code:

This works:
HWND mainWindow = CreateWindow(...);
HWND childWindow = CreateWindow(...);

ShowWindow(window, SW_SHOWNORMAL);
ShowWindow(childWindow, SW_SHOWNORMAL);

Now it works, and I don't even have to call the UpdateWindow() method. So just having another CreateWindow call between the call to ShowWindow somehow magically makes it work.

I've read the MSDN page on ShowWindow and it seems pretty quirky like for example it says it ignores the show flag if it's the first time the application calls it, so I'm not sure if that's got anything to do with it but I'm a bit confused why this behavior happens.

Some actual code:

HWND CreateMainWindow(HINSTANCE hInstnace, UINT width, UINT height)
{
	WNDCLASS MainWindowClass;
	MainWindowClass.style = CS_HREDRAW | CS_VREDRAW;
	MainWindowClass.lpfnWndProc = MainWndProc;
	MainWindowClass.cbClsExtra = 0;
	MainWindowClass.cbWndExtra = 0;
	MainWindowClass.hInstance = hInstnace;
	MainWindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
	MainWindowClass.hCursor = LoadCursor(0, IDC_ARROW);
	MainWindowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	MainWindowClass.lpszMenuName = 0;
	MainWindowClass.lpszClassName = L"MainWindowClass";

	if (!RegisterClass(&MainWindowClass))
	{
		MessageBoxW(0, L"Failed to register the window class!", 0, 0);
		PostQuitMessage(0);
	}


	// Compute window rectangle dimensions based on requested client area dimensions.
	UINT inWidth = width;
	UINT inHeight = height;
	RECT R = { 0, 0, inWidth, inHeight };
	AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);
	UINT clientWidth = R.right - R.left;
	UINT clientHeight = R.bottom - R.top;


	HMENU hMenu = LoadMenu(hInstnace, MAKEINTRESOURCE(IDM_MAINMENU));
	return CreateWindowW(L"MainWindowClass",
		L"ComputerVision",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
		clientWidth, clientHeight,
		NULL, hMenu,
		hInstnace,
		0);

}

HWND CreateRenderWindow(HINSTANCE hInstnace, HWND parentWindow)
{
	WNDCLASS RenderWindowClass;
	RenderWindowClass.style = CS_HREDRAW | CS_VREDRAW;
	RenderWindowClass.lpfnWndProc = RenderWndProc;
	RenderWindowClass.cbClsExtra = 0;
	RenderWindowClass.cbWndExtra = 0;
	RenderWindowClass.hInstance = hInstnace;
	RenderWindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
	RenderWindowClass.hCursor = LoadCursor(0, IDC_ARROW);
	RenderWindowClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	RenderWindowClass.lpszMenuName = 0;
	RenderWindowClass.lpszClassName = L"RenderWindowClass";

	if (!RegisterClass(&RenderWindowClass))
	{
		MessageBoxW(0, L"Failed to register the window class!", 0, 0);
		PostQuitMessage(0);
	}

	RECT rc;
	GetClientRect(parentWindow, &rc);

	UINT renderClientWidth = (rc.left - rc.right) - TOOL_PANEL_WIDTH;
	UINT renderClientHeight = rc.bottom - rc.top;

	return CreateWindowW(L"RenderWindowClass",
		L"",
		WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT,
		renderClientWidth, renderClientHeight, parentWindow,
		NULL,
		hInstnace,
		0);

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int nCmdShow)
{
	//create all the program windows
	gMainWindow = CreateMainWindow(hInstance, 800, 600);
	ShowWindow(gMainWindow, 1);
	
	gRenderWindow = CreateRenderWindow(hInstance, gMainWindow);
	ShowWindow(gRenderWindow, 1);
	

	
	/*...*/
	
}

The above doesn't work (displays window but doesn't begin the refresh cycle), however if I do this

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int nCmdShow)
{
	//create all the program windows
	gMainWindow = CreateMainWindow(hInstance, 800, 600);
	gRenderWindow = CreateRenderWindow(hInstance, gMainWindow);
	
	ShowWindow(gMainWindow, 1);
	ShowWindow(gRenderWindow, 1);
	

	
	/*...*/
	
}

This works as expected.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement

Sounds like you are not handling the messages properly, how are your wndProcs looking?

This topic is closed to new replies.

Advertisement