0

I'm working on a Win32 GUI project in which I have a main window and inside I display 3 buttons and 1 label with a BITMAP attached to it. Like this: Main window

I declared an array of labels, and I attached a bitmap to each one of them, here's how i do that:

/*Loading the bitmaps, creating the backgrounds and the buttons*/
for(i = 0; i <3; i++){
    background_bitmaps[i] = (HBITMAP)LoadImage(NULL, background_strings[i], IMAGE_BITMAP, 275, 183, LR_LOADFROMFILE);
    background_labels[i] = CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP, 25, 50, 275, 183, hwnd, NULL, hThisInstance, NULL);
    tab_buttons[i] = CreateWindow("BUTTON", buttons_strings[i] ,WS_CHILD | WS_VISIBLE, 10 + (50*i), 10, 40, 30, hwnd, NULL, hThisInstance, NULL);
    SendMessageW(background_labels[i], STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)background_bitmaps[i]);
}

The background must change depending on which button it's clicked and also the value of the variable called value, that can take the values of 1, 2 or 4.

Then I handle the events of the buttons like this:

/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch (message){
        value = 0;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

        case WM_COMMAND:
            for(i = 0; i<3; i++)
                if((HWND)lParam == tab_buttons[i]){
                    value = 1<<i;
                    setVisibleBackground(i);
                }
            break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
return 0;
}

and the declaration of the setVisibleBacground is this:

/*Functions declarations*/
void setVisibleBackground(int _value){
    for(i = 0; i<3; i++){
        bool result = (_value ==  i)?TRUE:FALSE;
        ShowWindow(background_labels[i], result);
    }
}

Until this point, everything works perfectly, but now I need that when I press the 3rd button to show the 3rd backgound and then draw a shape, for example could be a Rectangle or Ellipse above the background.

I try this:

case WM_PAINT:
        hDC = BeginPaint(hwnd, &Ps);
        EndPaint(hwnd, &Ps);
        if(value == 4){
            hDC = BeginPaint(background_labels[2], &Ps);
            hPen = CreatePen(PS_NULL, 1, RGB(0, 200, 0));
            SelectObject(hDC, hPen);
            BrushOn = CreateSolidBrush(RGB(31, 127, 0));
            SelectObject(hDC, BrushOn);
            Ellipse(hDC, 160, 140, 210, 190);
            EndPaint(background_labels[2], &Ps);
        }
        break;

value it's gonna be equals to 4 when the third button is clicked and I try changing the setVisibleBackground function like this:

/*Functions declarations*/
void setVisibleBackground(int index){
    for(i = 0; i<3; i++){
        bool result = (index ==  i)?TRUE:FALSE;
        ShowWindow(background_labels[i], result);
    }
    if( index == 2){
        InvalidateRect(background_labels[2], NULL, FALSE);
        UpdateWindow(hwnd);
    }
}

But this code only works when I press the first and the second button, the backgrund change, but when I press the third button it fails, first it draws the circle, and then it displays the background above the circle, like in this images

Any help will be appreciated

  • 1
    Probably not your problem, but _`bool result = (_value == i)?TRUE:FALSE;`_ makes not much sense. Rather simply write: `bool result = _value == i;` – user0042 Dec 31 '17 at 06:59
  • Hey thanks for your comment, but that part of code works fine. The problem is the if statement that it's below, I mean, I only want that to happen (draw a shape) when the second button it's clicked but I try it this way and the program didn't work. – AlfredoMartinez Dec 31 '17 at 07:04
  • [Elaborate _"didn't work"_ in your question](https://stackoverflow.com/posts/48040608/edit) please. – user0042 Dec 31 '17 at 07:06
  • 1
    Your WM_PAINT handler bypasses all WM_PAINT processing for any value other that four. From MSDN: "The DefWindowProc function validates the update region." – Steve Dec 31 '17 at 07:09
  • I'm sorry, what I mean when I say that didn't work it's that if I click the first and second buttons, it works fine, the background change, but when I click the third button this happens, first it draws the circle and if I click the button again it displays the background above the circle. Here are some images: https://imgur.com/a/v5Rid – AlfredoMartinez Dec 31 '17 at 07:40
  • @AlfredoMartinez [Edit] your question if you have additional information please. – user0042 Dec 31 '17 at 07:53

0 Answers0