关于该函数的原型及参数可以参考msdn:
The LineDDA function determines which pixels should be highlighted for a line defined by the specified starting and ending points.(指定由给定的起点和终点定义的线段上应该被“高亮”的像素)这个“高亮(highlighted)”用的很奇怪,让人不知道是什么意思,是不是要画线的意思?
BOOL LineDDA( int nXStart, // x-coordinate of starting point int nYStart, // y-coordinate of starting point int nXEnd, // x-coordinate of ending point int nYEnd, // y-coordinate of ending point LINEDDAPROC lpLineFunc, // callback function LPARAM lpData // application-defined data );
Parameters
-
nXStart
- [in] Specifies the x-coordinate, in logical units, of the line's starting point. nYStart
- [in] Specifies the y-coordinate, in logical units, of the line's starting point. nXEnd
- [in] Specifies the x-coordinate, in logical units, of the line's ending point. nYEnd
- [in] Specifies the y-coordinate, in logical units, of the line's ending point. lpLineFunc
- [in] Pointer to an application-defined callback function. For more information, see the LineDDAProc callback function. lpData
- [in] Pointer to the application-defined data.
LineDDA函数不会绘制直线,仅仅是调用回调函数。这个是我一直不能确定的问题,经过查阅资料和实例操作才得出这样的结论的。该函数使用Bre画直线算法求出直线上的点坐标,每计算出一个坐标,就将其和程序定义的数据lpData传送给回调函数lpLineFunc,由回调函数完成相应的操作。
下面给出一个示例程序:
- 建立一个MFC应用程序;
- 在函数OnDraw中添加如下代码:void CSimpleView::OnDraw(CDC* pDC) { //Implementation Note: it is safe to pass our local reference //of the pDC object because all of the callbacks will complete //before the pDC goes out of scope. CSimpleDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); //Draw a Line pDC->MoveTo(10,10); pDC->LineTo(300,300); //Use LineDDA to Draw 0's at intervals along the line. ::LineDDA(10,10,300,300,(LINEDDAPROC)DrawZero,(long)pDC); }
- 添加回调函数代码如下:VOID CALLBACK DrawZero(int X,int Y,LPARAM lpData) { //This Callback routine will be called for every calculated //point in the line. //Implementation Note: Use lpData to pass a reference to a //CDC object for drawing. CDC* pDC; pDC = (CDC*)lpData; if( X % 20 ==0) { pDC->TextOut(X,Y,_T("0")); } }
- 运行结果: