Jump to content
  • Advertisement
Sign in to follow this  
programering

Window doesn't show up

This topic is 4387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The window doesn't show up when I don't call the default DefWindowProc at the end of my WindowProc. Which is strange cos I've created it and showed it with: hWndMain = CreateWindow(...); and ShowWindow(hWndMain,SW_SHOW); How can this come?
// Game.cpp : Defines the entry point for the application.
//

#include "StdAfx.h"
#include "Resource.h"

//#include "Game.h"

#define CLASSNAME "WNDCLASS1"


HINSTANCE hInstance;
HWND hWndMain;


VOID MainGameLoop(VOID);


LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	// Defining WNDCLASS
 	WNDCLASS wndclass;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
	wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_GAMEAPP));
	wndclass.hInstance = hInstance;
	wndclass.lpfnWndProc = WindowProc;
	wndclass.lpszClassName = CLASSNAME;
	wndclass.lpszMenuName = NULL;
	wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

	// Register WNDCLASS
	if (!RegisterClass(&wndclass))
		MessageBox(NULL,"Couldn't register WNDCLASS!!!","Error",MB_OK);

	// Creating the main window
	hWndMain = CreateWindow(CLASSNAME,"Klingis",
	WS_OVERLAPPEDWINDOW | WS_VISIBLE,
	CW_USEDEFAULT, CW_USEDEFAULT,
	CW_USEDEFAULT, CW_USEDEFAULT,
	NULL, NULL, hInstance, NULL);

	// Showing the window
	ShowWindow(hWndMain,SW_SHOW);

	MainGameLoop();

	return 0;
}


LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		break;
	}

	return 0;//DefWindowProc(hWnd,msg,wParam,lParam);
}





VOID MainGameLoop(VOID)
{
	MSG msg;
	BOOL running = TRUE;

	while (running)
	{
		if (PeekMessage(&msg,hWndMain,0,0,PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
                            running = FALSE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Well, you've answered your own question:
Quote:

The window doesn't show up when I don't call the default DefWindowProc

DefWindowProc does a lot of stuff. One of which is to correctly process WM_NCCREATE, the API documentation specifies the return value for the window procedure as:
Quote:

If an application processes this message, it should return TRUE to continue creation of the window. If the application returns FALSE, the CreateWindow or CreateWindowEx function will return a NULL handle.

So you're returning 0 (== FALSE) which stops window creation and causes CreateWindow to return NULL. So, no window, no way to see it.

Skizz

Share this post


Link to post
Share on other sites
Change:

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}

return 0;//DefWindowProc(hWnd,msg,wParam,lParam);
}





To something like:

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}

return 0;
}



Share this post


Link to post
Share on other sites
I removed the DefWindowProc(...) to see if it
handled the WM_PAINT cos the window seem to repainted it self
without me handling the WM_PAINT in my WindowProc(...);

Share this post


Link to post
Share on other sites
Here's the API description for what DefWindowProc does with a WM_PAINT message:
Quote:

The DefWindowProc function validates the update region. The function may also send the WM_NCPAINT message to the window procedure if the window frame must be painted and send the WM_ERASEBKGND message if the window background must be erased.

and here's the description for WM_ERASEBKGND
Quote:

The DefWindowProc function erases the background by using the class background brush specified by the hbrBackground member of the WNDCLASS structure.

So, if you've defined a class background brush in the WNDCLASSEX structure then that will be used to clear the window if you let DefWindowProc handle the WM_PAINT and WM_ERASEBKGND. Try it out by changing the brush value and see what happens.

Skizz

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!