Win32 GDI Drawing a circle?

允我心安 提交于 2019-12-10 16:07:53

问题


I am trying to draw a circle and I am currently using the Ellipse() function.

I have the starting mouse coordinates - x1 and y1 and the ending coordinates x2 and y2. As you can see, I am forcing the y2(temp_shape.bottom) to be = y1+(x2-x1). This doesn't work as intended. I know the calculation is completely wrong but any ideas on what is right?

Code Below.

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;

回答1:


If you want Ellipse() to draw a perfectly round circle, you need to give it coordinates for a perfectly square shape, not a rectangular shape.

Assuming x1,y1 are the starting coordinates of the dragging and x2,y2 are the current mouse coordinates, then try this:

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



回答2:


I have worked out a calculation which works better. Pasted below for anyone else wanting the same.

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



回答3:


Your code is unnecessarily complex with temp DCs and back buffers and you are recreating GDI brushes in every WM_PAIT? But that's besides the point. Here is the question : why do you do this:

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

What framework stack is this on top of? .NET, then why don't you use built-in double buffering?



来源:https://stackoverflow.com/questions/14063463/win32-gdi-drawing-a-circle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!