Failure to display movement of a rectangle

Aug 29, 2009 at 5:02am
I'm trying to move a rectangle in response to the pressing of the arrow keys, but nothing appears to be happening. I'm not getting any flickers for constant redrawing, so I suspect it's due to a problem with the WM_KEYDOWN message, but I am not sure.
Here's the 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
HDC hdc;         // Handle to a device context, for drawing
    PAINTSTRUCT ps;  // Extra information useful when painting
    RECT Window;
    RECT ClickMeBox;
    RECT ExitBox;
    COLORREF BoxBorder = RGB(255,255,255);
    HBRUSH BoxBorderBrush = CreateSolidBrush(BoxBorder);
    COLORREF BoxInternal = RGB(0,0,0);
    HBRUSH BoxInternalBrush = CreateSolidBrush(BoxInternal);
    int PaintInitialiser=1;    
    switch (message)                  /* handle the messages */
    {
        //-------------------------------------------
        case WM_CREATE:
             // Once only initialisation
        return 0;

        //-------------------------------------------        
        case WM_PAINT:
             GetClientRect(hwnd, &Window); 
             hdc = BeginPaint( hwnd, &ps );        
             if (PaintInitialiser == 1)
             {    
                ClickMeBox.left = (Window.right/2) - 75;
                ClickMeBox.right = (Window.right/2) + 75;
                ClickMeBox.top = (Window.bottom/2) - 75;
                ClickMeBox.bottom = (Window.bottom/2) + 75;                
                PaintInitialiser = 0;
             }
             ExitBox.right = Window.right;
             ExitBox.top = Window.top;
             ExitBox.left = ExitBox.right - 50;
             ExitBox.bottom = ExitBox.top + 50;             
             Rectangle(hdc, ExitBox.left, ExitBox.top, ExitBox.right, ExitBox.bottom);
             Rectangle(hdc, ClickMeBox.left, ClickMeBox.top, ClickMeBox.right, ClickMeBox.bottom);
             char ClickMe[] = "Click Me!";        
             DrawText(hdc, ClickMe, strlen(ClickMe), &ClickMeBox, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
             char Exit[] = "Exit";
             DrawText(hdc, Exit, strlen(Exit), &ExitBox, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
             EndPaint( hwnd, &ps );
             return 0;
        
        case WM_KEYDOWN:
             switch(wParam)
             {
                case VK_UP:
                     {
                            ClickMeBox.top -= 10;
                            ClickMeBox.bottom -= 10;                            
                            break;
                     }
                case VK_DOWN:
                     {
                            ClickMeBox.top += 10;
                            ClickMeBox.bottom += 10;                            
                            break;
                     } 
                case VK_LEFT:
                     {
                            ClickMeBox.left -= 10;
                            ClickMeBox.right -= 10;
                            break;
                     }
                case VK_RIGHT:
                     {
                            ClickMeBox.left += 10;
                            ClickMeBox.right += 10;
                            break;
                     }  
             }
             InvalidateRect(hwnd, NULL, TRUE);
             return 0;
Aug 29, 2009 at 7:44am
Your logic is flawed in more than one place.

int PaintInitialiser=1 This will always be initialised to 1 each time time your windows
procedure is called because it is a non static local variable - so the boxes will always be printed in their original spot each time - so
no movement.


Even if your PaintInitialiser way did work (which as we say it does not) - your variables
1
2
3
    
    RECT ClickMeBox;
    RECT ExitBox;


are all non-static local variables which will be reinitialised (to random values) each time the window procedure is called. You need to save their values between calls.

Try this (notice the use of the static keyword)
1
2
3
4
    
     static RECT ClickMeBox;
    static RECT ExitBox;
     static int PaintInitialiser=1; 


Last edited on Aug 29, 2009 at 7:46am
Sep 1, 2009 at 10:53am
That worked. Thankyou very much.
Topic archived. No new replies allowed.