Win32 GDI Drawing a circle?

Win32 GDI Drawing a circle?

本文关键字:circle Drawing GDI Win32      更新时间:2023-10-16

我正在尝试画一个圆,我目前正在使用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;

如果你想让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);
}

我已经算出了一个更好的计算方法。粘贴在下面,供其他想要相同的人使用。

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

你的代码是不必要的复杂与临时dc和后台缓冲区,你是在每个WM_PAIT重新创建GDI刷?但这不是重点。问题是:你为什么这样做?

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

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