作者:朱金灿
来源:http://blog.csdn.net/clever101


GDI+的绘图类Graphics的绘制圆弧的接口DrawArc
Status DrawArc(IN const Pen* pen,
IN const Rect& rect,
IN REAL startAngle,
IN REAL sweepAngle)


教程是这样解释该接口的:
pen : 画笔
rect : 定义弧线的矩形
startAngle: 从x轴到弧线的起始点沿顺时针方向度量的角(以度为单位)。

sweepAngle: 从startAngle参数到弧线的结束点沿顺时针方向度量的角(以度为单位)。


pen参数很好理解,所谓弧线,本质就是椭圆的一部分,GDI/GDI+往往使用椭圆的外接矩形决定椭圆的形状,所以rect参数也很好理解,只是对其它一些参数还有一些疑问:首先哪一条直线代表x轴,其次一段圆弧有两端,哪一端表示弧线的起始点,哪一个结束点?

于是决心自己动手去搞明白这个问题,写了下面代码:


CDC *pDC = pView->GetDC(); Graphics gp(pDC->m_hDC); REAL startAngle = 60.0f; // 设置起点角度为60度 REAL sweepAngle = 150.0f; // 设置旋转角度为150度 Rect ellipseRect2(10,10,500,500); gp.DrawEllipse(&greenPen,245,245,2,2); // gp.DrawArc(&bluePen,ellipseRect2,startAngle,sweepAngle); gp.DrawRectangle(&Pen(Color::Blue),ellipseRect2); gp.ReleaseHDC(pDC->m_hDC); pView->ReleaseDC(pDC);


效果图如下:


对GDI+绘制圆弧接口的理解


分析了一下效果图,大致明白了DrawArc函数的各个参数的意义,于是自己画下下图:


对GDI+绘制圆弧接口的理解



看着上图,大家应该很明白,所谓x轴就是过外接矩形中心点的水平线,圆弧的起点就是靠右的一端,结束点就是靠左的一点,上面说的sweepAngle是从startAngle参数到弧线的结束点沿顺时针方向度量的角(以度为单位)其实说得不明不白的,我觉得更为准确的说法应该是沿着外接矩形中心点和圆弧起始点连成的直线扫描到外接矩形中心点和圆弧起始点连成的直线所经过的角度,sweep就有扫描的意思。

想到这里,我想到假如扫描角度超过360度,就可以绘制一个圆出来,还有如果起始角是负值,弧线应该在第一象限内。实际上确实这样的。还有一种情况是当sweepAngle是负值的情况下,是一种怎样的情况呢?我发现当sweepAngle是负值时,弧线是作逆时针扫描的。如下面的代码:


REAL startAngle = 0.0f; REAL sweepAngle = -60.0f; // notice:the sweepAngle is minus Rect ellipseRect2(10,10,250,250); gp.DrawEllipse(&greenPen,120,120,2,2); // gp.DrawArc(&bluePen,ellipseRect2,startAngle,sweepAngle); gp.DrawRectangle(&Pen(Color::Blue),ellipseRect2);


<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--><!-- [if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1026"/> </xml><![endif]--><!-- [if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout></xml><![endif]-->

效果图如下:


对GDI+绘制圆弧接口的理解









相关文章:

  • 2022-12-23
  • 2021-04-12
  • 2022-12-23
  • 2021-10-21
  • 2021-11-28
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-15
  • 2021-06-27
  • 2021-08-12
  • 2022-12-23
  • 2021-12-18
相关资源
相似解决方案