i cant get my buttons to show from charles petzold's book

what is wrong with my 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "windows.h"

struct
{
	int iStyle;
	TCHAR * szText;
}
button[] = {
			BS_PUSHBUTTON,		TEXT ("PUSHBUTTON"),
			BS_DEFPUSHBUTTON,	TEXT ("DEFPUSHBUTTON"),
			BS_CHECKBOX,		TEXT ("CHECKBOX"),
			BS_AUTOCHECKBOX,	TEXT ("AUTOCHECKBOX"),
			BS_RADIOBUTTON,		TEXT ("RADIOBUTTON"),
			BS_3STATE,			TEXT ("3STATE"),
			BS_AUTO3STATE,		TEXT ("AUTO3STATE"),
			BS_GROUPBOX,		TEXT ("GROUPBOX"),
			BS_AUTORADIOBUTTON,	TEXT ("AUTORADIO"),
			BS_OWNERDRAW,		TEXT ("OWNERDRAW")
		   };

#define NUM ( sizeof button / sizeof button [0] )

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT ("button");
	HWND hwnd;
	MSG msg;
	WNDCLASS my_program;

	my_program.style			= CS_HREDRAW | CS_VREDRAW;
	my_program.lpfnWndProc		= WndProc;
	my_program.cbClsExtra		= 0;
	my_program.cbWndExtra		= 0;
	my_program.hInstance		= hInstance;
	my_program.hIcon			= LoadIcon (NULL, IDI_APPLICATION);
	my_program.hCursor			= LoadCursor (NULL, IDC_ARROW);
	my_program.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	my_program.lpszMenuName		= NULL;
	my_program.lpszClassName	= szAppName;

	if (!RegisterClass (&my_program))
	{
		MessageBox (NULL, TEXT ("Program doesn't register"), szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow (
						 szAppName,					//Window class name
						 TEXT ("Button"),		//Window Caption
						 WS_OVERLAPPEDWINDOW,		//Window style 
						 120,						//initial x position
						 120,						//initial y position
						 500,						//initial x size
						 500,						//initial y size
						 NULL,						//parent window handle
						 NULL,						//window menu handle
						 hInstance,					//program instance handle
						 NULL						//creation parameters
						);

	ShowWindow (hwnd, iCmdShow);
	UpdateWindow (hwnd);

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

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HWND hwndButton [NUM];
	static RECT rect;
	static TCHAR szTop[]	= TEXT ("message			wParam			lParam"),
				 szUnd[]	= TEXT ("_______			______			______"),
				 szFormat[] = TEXT ("%-16s%04X-%04X		%04X-%04X"),
				 szBuffer[50];

	static int cxChar, cyChar;
	
	HDC hdc;
	PAINTSTRUCT ps;
	int i;

	switch (message)
	{
	case WM_CREATE:
		cxChar = LOWORD (GetDialogBaseUnits ());
		cyChar = HIWORD (GetDialogBaseUnits ());

		for (i = 0; i < NUM; i++)
		{
			hwndButton[i] = CreateWindow (
										  TEXT ("button"),
										  button[i].szText,
										  WS_CHILD | WS_VISIBLE | button[i].iStyle,
										  cxChar, cyChar * (1 + 2 * i),	//initial window position
										  20 * cxChar, 7 * cyChar / 4,		//initial window size
										  hwnd, (HMENU) i,
										  ((LPCREATESTRUCT) lParam) -> hInstance, NULL
										 );
		}

		return 0;

	case WM_SIZE:
		rect.bottom = HIWORD (lParam);
		rect.top = 2 * cyChar;
		rect.left = 24 * cxChar;
		rect.right = LOWORD (lParam);

		return 0;

	case WM_PAINT:
		InvalidateRect (hwnd, &rect, TRUE);

		hdc = BeginPaint (hwnd, &ps);
		SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
		SetBkMode (hdc, TRANSPARENT);

		TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop));
		TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd));

		EndPaint (hwnd, &ps);

		return 0;

	case WM_DRAWITEM:

	case WM_COMMAND:
		ScrollWindow (hwnd, 0, -cyChar, &rect, &rect);

		hdc = GetDC (hwnd);
		SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));

		TextOut (
				 hdc,
				 24 *cxChar,
				 cyChar * (rect.bottom / cyChar - 1),
				 szBuffer,
				 wsprintf (
						   szBuffer,
						   szFormat,
						   message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM"):
						   							TEXT ("WM_COMMAND"),
						   HIWORD (wParam), LOWORD (wParam),
						   HIWORD (lParam), LOWORD (lParam)
						  )
				);

		ReleaseDC (hwnd, hdc);
		ValidateRect (hwnd, &rect);
		break;
		
	case WM_DESTROY:
		PostQuitMessage (0);
		return 0;
	}
	return DefWindowProc (hwnd, message, wParam, lParam);
}
Put some effort into asking a question.

- Is the code compiling?
-- if not, are you getting errors?
-- if so, what are the errors and what line are they on?

- Is the program crashing?
-- if so, explain how you recreate the crash
-- describe the crash. Does it just close expectedly? Do you get an access violation?

- Is the code running but just not doing what you expect?
-- if so, explain what behavior you expect, and what behavior you're getting.



It's hard enough solving the problem. We shouldn't have to pull teeth to find out what the problem is.
it compiles fine.

when i run the program the buttons dont show like it does on the book. other stuff are fine
I remember that Petzold program. What ide/dev environment are you using? If I have it I'll give it a try.
sorry i m amature at this, whats ide and dev?

i m using MS visual studio 2008 tho

thanks
u answered the question... the ide is the integrated development environment (for example visual studio) dev ist development^^...

i did tr running your programm... and same problem to me... the buttons won´t show...
The program you got there don't work for me either, but the program from the book does.

Here is the program from the book:
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
146
147
/*----------------------------------------
   BTNLOOK.C -- Button Look Program
                (c) Charles Petzold, 1998
  ----------------------------------------*/

#include <windows.h>

struct
{
     int     iStyle ;
     TCHAR * szText ;
}
button[] =
{
     BS_PUSHBUTTON,      TEXT ("PUSHBUTTON"),
     BS_DEFPUSHBUTTON,   TEXT ("DEFPUSHBUTTON"),
     BS_CHECKBOX,        TEXT ("CHECKBOX"), 
     BS_AUTOCHECKBOX,    TEXT ("AUTOCHECKBOX"),
     BS_RADIOBUTTON,     TEXT ("RADIOBUTTON"),
     BS_3STATE,          TEXT ("3STATE"),
     BS_AUTO3STATE,      TEXT ("AUTO3STATE"),
     BS_GROUPBOX,        TEXT ("GROUPBOX"),
     BS_AUTORADIOBUTTON, TEXT ("AUTORADIO"),
     BS_OWNERDRAW,       TEXT ("OWNERDRAW")
} ;

#define NUM (sizeof button / sizeof button[0])

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("BtnLook") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Button Look"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     while (GetMessage (&msg, NULL, 0, 0))
     {  
   TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static HWND  hwndButton[NUM] ;
     static RECT  rect ;
     static TCHAR szTop[]    = TEXT ("message            wParam       lParam"),
                  szUnd[]    = TEXT ("_______            ______       ______"),
                  szFormat[] = TEXT ("%-16s%04X-%04X    %04X-%04X"),
                  szBuffer[50] ;
     static int   cxChar, cyChar ;
     HDC          hdc ;
     PAINTSTRUCT  ps ;
     int          i ;
     
     switch (message)
     {
     case WM_CREATE :
          cxChar = LOWORD (GetDialogBaseUnits ()) ;
          cyChar = HIWORD (GetDialogBaseUnits ()) ;
          
          for (i = 0 ; i < NUM ; i++)
               hwndButton[i] = CreateWindow ( TEXT("button"), 
                                   button[i].szText,
                                   WS_CHILD | WS_VISIBLE | button[i].iStyle,
                                   cxChar, cyChar * (1 + 2 * i),
                                   20 * cxChar, 7 * cyChar / 4,
                                   hwnd, (HMENU) i,
                                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
          return 0 ;

     case WM_SIZE :
          rect.left   = 24 * cxChar ;
          rect.top    =  2 * cyChar ;
          rect.right  = LOWORD (lParam) ;
          rect.bottom = HIWORD (lParam) ;
          return 0 ;

     case WM_PAINT :
          InvalidateRect (hwnd, &rect, TRUE) ;
          
          hdc = BeginPaint (hwnd, &ps) ;
          SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
          SetBkMode (hdc, TRANSPARENT) ;
          
          TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop)) ;
          TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd)) ;
          
          EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_DRAWITEM :
     case WM_COMMAND :
          ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
          
          hdc = GetDC (hwnd) ;
          SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
          
          TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
                   szBuffer,
                   wsprintf (szBuffer, szFormat,
                         message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM") : 
                                                  TEXT ("WM_COMMAND"),
                         HIWORD (wParam), LOWORD (wParam),
                         HIWORD (lParam), LOWORD (lParam))) ;
          
          ReleaseDC (hwnd, hdc) ;
          ValidateRect (hwnd, &rect) ;
          break ;
          
     case WM_DESTROY :
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
thanks for your help guys
It took me so long to figure out why the original program didn't work.
It is a bit of confusion here:

It is because in the original program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	static TCHAR szAppName[] = TEXT ("button");// <<<< << 
	HWND hwnd;
	MSG msg;
	WNDCLASS my_program;

	my_program.style			= CS_HREDRAW | CS_VREDRAW;
	my_program.lpfnWndProc		= WndProc;
	my_program.cbClsExtra		= 0;
	my_program.cbWndExtra		= 0;
	my_program.hInstance		= hInstance;
	my_program.hIcon			= LoadIcon (NULL, IDI_APPLICATION);
	my_program.hCursor			= LoadCursor (NULL, IDC_ARROW);
	my_program.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	my_program.lpszMenuName		= NULL;
	my_program.lpszClassName	= szAppName;///ERROR - <<<< Main Window Class type is "button" 

?????? why does it make it not work? isnt it just a name????

thanks again

oooh is it because my define is already button so the name cant be the same
Last edited on
What it means is that the main window is a "button" - not only that it also uses the windows procedure (WndProc) that you provided - which means that you have overridden the standard windows procedure for the "button" class - which means you are responsible for the colouring and shading and operation of all buttons (which you haven't done).

Also, each time the WM_CREATE function was creating a button, your window proceedure was being called with a WM_CREATE message, which created another button,....... so your code was creating a button inside a button inside a button inside a button............
If your variables weren't static then you would have had an infinite cycle.

If you retry your original code, but change the CreateWindow call (in the WM_CREATE message) to include the WS_BORDER style - you will see exactly what I mean.

i finally get u

thankss
Topic archived. No new replies allowed.