Win32 GDI画一个圆圈?

时间:2012-12-28 01:29:13

标签: c++ gdi win32gui

我正在尝试绘制一个圆圈,我目前正在使用Ellipse()函数。

我有起始鼠标坐标 - x1和y1以及结束坐标x2和y2。如你所见,我强迫y2(temp_shape.bottom)为= y1 +(x2-x1)。这不符合预期。我知道计算是完全错误的,但任何关于什么是正确的想法?

以下代码。

case WM_PAINT:
        {

            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            RECT rect;
            GetClientRect(hWnd, &rect);

            HDC backbuffDC = CreateCompatibleDC(hdc);

            HBITMAP backbuffer = CreateCompatibleBitmap( hdc, rect.right, rect.bottom);

            int savedDC = SaveDC(backbuffDC);
            SelectObject( backbuffDC, backbuffer );
            HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
            FillRect(backbuffDC,&rect,hBrush);
            DeleteObject(hBrush);



            //Brush and Pen colours
            SelectObject(backbuffDC, GetStockObject(DC_BRUSH));
            SetDCBrushColor(backbuffDC, RGB(255,0,0));
            SelectObject(backbuffDC, GetStockObject(DC_PEN));
            SetDCPenColor(backbuffDC, RGB(0,0,0));



            //Shape Coordinates
            temp_shape.left=x1;
            temp_shape.top=y1;
            temp_shape.right=x2;
            temp_shape.bottom=y2;



            //Draw Old Shapes
            //Rectangles
            for ( int i = 0; i < current_rect_count; i++ )
            {
                Rectangle(backbuffDC, rect_list[i].left, rect_list[i].top, rect_list[i].right, rect_list[i].bottom);
            }
            //Ellipses
            for ( int i = 0; i < current_ellipse_count; i++ )
            {
                Ellipse(backbuffDC, ellipse_list[i].left, ellipse_list[i].top, ellipse_list[i].right, ellipse_list[i].bottom);
            }

            if(mouse_down)
            {
                if(drawCircle)
                {

                    temp_shape.right=y1+(x2-x1);

                    Ellipse(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }

                if(drawRect)
                {
                    Rectangle(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }

                if(drawEllipse)
                {
                    Ellipse(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }
            }

            BitBlt(hdc,0,0,rect.right,rect.bottom,backbuffDC,0,0,SRCCOPY);
            RestoreDC(backbuffDC,savedDC);

            DeleteObject(backbuffer);
            DeleteDC(backbuffDC);
            EndPaint(hWnd, &ps);
        }
        break;

3 个答案:

答案 0 :(得分:2)

如果您希望Ellipse()绘制一个完美的圆形圆,则需要为其提供完美方形的坐标,而不是矩形。

假设x1,y1是拖动的起始坐标,x2,y2是当前鼠标坐标,请尝试以下操作:

//Shape Coordinates
temp_shape.left = min(x1, x2);
temp_shape.top = min(y1, y2);
temp_shape.right = max(x1, x2);
temp_shape.bottom = max(y1, y2);

...

if (drawCircle)
{
    int length = min(abs(x2-x1), abs(y2-y1));

    if (x2 < x1)
        temp_shape.left = temp_shape.right - length;
    else
        temp_shape.right = temp_shape.left + length;

    if (y2 < y1)
        temp_shape.top = temp_shape.bottom - length;
    else
        temp_shape.bottom = temp_shape.top + length;

    Ellipse(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
}

答案 1 :(得分:1)

我已经计算出更好的计算方法。粘贴在下面的任何其他想要相同的人。

if(drawSquare)
                {

                    int xdiff = abs(x2-x1);
                    int ydiff=abs(y2-y1);

                    if(xdiff>ydiff)
                    {
                        if(y2>y1)
                            temp_shape.bottom=y1+xdiff;
                        else
                            temp_shape.bottom=y1-xdiff;
                    }
                    else
                    {
                        if(x2>x1)
                            temp_shape.right=x1+ydiff;
                        else
                            temp_shape.right=x1-ydiff;
                    }


                    Rectangle(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }

答案 2 :(得分:-2)

你的代码对于临时DC和后台缓冲区是不必要的复杂,你在每个WM_PAIT中重新创建GDI画笔?但这不仅仅是重点。这是一个问题:为什么要这样做:

temp_shape.right=y1+(x2-x1);  //basing horizontal coordinate on vertical?

这是什么框架堆栈? .NET,那你为什么不使用内置的双缓冲?