Win API/How to use button

Hi guys. This is my first attempt at Win API. I am trying to mess with some of the sourcecode provided here at this site. And I am desperately in need of assistance. What I want to do is when I press a button. The button change a text. I trieded, and fail miserably, some help would be great. :)

I just wanted to try something simple to begin with. Is that when I press the button
: ID_BUTTON, or "calculate". The text in static_label. Would change to something else. It be great if someone can fix and point me to the right direction. TY. This is my code so far. I didn't get any errors--so I suppose there is a logic error somewhere. :(

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
#include "windows.h"

enum { ID_LABEL = 1,ID_IMAGE,ID_EDIT,ID_LIST,ID_BUTTON,ID_COMBO, ID_BUTTON2 };
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 

HINSTANCE g_hInst;
TCHAR	szAppName[]	= TEXT("SDI Frame");
TCHAR	szWinClass[]	= TEXT("WIN_CLASS");
HWND static_label;
HWND edit;
HWND button;

int WINAPI WinMain(HINSTANCE hInstance, 
		HINSTANCE hPrevInstance,
		PSTR lpCmdLine,
		int nCmdShow){
						
	MSG			msg; 
	WNDCLASS	wc;	
	HWND		hwnd; 
	g_hInst = hInstance;
	
	wc.cbClsExtra		= 0; 
	wc.cbWndExtra		= 0; 
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1); 
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW); 
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 
	wc.hInstance		= hInstance; 
	wc.lpfnWndProc		= WinProc; 
	wc.lpszClassName	= szWinClass; 
	wc.lpszMenuName		= NULL; 
	wc.style			= CS_HREDRAW | CS_VREDRAW; 
 
 	if( !RegisterClass(&wc)) 
	{
		MessageBox(NULL, TEXT("Error registering class"), TEXT("ERROR"), MB_OK);
		return 0;
	}

	hwnd = CreateWindow(szWinClass,
						szAppName, 
						WS_OVERLAPPEDWINDOW, 
						CW_USEDEFAULT, 
						CW_USEDEFAULT, 
						300, 
						400, 
						(HWND) NULL, 
						(HMENU) NULL, 
						(HINSTANCE) hInstance, 
						(void *) NULL); 

	ShowWindow(hwnd, nCmdShow); 
	UpdateWindow(hwnd);	

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

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT	ps;
	HDC			hdc;
	switch(msg){
	case WM_CREATE:
		static_label = CreateWindow("Static",
"Please Enter A Number",WS_CHILD | WS_VISIBLE,40,15,200,25,hwnd,0,g_hInst,0);	
		edit = CreateWindow("Edit", NULL
, WS_BORDER | NULL | WS_CHILD | WS_VISIBLE | NULL | NULL ,40,45,200,20,hwnd,(HMENU) NULL,g_hInst,0);
		button = CreateWindow("Button","Calculate",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,140,70,100,25,hwnd,(HMENU)ID_BUTTON,g_hInst,0);	
		case WM_PAINT: 
		hdc = BeginPaint(hwnd, &ps);
		EndPaint(hwnd, &ps);
		return 0;
	case ID_BUTTON:
		{
		// code
		SendMessage(static_label,LB_ADDSTRING,255,(LPARAM)(LPCSTR)"You clicked the button");
		ShowWindow(static_label,SW_SHOW);
		}
					break;	
	case WM_DESTROY: 
		PostQuitMessage(0);
		break; // pass to DefWindowProc(...) as well
	case WM_CLOSE:
		DestroyWindow(hwnd);
        break;
	}return DefWindowProc(hwnd, msg, wParam, lParam);}

Problem here:
IIRC - Commands for childwindows (and menu options) are not sent as top level messages like this :
1
2
3
4
5
6
7
8
9
switch (msg)
{
case ID_BUTTON:
{
    // code
    SendMessage(static_label,LB_ADDSTRING,255,(LPARAM)(LPCSTR)"You clicked the button");
    ShowWindow(static_label,SW_SHOW);
    }
break;	


They are sent in the wParam part of the WM_COMMAND message.

Static windows don't do messages - so we will need to change that piece of code.

(ps You need a break statement for the WM_CREATE case)

Last edited on
So in light of what I've said above - change your code to look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
		case WM_PAINT: 
		hdc = BeginPaint(hwnd, &ps);
		EndPaint(hwnd, &ps);
		return 0;
        case WM_COMMAND: //Command from Child windows and menus are under this message
            switch(wParam) //the ID is is wParam
            {
                case ID_BUTTON: //check for our button ID
                {
                    // Static labels dont do messages
                    //we can set the text directly though
                    SetWindowText(static_label,"You clicked the button");
                    break;
                }
            }
        break;
you can try this:
1
2
3
4
5
6
7
8
9
BOOL keys[256];//global Variable
//...
case WM_KEYDOWN:
keys[wParam]=truel
case WM_KEYUP:
keys[wParam]=false;
//in the while loop in WINMAIN:
if(keys[/*coresponding key id,like VK_KEYDOWN,VK_ESCAPE or 'W','S'*/])
//do something 
Nandor what are you talking about?

The original request was that when a user clicked a pushbutton (Not a key on the keyboard!), he wanted to change the text of a label.

Maybe you replied to the wrong post.

oh,sorry......it's my fault
Thx A lot Guys. Now what if I wanted to do something else with the text, window. Basically what I wanted to do is build a calculator that look like the one that came with window. But a lot of this is not making anysense to me. I tried messing with all the window fx such as...SendMessage...etc. This the problem I have:

1. How do I get the number to stack-up to each other: EX. When I press button 1, the second press doesn't replace the first 1. So that I can enter multi-digits number. etc.

2. How do I get the text to align to the left?

3. How do I copy, or retrieve the text from the "edit" box, so I can utilizes it in a function, or class?

Any help is greatly appreciated guys. Thank you.



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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "windows.h"

enum { ID_LABEL = 1,ID_IMAGE,ID_EDIT,ID_LIST,ID_BUTTON1,ID_COMBO,ID_BUTTON2,ID_BUTTON0,
ID_BUTTON3,ID_BUTTON4,ID_BUTTON5,ID_BUTTON6,ID_BUTTON7,ID_BUTTON8,ID_BUTTON9, 
ID_BUTTONPLUS, ID_BUTTONMINUS, ID_BUTTONTIME, ID_BUTTONDIVIDE, ID_BUTTONEQUAL};
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 

HINSTANCE g_hInst;
TCHAR	szAppName[]	= TEXT("Calculator");
TCHAR	szWinClass[]	= TEXT("WIN_CLASS");

HWND static_label;
HWND edit;
HWND list; 
//number
HWND button0; 
HWND button1;
HWND button2;
HWND button3;
HWND button4;
HWND button5;
HWND button6;
HWND button7;
HWND button8;
HWND button9;
//operation
HWND buttonPlus; 
HWND buttonMinus;
HWND buttonTime;
HWND buttonDivide; 
HWND buttonEqual; 

int WINAPI WinMain(HINSTANCE hInstance, 
		HINSTANCE hPrevInstance,
		PSTR lpCmdLine,
		int nCmdShow){
						
	MSG			msg; 
	WNDCLASS	wc;	
	HWND		hwnd; 
	g_hInst = hInstance;
	
	wc.cbClsExtra		= 0; 
	wc.cbWndExtra		= 0; 
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1); 
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW); 
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 
	wc.hInstance		= hInstance; 
	wc.lpfnWndProc		= WinProc; 
	wc.lpszClassName	= szWinClass; 
	wc.lpszMenuName		= NULL; 
	wc.style			= CS_HREDRAW | CS_VREDRAW; 
 
 	if( !RegisterClass(&wc)) 
	{
		MessageBox(NULL, TEXT("Error registering class"), TEXT("ERROR"), MB_OK);
		return 0;
	}

	hwnd = CreateWindow(szWinClass,
						szAppName, 
						WS_OVERLAPPEDWINDOW, 
						CW_USEDEFAULT, 
						CW_USEDEFAULT, 
						300, 
						400, 
						(HWND) NULL, 
						(HMENU) NULL, 
						(HINSTANCE) hInstance, 
						(void *) NULL); 

	ShowWindow(hwnd, nCmdShow); 
	UpdateWindow(hwnd);	

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

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT	ps;
	HDC			hdc;
	
	switch(msg){
		case WM_CREATE:
			static_label = CreateWindow("Static","Please Enter A Number",WS_CHILD | WS_VISIBLE,35,15,175,25,hwnd,0,g_hInst,0);	
			edit = CreateWindow("Edit", NULL,WS_BORDER | NULL | WS_CHILD | WS_VISIBLE | NULL | NULL ,35,45,175,20,hwnd,(HMENU)ID_EDIT,g_hInst,0);
			
			button0 = CreateWindow("Button","0",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,80,220,35,35,hwnd,(HMENU)ID_BUTTON0,g_hInst,0);	
			button1 = CreateWindow("Button","1",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,35,170,35,35,hwnd,(HMENU)ID_BUTTON1,g_hInst,0);	
			button2 = CreateWindow("Button","2",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,80,170,35,35,hwnd,(HMENU)ID_BUTTON2,g_hInst,0);	
			button3 = CreateWindow("Button","3",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,125,170,35,35,hwnd,(HMENU)ID_BUTTON3,g_hInst,0);	
			button4 = CreateWindow("Button","4",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,35,120,35,35,hwnd,(HMENU)ID_BUTTON4,g_hInst,0);	
			button5 = CreateWindow("Button","5",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,80,120,35,35,hwnd,(HMENU)ID_BUTTON5,g_hInst,0);	
			button6 = CreateWindow("Button","6",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,125,120,35,35,hwnd,(HMENU)ID_BUTTON6,g_hInst,0);	
			button7 = CreateWindow("Button","7",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,35,70,35,35,hwnd,(HMENU)ID_BUTTON7,g_hInst,0);	
			button8 = CreateWindow("Button","8",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,80,70,35,35,hwnd,(HMENU)ID_BUTTON8,g_hInst,0);	
			button9 = CreateWindow("Button","9",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,125,70,35,35,hwnd,(HMENU)ID_BUTTON9,g_hInst,0);	
			
			buttonDivide = CreateWindow("Button","/",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,170,70,35,35,hwnd,(HMENU)ID_BUTTONDIVIDE,g_hInst,0);	
			buttonTime = CreateWindow("Button","*",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,170,120,35,35,hwnd,(HMENU)ID_BUTTONTIME,g_hInst,0);	
			buttonMinus = CreateWindow("Button","-",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,170,170,35,35,hwnd,(HMENU)ID_BUTTONMINUS,g_hInst,0);	
			buttonPlus = CreateWindow("Button","+",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,170,220,35,35,hwnd,(HMENU)ID_BUTTONPLUS,g_hInst,0);	
			buttonEqual = CreateWindow("Button","=",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,125,220,35,35,hwnd,(HMENU)ID_BUTTONEQUAL,g_hInst,0);
			
		case WM_PAINT: 
			hdc = BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
			return 0;
		case WM_COMMAND: //Command from Child windows and menus are under this message
            switch(wParam) //the ID is wParam
            {
                case ID_BUTTON0: //check for our button ID
					{
					// Static labels dont do messages
                    //we can set the text directly though
						
						SetWindowText(edit,"0");
						//SetWindowText(edit,"0");
						break;
					}
					
				case ID_BUTTON1:
					{
						SetWindowText(edit,"1");
						break;
					}
					
				case ID_BUTTON2:
					{
						SetWindowText(edit,"2");
						break;
					}
				
				case ID_BUTTON3:
					{
						SetWindowText(edit,"3");
						break;
					}
				
				case ID_BUTTON4:
					{
						SetWindowText(edit,"4");
						break;
					}
				
				case ID_BUTTON5:
					{
						SetWindowText(edit,"5");
						break;
					}
				
				case ID_BUTTON6:
					{
						SetWindowText(edit,"6");
						break;
					}
				                
				case ID_BUTTON7:
					{
						SetWindowText(edit,"7");
						break;
					}
				                
				case ID_BUTTON8:
					{
						SetWindowText(edit,"8");
						break;
					}
				
				case ID_BUTTON9: 
					{
						SetWindowText(edit,"9");
						break;
					}
					
				case ID_BUTTONPLUS: 
					{
						SetWindowText(edit,"+"); 
						break;
					}
				
				case ID_BUTTONMINUS: 
					{
						SetWindowText(edit,"-");
						break;
					}
				
				case ID_BUTTONTIME: 
					{
						SetWindowText(edit,"*");
						break;
					}
				
				case ID_BUTTONDIVIDE: 
					{
						SetWindowText(edit,"/");
						break;
					}
				case ID_BUTTONEQUAL:
					{
						SetWindowText(edit,"="); 
						break; 
					}
            }//switch. 
			break;
		case WM_DESTROY: 
			PostQuitMessage(0);
			break; // pass to DefWindowProc(...) as well
		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;
	}return DefWindowProc(hwnd, msg, wParam, lParam);}
Topic archived. No new replies allowed.