Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sirSolarius

Creating a window in Windows

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

Please help, m_window keeps getting assigned to 0!
//////////////////////////////////////////////////////////////////////////////////////////

// Name: WinMain

// Desc: the entry function for the entire engine.

// Notes:

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	// Start by creating my class

	WindowCreater *wnd=WindowCreater::getInstance();
	wnd->CreateClass("Kutatas", WindowsMsgHandler, hInstance);
	if(! (wnd->SpawnWindow("Kutatas Engine", 200, 200)))
	{
		ExitProgram();		// Window did not create

		return 0;
	}

	g_state=RUNNING;

	MSG msg;		// Message for recieving Windows messages


	while(g_state == RUNNING)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if(g_state==PAUSED)
			g_state=RUNNING;
	}
	
	return 1;	
}
The WindowCreater code is here:
//////////////////////////////////////////////////////////////////////////////////////////

// Name: getInstance

// Desc: singleton retrieval function

// Notes:

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////

WindowCreater *WindowCreater::getInstance()
{
	if(!instance)
	{
		instance=new WindowCreater();
	}
	return instance;
}


//////////////////////////////////////////////////////////////////////////////////////////

// Name: SpawnWindow

// Desc: Creates a new window with title *title

// Notes: Returns 0 if there is an error

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////

HWND WindowCreater::SpawnWindow(char *title, int sizex, int sizey)
{
	DWORD		dwExStyle;				// Window Extended Style

	DWORD		dwStyle;				// Window Style


	dwExStyle=WS_EX_APPWINDOW;								// Window Extended Style

	dwStyle=WS_POPUP;										// Windows Style


	// Create The Window

	m_window = CreateWindowEx(dwExStyle,		// topmost window

                        m_class.lpszClassName,	// class identifier

                        title,				    // window title

                        WS_POPUP | WS_VISIBLE,  // parameters

                        0, 0, sizex, sizey,     // initial position, size

                        NULL,					// handle to parent (the desktop)

                        NULL,                   // handle to menu (none)

                        m_hInstance,            // application instance handle

                        NULL);                 // who needs it?


	ShowWindow(m_window,SW_SHOW);						// Show The Window

	SetForegroundWindow(m_window);						// Slightly Higher Priority

	SetFocus(m_window);									// Sets Keyboard Focus To The Window

	return m_window;
}

void WindowCreater::CreateClass(char *className, WNDPROC msgHandler, HINSTANCE hinstance)
{
	m_hInstance = hinstance;				// Grab An Instance For Our Window


	m_class.cbSize =        sizeof(WNDCLASSEX);           // always use this!

	m_class.style =         CS_DBLCLKS | CS_OWNDC |
								CS_HREDRAW | CS_VREDRAW;       // standard settings

	m_class.lpfnWndProc =   msgHandler;		           // we need to write this!

	m_class.cbClsExtra =    0;                            // extra class info, not used

	m_class.cbWndExtra =    0;                            // extra window info, not used

	m_class.hInstance =     m_hInstance;                    // parameter passed to WinMain()

	m_class.hIcon =         LoadIcon(NULL, IDI_WINLOGO);  // Windows logo

	m_class.hCursor =       LoadCursor(NULL, IDC_ARROW);  // standard cursor

	m_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);  // a simple black brush

	m_class.lpszMenuName =  NULL;                         // no menu

	m_class.lpszClassName = className;			           // class name

	m_class.hIconSm =       LoadIcon(NULL, IDI_WINLOGO);  // Windows logo again


	RegisterClassEx(&m_class);
}

void WindowCreater::Destroy()
{
	if (m_window && !DestroyWindow(m_window))			// Are We Able To Destroy The Window?

	{
		// Window cannot be destroyed

		m_window=NULL;									// Set hWnd To NULL

	}

	if (!UnregisterClass(m_class.lpszClassName,m_hInstance))		// Are We Able To Unregister Class

	{
		// Class cannot be unregistered

		m_hInstance=NULL;									// Set hInstance To NULL

	}
	delete instance;
	instance=NULL;
}

Share this post


Link to post
Share on other sites
Advertisement

HWND WindowCreater::SpawnWindow(char *title, int sizex, int sizey){ DWORD dwExStyle; // Window Extended Style DWORD dwStyle; // Window Style dwExStyle=WS_EX_APPWINDOW; // Window Extended Style dwStyle=WS_POPUP; // Windows Style // Create The Window m_window = CreateWindowEx(dwExStyle, // topmost window m_class.lpszClassName, // class identifier title, // window title WS_POPUP | WS_VISIBLE, // parameters 0, 0, sizex, sizey, // initial position, size NULL, // handle to parent (the desktop) NULL, // handle to menu (none) m_hInstance, // application instance handle NULL); // who needs it? ShowWindow(m_window,SW_SHOW); // Show The Window SetForegroundWindow(m_window); // Slightly Higher Priority SetFocus(m_window); // Sets Keyboard Focus To The Window return m_window;}


Shouldn''t m_hInstance be instance??

Share this post


Link to post
Share on other sites
m_hInstance is a member variable of WindowCreater that gets set in the CreateClass method (and is passed hInstance from WinMain).

Share this post


Link to post
Share on other sites
1. You should check the return value of RegisterClassEx.
2. You should check the return value of CreateWindowEx.
3. When a function fails, you should call GetLastError() to find out why it failed.
4. What does your window procedure look like? It may be that it is inadvertantly telling the WM_CREATE call to fail.

Share this post


Link to post
Share on other sites
quote:
Original post by Dave Hunt
1. You should check the return value of RegisterClassEx.
2. You should check the return value of CreateWindowEx.
3. When a function fails, you should call GetLastError() to find out why it failed.
4. What does your window procedure look like? It may be that it is inadvertantly telling the WM_CREATE call to fail.



Woot! It was #4... I had accidentally trapped the WM_CREATE code instead of passing it to the default message handler.

Thanks a ton!

Share this post


Link to post
Share on other sites

  • 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!