Screen Refresh

My Program displays your score in the game. the only problem i have is that the score won't change unless the button moves over the score. If someone could help me that would be great.

Thanks,
MM

Here is my source.
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
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch(msg)  
	{
		case WM_PAINT:
		{
			char buff[5];
			PAINTSTRUCT ps;
			HDC hdc; 
			hdc = BeginPaint(hwnd, &ps);

			//Displays Score: on the Screen
			TextOut(hdc, 0,0, TEXT("Score:"), 6);
			
			// Displays the score on the screen
			TextOut(hdc, 45, 0, buff, wsprintf(buff, "%d", Score));
			
			EndPaint(hwnd, &ps);
			break;
		}
		
		case WM_CREATE:
		{
			//Creates the Button
			MyButton = CreateWindow(TEXT("button"), TEXT("Click Me"),    
						WS_VISIBLE | WS_CHILD,
						ButtonX, ButtonY, ButtonWidth, ButtonHeight,        
						hwnd, (HMENU) 1, NULL, NULL); 
		}

		case WM_COMMAND:
		{
			if (LOWORD(wParam) == 1) 
			{
				// Gets the Prev. location.
				PrevSpotX = ButtonX;
				PrevSpotY = ButtonY;
				
				// Changes the X and Y values
				ButtonX = rand() % 100 + 1;
				ButtonY = rand() % 100 + 1;
				
				// Makes sure that the same sport comes up
				if (PrevSpotX == ButtonX && PrevSpotY == ButtonY)
				{
					// Adds one to the score
					Score++;
					PostMessage(hwnd,WM_PAINT, 0, 0);
					
					//Changes the buttons X and Y values
					ButtonX = rand() % 100;
					ButtonY = rand() % 100;
					
					//Moves the button
					MoveWindow(MyButton, ButtonX, ButtonY, ButtonWidth, ButtonHeight, 1);
				}
				if (PrevSpotX != ButtonX || PrevSpotY != ButtonY)
				{
					// Adds one to the score
					Score++;
					PostMessage(hwnd, WM_PAINT, 0, 0);
					
					// Moves the button
					MoveWindow(MyButton, ButtonX, ButtonY, ButtonWidth, ButtonHeight, 1);
				}
			}
			break;
		}

		//Destroys the window
		case WM_DESTROY:
			{
				PostQuitMessage(0);
				break;
			}
		}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}
Last edited on
Do not post WM_PAINT messages. Call InvalidateRect() with the "dirty" rectangle (the area that needs redrawing), or InvalidateRect(hwnd,NULL); to invalidate the entire client area.

BeginPaint/EndPaint do a sort of clipping thing so that only areas marked as "dirty" are redrawn. Because you are never marking anything as dirty, it's not being drawn.
Thanks that helped alot.

Thanks,
MM
Topic archived. No new replies allowed.