在项目里碰到这个函数,不知道怎么使用,记录在这里。
该函数的原型如下:
BOOL LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd, LINEDDAPROC lpLineFunc, LPARAM lpData);
参数说明如下:
nXStart:起点的X值
nYStart:起点的Y值
nXEnd:终点的X值
nYEnd:终点的Y值
lpLineFunc:回调函数的地址
lpData:用户自定义参数(这个参数会传给回调函数)
这个函数和动画其实没什么关系,它的功能就是计算出连接两点的线段上的每一个屏幕像素的坐标,这两个点的坐标已经在函数的前四个参数中给出。
每计算出一个坐标,该函数就会调用第五个参数所指的回调函数,我们可以在回调函数中完成一些简单的操作,以实现动画效果。
回调函数的原型是: VOID CALLBACK LineDDAProc(int X, int Y, LPARAM lpData);
前两个参数是点的坐标,第三个参数就是由LineDDA传过来的自定义参数,是由我们自己指定的,传什么都行。 :)
详解:
实现以复杂线条为基础的图形绘图
在GIS(地理信息系统)类软件设计中经常需要在绘图时使用一些相对固定但又频繁使用的一些用以代表地理状态的符号如河流、铁路、海岸线等等。每一种符号均有其各自的风格,但在不同的位置的具体表示却不尽相同,比如代表铁路的符号是一段黑白相间的细矩形,但有时是平直的,在拐弯时用弯曲的矩形来表示。因此对于上述符号的绘制一般不易用固定的图标去实现,而多采用灵活多变的用函数来直接绘制的方法。显然作为GIS基本符号的图形一般都是相对比较复杂的线条,在MFC提供的基本类库中并未提供可以直接使用的相关函数。即使是在绘图功能比较强大的CDC中也仅仅提供了LineTo()、SetPixel()等一些通用的最基本的绘图函数,虽然也可以使用这些基本函数来绘制GIS里的基本符号,但这是效率比较低下的一种办法,这在大量的绘图操作中将会表现的比较明显,因此不宜提倡。本文下面将介绍一种使用Win32 API函数LineDDA来绘制复杂风格线条的方法来解决上述类似问题。
运行的程序代码:
void CMyFrameWnd::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);
dc.SetTextAlign(TA_BOTTOM | TA_CENTER);
::LineDDA(50, 50, rect.right / 2, rect.bottom / 2,
(LINEDDAPROC)LineDDACallback, (LPARAM)(LPVOID)&dc);
}
//--------------------------------------------------------------------
VOID CALLBACK CMyFrameWnd::LineDDACallback(int x, int y, LPARAM lpData)
{
CDC* pDC = (CDC*)lpData;
pDC->SetPixel(x, y+ sin(x), RGB(255, 0, 0));
Sleep(1); //延时一下,以便观察
}
MSDN的解释的地址:
https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k(WINGDI%2FLineDDA);k(LineDDA);k(DevLang-C%2B%2B);k(TargetOS-Windows)&rd=true
LineDDA function
The LineDDA function determines which pixels should be highlighted for a line defined by the specified starting and ending points.
Syntax
C++
BOOL LineDDA(
_In_ int nXStart,
_In_ int nYStart,
_In_ int nXEnd,
_In_ int nYEnd,
_In_ LINEDDAPROC lpLineFunc,
_In_ LPARAM lpData
);
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.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Remarks
The LineDDA function passes the coordinates for each point along the line, except for the line's ending point, to the application-defined callback function. In addition to passing the coordinates of a point, this function passes any existing application-defined data.
The coordinates passed to the callback function match pixels on a video display only if the default transformations and mapping modes are used.
Requirements
Minimum supported client
Windows 2000 Professional [desktop apps only]
Minimum supported server
Windows 2000 Server [desktop apps only]
Header
Wingdi.h (include Windows.h)
Library
Gdi32.lib
DLL
Gdi32.dll