Win32 GDI Drawing a circle?
Win32 GDI Drawing a circle?
我正在尝试画一个圆,我目前正在使用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,那么为什么不使用内置的双缓冲呢?
相关文章:
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- C++ OpenCV 3D Drawing
- 圆形与方形碰撞检测以及需要响应C++ |OpenGL。我的问题就是这个"I need to keep the circle outside the square"
- circle.exe 在代码块中使用 graphics.h 时停止工作
- circle类和班级成员的打印向量发行
- opengl instanced drawing - 3D Arrows
- glDrawElements not drawing
- 如何将System :: Drawing :: Image^转换为未签名的字符
- 使用"pencil"在网格上"Drawing"数字
- 使用ProtoBuf序列化类型System.Drawing.Rectangle从.NET到C
- 错误:无法使用初始值设定项列表初始化非聚合类型 'Circle'
- OpenGL VBO Drawing
- Allegro Circle碰撞检测
- Qt OpenGL DrawElements not Drawing the second triangle
- 从 Circle 类返回布尔值 C++
- 重载Circle()在C++Circle类中不明确
- MFC drawing board
- cv::circle的怪异行为
- visual drawing circle in c++
- Win32 GDI Drawing a circle?