Question about scope in MFC programming

Im new to Windows programming and I am trying to make a tic tac toe program just to get used to it. My problem is when I run the code cursorX and cursorY retain their original values and draw their positions again like a stamp. How can I make it so that the changes made in my WM_KEYDOWN switch are processed as the new coordinates?

-edit- I tried setting them as global variables it had the same result, I also tried different WM messages and they had the same result D:

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
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)              
    {
		
		HDC hdc;
		PAINTSTRUCT ps;
		RECT rect;
		static bool isPushed;
		static int cursorX, cursorY;
		case WM_CREATE:
			cursorX = 50;
			cursorY = 50;
			return 0;
		case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);
			//board
			MoveToEx(hdc, 200, 50, NULL);//left side of board up and down
			LineTo(hdc, 200, 500);
			MoveToEx(hdc,350, 50, NULL);//right side of the board up and down
			LineTo(hdc, 350, 500);
			MoveToEx(hdc, 50, 200, NULL);//topmost line across
			LineTo(hdc, 500, 200);
			MoveToEx(hdc, 50, 350, NULL);//bottommost line
			LineTo(hdc, 500, 350);
			//cursor
			MoveToEx(hdc, cursorX, cursorY, NULL);//top-right corner of the cursor
			LineTo(hdc,cursorX + 20, cursorY);
			MoveToEx(hdc, cursorX, cursorY, NULL);
			LineTo(hdc, cursorX, cursorY+20);
			MoveToEx(hdc, cursorX+145, cursorY, NULL);//top-left corner of the cursor
			LineTo(hdc, cursorX+125, cursorY);
			MoveToEx(hdc, cursorX+145, cursorY,NULL);
			LineTo(hdc, cursorX+145, cursorY+20);
			MoveToEx(hdc, cursorX, cursorY+145, NULL);//bottom-left corner of the cursor
			LineTo(hdc, cursorX, cursorY+125);
			MoveToEx(hdc,cursorX, cursorY+145, NULL);
			LineTo(hdc, cursorX+20, cursorY+145);
			MoveToEx(hdc, cursorX+145, cursorY+145,NULL);//bottom-right corner of the cursor
			LineTo(hdc, cursorX+145, cursorY+125);
			MoveToEx(hdc, cursorX+145, cursorY+145,NULL);
			LineTo(hdc, cursorX+125, cursorY+145);
			EndPaint(hwnd, &ps);
			return 0;
		case WM_KEYDOWN:
			isPushed = true;
			SetRect(&rect, 0, 0, 700, 700);
			if(isPushed){	
				switch(wParam){
					case VK_LEFT:
						cursorX -= 150;
						InvalidateRect(hwnd, &rect, false);
						isPushed = false;
						break;
					case VK_RIGHT:
						cursorX += 150;
						InvalidateRect(hwnd, &rect, false);
						isPushed = false;
						break;
					case VK_UP:
						cursorY -= 150;
						InvalidateRect(hwnd, &rect, false);
						isPushed = false;
						break;
					case VK_DOWN:
						cursorY += 150;
						InvalidateRect(hwnd, &rect, false);
						isPushed = false;
						break;
				}
			}
		return 0;
        case WM_DESTROY:
            PostQuitMessage (0);      
            break;
        default:                     
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Last edited on
After some experimentation i realized that im not generating a new WM_PAINT message even after using InvalidateRect (its obvious because I move my coordinates and resize my window and the change becomes what i want it to be after Windows processes a new WM_PAINT message from the resize) how do I generate another WM_PAINT message on the fly?
Last edited on
Topic archived. No new replies allowed.