加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Win32 GDI画一个圆圈?

发布时间:2020-12-16 07:02:55 所属栏目:百科 来源:网络整理
导读:我想绘制一个圆,我目前正在使用Ellipse()函数. 我有起始鼠标坐标 – x1和y1以及结束坐标x2和y2.如你所见,我强迫y2(temp_shape.bottom)为= y1(x2-x1).这不符合预期.我知道计算是完全错误的,但任何关于什么是正确的想法? 代码如下. case WM_PAINT: { hdc = Be
我想绘制一个圆,我目前正在使用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));
            SelectObject(backbuffDC,GetStockObject(DC_PEN));
            SetDCPenColor(backbuffDC,RGB(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.bottom);
                }

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

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

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

解决方法

如果你想让椭圆()绘制一个完美的圆形圆,你需要给它一个完美的方形形状的坐标,而不是矩形.

假设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.bottom);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读