Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

Help with WIN32 API

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

Hi its me again with a WIN32 question now my program compiles beatifully without giving any errors at comile time & run time but the thing is , I dont get to see any window.. it just runs and inmediately after the program closes by itself returning me to the compiler heres the code
//---------------------------------------------
//cApplication.h 
//Creates and register main application window
//
//Armando Alva@2005
//---------------------------------------------

#ifndef _CAPPLICATION_H__
#define _CAPPLICATION_H__

#include <windows.h>

class cApplication
{
public:
	// WIN32 API Variables - Made Public for easy access
	HWND hWnd;
	HINSTANCE hInstance;
	MSG msg;
	WNDCLASSEX wcex;

	cApplication();
	~cApplication();
	
	// Class Methods
    void RegisterApp();
	void CreateApp();
	void ShowError(char*);

private:
	// Nothing 
};
	



#endif _CAPPLICATION_H__

//---------------------------------------------
//cApplication.cpp
//Class Implementation File
//
//Armando Alva@2005
//---------------------------------------------

#include "cApplication.h"
//
LRESULT CALLBACK MsgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
	case WM_DESTROY:
		PostQuitMessage(WM_QUIT);
		break;
	default:
		return DefWindowProc(hWnd, Msg, wParam, lParam);
	}

	return 0;
}
//

// Constructor //
cApplication::cApplication()
{
	RegisterApp();
	CreateApp();
}

// Destructor //
cApplication::~cApplication()
{
	// Do Nothing (for now)
}

//---------------------------------------------
// RegisterApp()
// registers main window settings
//---------------------------------------------
void cApplication::RegisterApp()
{
	// Register Window class
	wcex.cbSize			= sizeof(WNDCLASSEX);
	wcex.style			= 0;
	wcex.lpfnWndProc	= MsgProc; //POINTS TO THE MESSAGE PROCEDURE FUNCTION VERY IMPORTANT DO NOT FORGET
	wcex.cbWndExtra		= 0;
	wcex.cbClsExtra		= 0;
	wcex.hInstance		= hInstance; //GetModuleHandle(NULL); // Must be related with WinMain HINSTANCE var
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hbrBackground	= NULL;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "Game Class";

	// check for registration errors
	if(!RegisterClassEx(&wcex)) //RegisterCLassEx if failed returns 0
	{
		ShowError("Window Registration Error!");
		//return 0;
	}

}

//---------------------------------------------
// CreateApp()
// creates already registered window
//---------------------------------------------

void cApplication::CreateApp()
{
	hWnd = CreateWindowEx(NULL, "Game Class", "Double Dragon Clone",
						  WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
						  NULL, NULL, wcex.hInstance, NULL);

	// check for creation errors
	if(hWnd == NULL)
	{
		ShowError("Window Creation Error!");
		//return 0;
	}

	//return 0;
}


//---------------------------------------------
// ShowError(*char)
// Shows Messagebox with desired text
//---------------------------------------------

void cApplication::ShowError(char* error)
{
	MessageBox(NULL, error, "Error!", MB_OK);
}



// WinMain Test

#include <windows.h>
#include "cApplication.h"


INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow )
{
    cApplication testwin;

	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
Your program does exactly what you told it to. Register your window, create the window, end exit.

You need a message loop to process messages. Something like

while (GetMessage(...))
{
TranslateMessage(...);
DispatchMessage(...);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DeadXorAlive
I'm quite sure that you have to call ShowWindow(HWND hWnd, int nCmdShow) to make your window visible.
MSDN, ShowWindow()


Correct. However, that won't do anything without a message loop to process the messages.

Share this post


Link to post
Share on other sites
i think you need to add:

ShowWindow(hWindow, cmdshow);
UpdateWindow(hWindow);
SetFocus(hWindow);

to display the window, and you need a main loop:

while (true)
{
MSG msg;
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//Do main loop here
}
}
}


i think when your program closes you should call:
UnregisterClass(ClassName, hInstance);

Share this post


Link to post
Share on other sites
Yes youre right..
my class its by far unoptimized..

i just wanted to see it compile & run


optimization begins now

thanks a lot for your help


now ill work on a derived class based on cApplication to initialize D3D9
wish me luck ;)

Share this post


Link to post
Share on other sites
You can have a look at the DirectX framework code which does almost exactly what you try to do, I guess. I am not sure though if it is still installed with the current versions of DirectX.

But further: good luck :)

Share this post


Link to post
Share on other sites
By the way, I assumed you were making a plain old windows application, so I suggested GetMessage for your message loop. As you appear to be making a game, you will want to use PeekMessage, as littlekid suggested.

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!