Help with WIN32 API

Started by
7 comments, last by Dave Hunt 18 years, 7 months ago
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;
}


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(...);}

I'm quite sure that you have to call ShowWindow(HWND hWnd, int nCmdShow) to make your window visible.
MSDN, ShowWindow()
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.
Every day I post I learn something new , thanks a lot guys
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);
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 ;)
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 :)

Crafter 2D: the open source 2D game framework

?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]

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.

This topic is closed to new replies.

Advertisement