5 Replies - 890 Views - Last Post: 15 March 2009 - 06:53 PM Rate Topic: -----

#1 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Win32 App Problem

Posted 14 March 2009 - 12:21 PM

Hi everyone, I'm having a bit of an issue with my first win32 program. I'm reading through the book "Beginning Game Programming Second Edition" by Jonathan S. Harbour. He gives an example of a basic win32 application in his book and I can't seem to get it to work. The app is suppose to create a window, then display the text "Hello World" on the window, then display 1000 randomly placed pixels around the window. It actually compiles just fine, but the problem is that it doesn't show the window. It just says the build was successful then doesn't show anything. I'm using VC++ 2005. Any help would be appreciated, thanks.

 / Beginning Game Programming
// Chapter 3
// WindowTest program

//header files to include
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <atlstr.h>

//application title
//#define APPTITLE "Hello World"

//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE,int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);


//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	char *szHello = "Hello World!";
	RECT rt;
	int x, y, n;
	COLORREF c;

	switch (message)
	{
		case WM_PAINT:
			//get the dimensions of the window
			GetClientRect(hWnd, &rt);

			//start drawing on device context
			hdc = BeginPaint(hWnd, &ps);

			//draw some text
			DrawText(hdc, L"Hello World!", strlen(szHello), &rt, DT_CENTER);

			//draw 1000 random pixels
			for (n=0; n<3000; n++)
			{
				x = rand() % (rt.right - rt.left);
				y = rand() % (rt.bottom - rt.top);
				c = RGB(rand()%256, rand()%256, rand()%256);
				SetPixel(hdc, x, y, c);
			}

			//stop drawing
			EndPaint(hWnd, &ps);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	//create the window class structure
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX); 

	//fill the struct with info
	wc.style		 = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)WinProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = NULL;
	wc.hCursor	   = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = L"Hello World";
	wc.hIconSm	   = NULL;

	//set up the window with the class info
	return RegisterClassEx(&wc);
}

//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   //create a new window
   hWnd = CreateWindow(
	   L"Window",				 //window class
	   L"The Title Of My Window", //title bar
	   WS_OVERLAPPEDWINDOW,	   //window style
	   CW_USEDEFAULT,			 //x position of window
	   CW_USEDEFAULT,			 //y position of window
	   500,					   //width of the window
	   400,					   //height of the window
	   NULL,					  //parent window
	   NULL,					  //menu
	   hInstance,				 //application instance
	   NULL);					 //window parameters

   //was there an error creating the window?
   if (!hWnd)
	  return false;

   //display the window
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return true;
}


//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR	 lpCmdLine,
				   int	   nCmdShow)
{
	// declare variables
	MSG msg;

	// register the class
	MyRegisterClass(hInstance);

	//MessageBox(NULL, TEXT("Chris Fibich"), TEXT("My Name"), MB_OK | MB_ICONQUESTION);

	// initialize application
	if (!InitInstance (hInstance, nCmdShow)) 
		return false;

	//set random number seed
	srand(time(NULL));

	// main message loop
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Win32 App Problem

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Win32 App Problem

Posted 14 March 2009 - 03:27 PM

That's odd, you could ask Jonathon Harbour yourself at his forum: http://www.jharbour.com/forum
I got banned from that site long ago for saying Michael Olsen's a nigger killer.
http://policecrimes....opic.php?t=1174

I'm Oblivion on the forums (JHarbour). :)
PS: That's a very fun book.

Quote

Sorry Oblivion, you are banned from posting or sending personal messages on this forum.
Racist and volatile comments


This post: http://theharbourfam...msg3076#msg3076

EDIT: Oh, and can you ask him to unban me now? Been almost two years

This post has been edited by Hyper: 14 March 2009 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 kwest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: Win32 App Problem

Posted 14 March 2009 - 03:44 PM

Hey Fib that is weird. I've done quite a bit of Win32 programming the last couple months
and it seems like it should work. I know its not I've tried compiling your code myself and
other than a couple warnings due to my settings in Visual Studio it Compiled.
I'll have to take a better look over it again later tonight. It's really weird though.
Was This Post Helpful? 0
  • +
  • -

#4 kwest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: Win32 App Problem

Posted 15 March 2009 - 09:26 AM

Here You Go Fib. I'm Still not sure what was wrong with your first one
but I just recreated the Window app and then just reapplied your WM_PAINT
message. Try to copy and paste this into your IDE and build it It worked for me
I did it using Visual Studio 2005.


#define WIN32_LEAN_AND_MEAN // exclude rarely used stuff from windows headers

// Windows Header Files
#include <windows.h>

// C Runtime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <atlstr.h>
#include <time.h>

// Global Variables
const int kWinWid = 500;
const int kWinHgt = 400;
const LPCWSTR kClassName = _T("MyWinClass"); // WNDCLASSEX name
const LPCWSTR kTitleBar  = _T("DreamInCode.Net"); // Main Window Title Bar

// Forward Declarations
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevious, LPSTR lpCmdLine, int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevious);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// TODO: PLACE CODE HERE									
	srand((unsigned int)time(NULL));	//set random number seed
	MSG msg;	// Create a Message Variable

	MyRegisterClass(hInstance);

	// Perform Application Initialization
	if (! InitInstance(hInstance, nCmdShow))
	{
		return FALSE;
	}

	// Main Message Loop
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}

// Function Definition(s)
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex = {0};

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcex.lpszClassName = kClassName;

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	hWnd = CreateWindow(kClassName, kTitleBar, WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, CW_USEDEFAULT, kWinWid, kWinHgt, NULL, NULL,
		hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	LPCWSTR szHello = _T("Hello World");
	RECT rect;
	int x, y, n;
	COLORREF c;

	switch (message)
	{
	case WM_PAINT:
		{
			//get the dimensions of the window
			GetClientRect(hWnd, &rect);

			//start drawing on device context
			hdc = BeginPaint(hWnd, &ps);

			//draw some text
			DrawText(hdc, szHello, lstrlen(szHello), &rect, DT_CENTER);

			//draw 1000 random pixels
			for (n = 0; n < 3000; n++)
			{
				x = rand() % (rect.right - rect.left);
				y = rand() % (rect.bottom - rect.top);
				c = RGB(rand()%256, rand()%256, rand()%256);
				SetPixel(hdc, x, y, c);
			}
			//stop drawing
			EndPaint(hWnd, &ps);
			break;
		}
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	case WM_CLOSE:
		{
			DestroyWindow(hWnd);
			break;
		}
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Win32 App Problem

Posted 15 March 2009 - 06:49 PM

Thanks kwest, your code works great. I'll have to look through it and try to make sense of it. I still find it very odd that the code straight from the book would not work... That kind of scares me because that means the code in the later chapters could not work. Oh well I guess we'll have to see. And Hyper, I posted the same code on J Harbours forum, but didn't get any replies :( Oh well it doesn't matter. Thanks
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Win32 App Problem

Posted 15 March 2009 - 06:53 PM

But did you ask JHarbour if he'd unban me? :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1