Error in displaying rectangles

Aug 28, 2009 at 5:52am
The aim of this program at this point is to get the window to display a rectangle in the middle of the window, and a box in the top right window. however, neither rectangle is appearing. Can anyone help me with this?
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
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;         // Handle to a device context, for drawing
    PAINTSTRUCT ps;  // Extra information useful when painting
    RECT Window;
    RECT ClickMeBox;
    RECT ExitBox;
    BOOL PaintInitialiser;    
    switch (message)                  /* handle the messages */
    {
        //-------------------------------------------
        case WM_CREATE:
             // Once only initialisation
             GetClientRect(hwnd,&Window);
             PaintInitialiser = TRUE;
        return 0;

        //-------------------------------------------        
        case WM_PAINT:
             hdc = BeginPaint( hwnd, &ps );
             if (PaintInitialiser = TRUE)
             {
                ExitBox.right = Window.right;
                ExitBox.top = Window.top;
                ExitBox.left = ExitBox.right - 50;
                ExitBox.bottom = ExitBox.top + 50;
                ClickMeBox.left = (Window.right/2) - 75;
                ClickMeBox.right = (Window.right/2) + 75;
                ClickMeBox.top = (Window.bottom/2) - 75;
                ClickMeBox.bottom = (Window.bottom/2) + 75;
                Rectangle(hdc, ExitBox.left, ExitBox.top, ExitBox.right, ExitBox.bottom);
                Rectangle(hdc, ClickMeBox.left, ClickMeBox.top, ClickMeBox.right, ClickMeBox.bottom);
             }
             // Do painting here
             
             EndPaint( hwnd, &ps );
             return 0;
             
       //-------------------------------------------
        case WM_DESTROY:
             // Once only cleanup.
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        return 0;
        
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
Last edited on Aug 28, 2009 at 10:31am
Aug 28, 2009 at 1:22pm
Why are you conditioning the WM_PAINT message? You shouldn't because WM_PAINT is issued by Windows whenever repainting is needed. And if you condition it, BeginPaint() and EndPaint() should be included inside the conditional to avoid repainting of the background without repainting the rectangles.
Aug 28, 2009 at 2:40pm
closed account (z05DSL3A)
Try:
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
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;         // Handle to a device context, for drawing
    PAINTSTRUCT ps;  // Extra information useful when painting
    RECT Window;
    RECT ClickMeBox;
    RECT ExitBox;
    BOOL PaintInitialiser;    
    switch (message)                  /* handle the messages */
    {
        //-------------------------------------------
        case WM_CREATE:
        break;

        //-------------------------------------------        
        case WM_PAINT:
             hdc = BeginPaint( hwnd, &ps );
             
             GetClientRect(hwnd,&Window);
             ExitBox.right = Window.right;  
             ExitBox.top = Window.top;
             ExitBox.left = ExitBox.right - 50;
             ExitBox.bottom = ExitBox.top + 50;
             ClickMeBox.left = (Window.right/2) - 75;
             ClickMeBox.right = (Window.right/2) + 75;
             ClickMeBox.top = (Window.bottom/2) - 75;
             ClickMeBox.bottom = (Window.bottom/2) + 75;
             Rectangle(hdc, ExitBox.left, ExitBox.top, ExitBox.right, ExitBox.bottom);
             Rectangle(hdc, ClickMeBox.left, ClickMeBox.top, ClickMeBox.right, ClickMeBox.bottom);
             // Do painting here
             
             EndPaint( hwnd, &ps );
             break;
             
       //-------------------------------------------
        case WM_DESTROY:
             // Once only cleanup.
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
        
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
Aug 29, 2009 at 4:42am
I managed to find the problem - I wasn't using the GetClientRect function correctly, so it was passing values of 0 to my 'Window' rect.
Topic archived. No new replies allowed.