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,如果边框不设置颜色,默认为黑色
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,如果边框不设置颜色,默认为黑色
相关文章推荐
- 使用VC++绘制不规则的窗体
- VC++之绘制连续线条和扇形效果的线条
- 窗体皮肤实现 - 在VC中简单实现绘制(五)
- VC 窗体非客户区的绘制 OnNcPaint(UINT message, WPARAM wParam, LPARAM lParam)
- 【VC++】003绘制连续线条-扇形线条
- VC++玩转炫酷悬浮窗2---不规则窗体的实现(常规)
- Win10系列:VC++绘制位图图片
- VC—主框架窗口绘制背景
- [Unity]利用Mesh在Unity中绘制扇形图片2
- 【VC编程技巧】窗体☞3.4利用bitmap改变对话框的背景。
- 【VC编程技巧】窗体☞3.5对单文档或者多文档程序制作启动画面
- VC中实现非标准对话框窗体的实现
- vc中透明窗体的实现!!
- 添加资源图片,获取图片实例并绘制到窗体
- VC中获取窗体句柄的各种方法
- 纯css3绘制扇形
- VC图形绘制双缓存的代码复用性讨论
- vc动态曲线绘制方法
- VC下简易实现窗体的淡入淡出效果(fade in-out for CDialog)
- C#窗体中根据两点坐标绘制直线