Win32 GDI画圆? [英] Win32 GDI Drawing a circle?

查看:581
本文介绍了Win32 GDI画圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一个圆,我目前正在使用Ellipse()函数。

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

我有起始的鼠标坐标 - x1和y1以及结束坐标x2和y2。正如你可以看到,我迫使y2(temp_shape.bottom)为= y1 +(x2-x1)。这不工作按预期。我知道计算是完全错误的,但任何想法是什么是正确的?

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?

代码下面。

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;


推荐答案

我制定了一个更好的计算。

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

这篇关于Win32 GDI画圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆