WinAPI program won't work

I've written a small calculator program useing the winapi, i'm a begginer so I thought I'd start with getting it working before optimization and stuff. The only problem is that it dosn't work. I think i'm haveing one of two problems, 1. the finalout isn't being updated or its not updating at all. If you could help that would be great.
Here's my program code (ive cut out some of the nonessential repetitive bits)
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>
#include <iostream>


using namespace std;
float output = 0;
float finalout;
float hold;
bool holding = false;
bool alive = true;
int sign = 1;
char textOutput [32];

void addNumber( int input )
{
	finalout = output * 10 + input;
	sprintf_s (textOutput, "%f", finalout);
}

void calculate(int which)
{
	if (which != 0)
		sign = which;
	hold = output;
	if (holding == true)
	{
		if (sign == 1)
		{
			output *= hold;
		}
		if (sign == 2)
		{
			output /= hold;
		}
		if (sign == 3)
		{
			output += hold;
		}
		if (sign == 4)
		{
			output -= hold;
		}
	}
	holding = true;
}


//WinAPI Procedure, would like to give credit to zetcode 
//http://zetcode.com/tutorials/winapi/ helped a lot.
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);



int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  HWND hwnd;
  WNDCLASS wc;
	
  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.lpszClassName = TEXT( "Calculator" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpszMenuName  = NULL;
  wc.lpfnWndProc   = WndProc;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  
  RegisterClass(&wc);
  hwnd = CreateWindow( wc.lpszClassName, TEXT("Calculator"),
                WS_OVERLAPPED | WS_SYSMENU,
                100, 100, 155, 225, NULL, NULL, hInstance, NULL);  

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

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

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
//PAINT CONSTRUCT
	HDC hDC;
	PAINTSTRUCT Ps;

  switch(msg)  
  {
  case WM_PAINT:
	  {
		  hDC = BeginPaint(hwnd, &Ps);
		  TextOut(hDC, 10, 10, textOutput, lstrlen(textOutput));
		  EndPaint(hwnd, &Ps);
		  break;
	  }
  case WM_CREATE:
	{
//BUTTONS!!!
            CreateWindow(TEXT("button"), TEXT("1"),    
		             WS_VISIBLE | WS_CHILD ,
		             10, 50, 25, 25,        
		             hwnd, (HMENU) 1, NULL, NULL); 

		    CreateWindow(TEXT("button"), TEXT("2"),    
		             WS_VISIBLE | WS_CHILD ,
		             45, 50, 25, 25,        
		             hwnd, (HMENU) 2, NULL, NULL);

//SIGN BUTTONS
			CreateWindow(TEXT("button"), TEXT("/"),    
		             WS_VISIBLE | WS_CHILD ,
		             115, 50, 25, 25,        
		             hwnd, (HMENU) 11, NULL, NULL);


			CreateWindow(TEXT("button"), TEXT("="),    
		             WS_VISIBLE | WS_CHILD ,
		             80, 155, 25, 25,        
		             hwnd, (HMENU) 15, NULL, NULL);

		break;
	}

  case WM_COMMAND:
	  {

//NUMBER COMMANDS
		 if (LOWORD (wParam) == 1)
		 {
			addNumber (1);
			UpdateWindow (hwnd);
		 }
		 else if (LOWORD (wParam) == 2)
		 {
			 addNumber (2);
			 UpdateWindow (hwnd);
		 }
//CALCULATION COMMANDS
		 else if (LOWORD (wParam) == 11)
		 {
			 calculate (1);
		 }

		 else
		 {
			 calculate (0);
			 UpdateWindow (hwnd);
		 }

		 break;
	  }
    case WM_DESTROY:
    {
		PostQuitMessage(0);
		return 0;
    }
  }
	addNumber(0);
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

Thanks a lot!
Hi
I think I found some problems

firstly I changed UpdateWindow(hwnd)
to InvalidateRect(hwnd,NULL,true) so that it is displaying properly

next I removed the addNumber(0); at the end of wndProc because it
was setting everthing back to zero.

and I also put the defwindowProc into the switch statement as default,
because you dont want it called when you have already processed the messages.


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
  case WM_COMMAND:
	  {

//NUMBER COMMANDS
		 if (LOWORD (wParam) == 1)
		 {
			addNumber (1);
			
			InvalidateRect(hwnd,NULL,true);
		 }
		 else if (LOWORD (wParam) == 2)
		 {
			 addNumber (2);
			InvalidateRect(hwnd,NULL,true);
		 }
//CALCULATION COMMANDS
		 else if (LOWORD (wParam) == 11)
		 {
			 calculate (1);
		 }

		 else
		 {
			 calculate (0);
			 InvalidateRect(hwnd,NULL,true);
		 }

		 break;
	  }
    case WM_DESTROY:
    {
		PostQuitMessage(0);
		return 0;
    }

	default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
  }
	//addNumber(0);
	return(0);
}


Hope this helps
Shredded
Thanks! It works perfectly now!
Topic archived. No new replies allowed.