Basic Win32

I'm currently reading a book (beginning game programming) and I'm having some issues with a program compiling but I'm not getting any output. I don't have any base of windows programming. I did try to debug but I couldn't find where the issue was. In the output window the compiler tells me a bunch of .dll loaded and the last line is
The program '[5940] WindowTest.exe: Native' has exited with code 0 (0x0).
. Any help would be greatly appreciated.
This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//Beginning Game Programing
//Chapter 3
// WindowTest Program

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

//application title
#define APPTITLE "Hello World"

//functino prototypes (foward 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, szHello, strlen(szHello), &rt, DT_CENTER);

		//draw 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 setup the window properties
ATOM  MyRegisterClass(HINSTANCE hInstance)
{
	//create a 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.hbrBackground	= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= NULL;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszClassName	= APPTITLE;
	wc.hIconSm			= NULL;

	//setup 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(
		APPTITLE,			//window class
		APPTITLE,			//title bar
		WS_OVERLAPPEDWINDOW,//window style
		CW_USEDEFAULT,		//x position
		CW_USEDEFAULT,		//y position
		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);

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

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

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


Just a quick perusal of your code reveals one error right off the bat.

You have defined your windows class as WNDCLASSEX but your window creation is plain WNDCLASS. The easiest thing to do is change CreateWindow to CreateWindowEx and insert into the first paramenter WS_EX_CLIENTEDGE.

Put this BEFORE the first paramenter you have now, but keep it and all the others as well.

See if it compiles now. If not, post the exact code again and we'll go from there.
you know Im not getting any errors. Can it be because I'm not using unicode? I changed char set to multi-byte. If I use unicode character set I get 3 errors:

1
2
3
Error	1	error C2664: 'DrawTextW' : cannot convert parameter 2 from 'char *' to 'LPCWSTR'	c:\users\v\documents\visual studio 2008\projects\begining game programing\windowtest\windowtest\main.cpp	38	WindowTest
Error	2	error C2440: '=' : cannot convert from 'const char [12]' to 'LPCWSTR'	c:\users\v\documents\visual studio 2008\projects\begining game programing\windowtest\windowtest\main.cpp	74	WindowTest
Error	3	error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'	c:\users\v\documents\visual studio 2008\projects\begining game programing\windowtest\windowtest\main.cpp	98	WindowTest
Last edited on
if you use unicode you can't use this...

char str[] = "my string";

rather, with unicode you use this...

TCHAR str[] = _T("my string");

If I were you I'd just keep your project as multi-byte and use the first method.
ok I will try that then... I will post back
so I tried the code from the book (the one on the CD) and it compiled. I dunno why mine did not since I typed just like in the book. I guess I will have to print my code and compare.

cd code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Beginning Game Programming
// Chapter 3
// WindowTest program

//header files to include
#include <windows.h>
#include <stdlib.h>
#include <time.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, szHello, 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 = APPTITLE;
    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(
       APPTITLE,              //window class
       APPTITLE,              //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);

    // 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;
}

That code looks fine. I was wrong about CreateWindow being an error. It's only an error if you try to register a plain WNDCLASS when you declared a WNDCLASSEX structure, but if you declare a WNDCLASSEX structure you should go ahead and use CreateWindowEx().

If your CD code compiled and your typed code didn't, then you made a typo. It's that simple.
I must have... It sucks that I gotta wait till next yr for my windows programming class. Win32 is complex an the book just covers the very basics then jumps into DX9
Topic archived. No new replies allowed.