在子窗口中绘制形状

时间:2017-12-31 06:53:39

标签: c++ codeblocks win32gui wm-paint

我正在开发一个Win32 GUI项目,其中我有一个主窗口,里面我显示3个按钮和1个附有BITMAP的标签。像这样: Main window

我声明了一个标签数组,并且我为每个标签附加了一个位图,这就是我如何做到的:

/*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]);
}

背景必须根据点击的按钮以及名为value的变量的值来改变,该值可以取值为1,2或4。

然后我像这样处理按钮的事件:

/*  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;
}

并且setVisibleBacground的声明是这样的:

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

在此之前,一切都运作良好,但现在我需要按下第三个按钮来显示第三个背景,然后绘制一个形状,例如可以是背景上方的矩形或椭圆。

我试试这个:

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;

当单击第三个按钮时,它将等于4,我尝试更改setVisibleBackground函数,如下所示:

/*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);
    }
}

但是这个代码只有在我按下第一个和第二个按钮时才会起作用,背景变化,但是当我按下第三个按钮时,它会失败,首先它会绘制圆圈,然后它会在圆圈上方显示背景,就像在这images

任何帮助将不胜感激

0 个答案:

没有答案