Jump to content
  • Advertisement
Sign in to follow this  
Daivuk

Problem creating my Win32 window

This topic is 4893 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

I there ! I'm VERY desasperate... I'm starting my new engine duk7. So its the 7th time i'm creating a window. But for some reason it dont work :|! I passed the last two days to search for the problem and always the same error. I of course compare it to my previous engines and its the SAME. Except for the global architecture of the engine. Here is the problem : When calling : CreateWindow or CreateWindowEx, it return me a NULL handle (HWND). I'm calling GetLastError() and output this to my console log file, and the error is : ERROR_INVALID_HANDLE 6 The handle is invalid. I searched on google and find only 3 sites about "createwindow" and this error. And nothing to do with me :| I tried cleaning the project an a lot of thing... So know, i'm putting the code here, and some of you will probably see what I'm not seeing!!! Thanks for help guys ;) here the hole .cpp file that create my window : (sorry the comments are in french, from montreal ;))

#include "CWindow.h"
#include "CConfig.h"
#include "CApplication.h"



CWindow::_typGlobalWindowID* CWindow::_typGlobalWindowID::list = 0;


//
// Constructeurs / destructeur
//
CWindow::CWindow(const CString& mName)
{
	name = mName;
	windowClass = 0;
	globalWindowID = 0;
	windowHandle = 0;
}

CWindow::~CWindow()
{
	if (windowClass) delete windowClass;
	if (globalWindowID) delete globalWindowID;
}



//
// Création de la fenetre
//
int CWindow::create()
{
	// La profondeur des couleurs
	colorBit = CONFIG->v_colorBit;

	// Bon, on défini la résolution avec les config
	size = CONFIG->v_resolution;

	// On check si on est en full screen ou pas
	if (CONFIG->v_fullscreen)
	{
		position = CPoint2i(0,0);
	}
	else
	{
		// On pogne le screen resolution
#ifdef OS_WIN32
		position._X = GetSystemMetrics(SM_CXSCREEN)/2 - size._X/2;
		position._Y = GetSystemMetrics(SM_CYSCREEN)/2 - size._Y/2;
#endif
	}

	// On génère un nouveau ID
	globalWindowID = new _typGlobalWindowID();
	globalWindowID->window = this;

#ifdef OS_WIN32
	// On cré la class de la fenetre
	windowInstance = GetModuleHandle(NULL);
	windowClass = createWindowClass(CString("WinClassDuk7%02i", (int)(globalWindowID->ID)));

	// On check si elle est valide
	if (!windowClass)
	{
		DWORD error = GetLastError();

		PRINT(CString("Error > can not create window class\n      > Win32 API error code : %i", error));
		return 0;
	}

	// On check si on doit aller en fullscreen
	DWORD dwExStyle;
	DWORD dwStyle;
	if (CONFIG->v_fullscreen)
	{
		// On set les cossains pour le fullscreen
		DEVMODE dmScreenSettings;
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);
		dmScreenSettings.dmPelsWidth	= size._W;
		dmScreenSettings.dmPelsHeight	= size._H;
		dmScreenSettings.dmBitsPerPel	= colorBit;
		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

		// On switch en fullscreen
		if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
		{
			PRINT(CString("Error > failed to change display settings\n      > Defaulting to window Mode"));
			CONFIG->v_fullscreen = 0;

			// On set le style de fenetre pour WINDOW MODE
			dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
			dwStyle = WS_OVERLAPPEDWINDOW;
		}
		else
		{
			// On set le style de fenetre pour FULL SCREEN
			dwExStyle = WS_EX_APPWINDOW;
			dwStyle = WS_POPUP;
		}
	}
	else
	{
		// On set le style de fenetre pour WINDOW MODE
		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwStyle = WS_OVERLAPPEDWINDOW;
	}

	// Ajuster le rectangle de la fenetre
	RECT windowRect;
	windowRect.left = position._X;
	windowRect.top = position._Y;
	windowRect.right = position._X + size._W;
	windowRect.bottom = position._Y + size._H;
	if (AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle) == 0) 
	{
		DWORD error = GetLastError();
		if (CONFIG->v_fullscreen) ChangeDisplaySettings(NULL,0);
		PRINT(CString("Error > can not ajust window's rect\n      > Win32 API error code : %i", error));
		return 0;
	}

	// On cré la fenetre
	windowHandle = CreateWindow(CString("WinClassDuk7%02i", (int)(globalWindowID->ID)).string, 
		name.string, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
		windowRect.left, windowRect.top, (long)(size._W), (long)(size._H), 
		NULL, NULL, windowInstance, NULL);

	globalWindowID->handle = windowHandle;

	// On check si la création de la fenetre a fonctionné
	if (windowHandle == NULL)
	{
		DWORD error = GetLastError();
		if (CONFIG->v_fullscreen) ChangeDisplaySettings(NULL,0);
		PRINT(CString("Error > can not create window\n      > Win32 API error code : %i", error));
		return 0;
	}

	// On affiche la fenetre finalement
	ShowWindow(windowHandle,SW_SHOW);
	SetForegroundWindow(windowHandle);
	SetFocus(windowHandle);
#endif

	return 1;
}



#ifdef OS_WIN32
//
// Pour créer la class de la fenetre
//
WNDCLASS* CWindow::createWindowClass(CString className)
{
	WNDCLASS* newWinClass = new WNDCLASS;
	newWinClass->style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	newWinClass->lpfnWndProc = (WNDPROC) WindowProc; // La procédure window
	newWinClass->cbClsExtra = 0;
	newWinClass->cbWndExtra = 0;
	newWinClass->hInstance = windowInstance;
	newWinClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
	newWinClass->hCursor = LoadCursor(NULL, IDC_ARROW);
	newWinClass->hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH); // Fond gris
	newWinClass->lpszMenuName = NULL;
	newWinClass->lpszClassName = className.string;

	// Enregistrer la classe
	if (RegisterClass(newWinClass)==0) 
	{
		DWORD error = GetLastError();
		delete newWinClass;
		newWinClass = 0;

		PRINT(CString("Error > can not register window's class\n      > Win32 API error code : %i", error));
		return 0;
	}

	return newWinClass;
}



//
// La winproc appelé par toute les fenetre
//
LRESULT CALLBACK CWindow::WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return 0;
}
#endif



Share this post


Link to post
Share on other sites
Advertisement
I'm very sorry, i just found it!!!
That can be usefull if someone have the same problem :

I was returning 0 in my winproc.
That was logic... Because the windowproc is called on the creation of the window and I returned an invalid handle 0 (NULL) myself !
I had to know this :S


//
// La winproc appelé par toute les fenetre
//
LRESULT CALLBACK CWindow::WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc (hwnd,uMsg,wParam,lParam);
// return 0;
}

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!