Sign in to follow this  
chubbc

Windows application on Visual C++ express 2008

Recommended Posts

Hi, I am a fairly big noob when it come to game development and have started to get back into it recently. I was using Code blocks but have recently changed to visual C++ express 2008. I had made a few windows program in code blocks and was becoming familiar with the library. I created a standard win32 program as per the Visual C++ templates and found it too be a lot bigger and more complicated that I needed so I just entered the frame of a windows application and various issues arose whilst attempting to compile it. The following is the code I used:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>

LRESULT CALLBACK MsgHandler(
    HWND hwnd,
    UINT msg,
    WPARAM wparam, 
    LPARAM lparam 
);

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX sampleClass;

	sampleClass.cbSize =        sizeof(WNDCLASSEX);
	sampleClass.style =         CS_DBLCLKS | CS_OWNDC |
								CS_HREDRAW | CS_VREDRAW;
	sampleClass.lpfnWndProc =   MsgHandler;                   
	sampleClass.cbClsExtra =    0;          
	sampleClass.cbWndExtra =    0;
	sampleClass.hInstance =     hinstance;
	sampleClass.hIcon =         LoadIcon(NULL, IDI_WINLOGO); 
	sampleClass.hCursor =       LoadCursor(NULL, IDC_ARROW);  
	sampleClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 
	sampleClass.lpszMenuName =  NULL;                  
	sampleClass.lpszClassName = (LPCWSTR)"Sample Class";       
	sampleClass.hIconSm =       LoadIcon(NULL, IDI_WINLOGO);  

	RegisterClassEx(&sampleClass);
	HWND hwnd;
	if (!(hwnd = CreateWindowEx(NULL,              
                            (LPCWSTR)"Sample Class", 
                            (LPCWSTR)"Sample Window",    
                            WS_POPUP | WS_VISIBLE,
                            0, 0, 320, 240,
                            NULL, 
                            NULL,
                            hinstance,    
                            NULL)))        
		return(0);
	MSG msg;
	for( ; ; ){
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT) break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

}

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    return(DefWindowProc(hwnd, msg, wparam, lparam));
}
As you can probably noticed I had to typecast the string to shut the compiler up but now when I run this it compiles fine but merely just makes the top left corner of my screen black and doesnt close or respond in any way. Thanks for your help in advanced

Share this post


Link to post
Share on other sites
Do NEVER cast the string type.

1)
Your project is set to Unicode, you can use the L makro to let the compiler treat the string literals as Unicode:

sampleClass.lpszClassName = L"Sample Class";

2)
Also, your app never ends because there is no WM_QUIT generated. To do this you need to call PostQuitMessage. Best place would be inside either WM_CLOSE or WM_DESTROY.

3)
You told your window to look black and without border, if you want a caption and a border, you need to pass the according styles.

4)
Your WinMain is missing a final return at the end, the compiler ought to issue a warning.


All those points fixed should look like this:


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>

LRESULT CALLBACK MsgHandler(
HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam
);

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX sampleClass;

sampleClass.cbSize = sizeof(WNDCLASSEX);
sampleClass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
sampleClass.lpfnWndProc = MsgHandler;
sampleClass.cbClsExtra = 0;
sampleClass.cbWndExtra = 0;
sampleClass.hInstance = hinstance;
sampleClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
sampleClass.hCursor = LoadCursor(NULL, IDC_ARROW);
sampleClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
sampleClass.lpszMenuName = NULL;
sampleClass.lpszClassName = L"Sample Class";
sampleClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

RegisterClassEx(&sampleClass);
HWND hwnd;
if (!(hwnd = CreateWindowEx(NULL,
L"Sample Class",
L"Sample Window",
WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
0, 0, 320, 240,
NULL,
NULL,
hinstance,
NULL)))
return(0);
MSG msg;
for( ; ; ){
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;

}

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if ( msg == WM_CLOSE )
{
PostQuitMessage( 0 );
}
return(DefWindowProc(hwnd, msg, wparam, lparam));
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this