您的位置:首页 > 其它

vc在窗体上绘制扇形

2006-06-17 09:42 357 查看
做控件的时候难免会遇到绘制图形,而扇形也是其中的一种,在查询MSDN后,经过iN次摸索,才渐渐明白了扇形的绘制方法,过程如下:

1、新建一个Dialog工程,在OnPaint里加上如下代码:

void CtestchDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
CRect rectClient; //当前控件的矩形坐标结构
GetClientRect(rectClient);

// Make a couple of pens and similar brushes.
CBrush brushBlue;
CBrush* pOldBrush;
CPen* pOldPen;

brushBlue.CreateSolidBrush(RGB(0,0, 255)); //创建实心画刷

// Draw from 3 o'clock to 6 o'clock, counterclockwise,
// in a blue pen with a solid blue fill.
pOldBrush = dc.SelectObject(&brushBlue);
rectClient.bottom*=2;
dc.Pie(rectClient,
CPoint(rectClient.right,0), //CenterPoint()为中心点
//CPoint(rectClient.CenterPoint().x, rectClient.right));
CPoint(rectClient.left,0));
// Restore the previous pen.
dc.SelectObject(pOldPen);
CDialog::OnPaint();
}
}

3、MSDN解释如下:

BOOL Pie( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 );

BOOL Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

Return Value

Nonzero if the function is successful; otherwise 0.

Parameters

x1

Specifies the x-coordinate of the upper-left corner of the bounding rectangle (in logical units).

y1

Specifies the y-coordinate of the upper-left corner of the bounding rectangle (in logical units).

x2

Specifies the x-coordinate of the lower-right corner of the bounding rectangle (in logical units).

y2

Specifies the y-coordinate of the lower-right corner of the bounding rectangle (in logical units).

x3

Specifies the x-coordinate of the arc’s starting point (in logical units). This point does not have to lie exactly on the arc.

y3

Specifies the y-coordinate of the arc’s starting point (in logical units). This point does not have to lie exactly on the arc.

x4

Specifies the x-coordinate of the arc’s endpoint (in logical units). This point does not have to lie exactly on the arc.

y4

Specifies the y-coordinate of the arc’s endpoint (in logical units). This point does not have to lie exactly on the arc.

lpRect

Specifies the bounding rectangle. You can pass either a CRect object or a pointer to a RECT structure for this parameter.

ptStart

Specifies the starting point of the arc. This point does not have to lie exactly on the arc. You can pass either a POINT structure or a CPoint object for this parameter.

ptEnd

Specifies the endpoint of the arc. This point does not have to lie exactly on the arc. You can pass either a POINT structure or a CPoint object for this parameter.

Remarks

Draws a pie-shaped wedge by drawing an elliptical arc whose center and two endpoints are joined by lines. The center of the arc is the center of the bounding rectangle specified by x1, y1, x2, and y2 (or by lpRect). The starting and ending points of the arc are specified by x3, y3, x4, and y4 (or by ptStart and ptEnd).

The arc is drawn with the selected pen, moving in a counterclockwise direction. Two additional lines are drawn from each endpoint to the arc’s center. The pie-shaped area is filled with the current brush. If x3 equals x4 and y3 equals y4, the result is an ellipse with a single line from the center of the ellipse to the point (x3, y3) or (x4, y4).

The figure drawn by this function extends up to but does not include the right and bottom coordinates. This means that the height of the figure is y2 – y1 and the width of the figure is x2 – x1. Both the width and the height of the bounding rectangle must be greater than 2 units and less than 32,767 units.

简单的说,x1,y1,x2,y2 确定外接矩形,扇形的中心和外接矩形的中心重合

至于x3,y3,x4,y4,只是为了确定起点和终点,不一定在扇形上,系统会自动计算扇形的位置

同时,绘制出的扇形并不一定是圆的一部分,形状不是很规则,绘制的时候要注意

另外,扇形的边框是用pen来绘的,填充是用brush,如果边框不设置颜色,默认为黑色
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: