如何从内存中分配GDI+ POINT类地址?

How to assign GDI+ POINT Class address from memory?

本文关键字:POINT 地址 GDI+ 分配 内存      更新时间:2023-10-16

在C++中,我想使用来自准备好的点对 x, y 的函数折线(hdc、apt、cpt(,但我需要在

POINT apt[156];

因为我有存储在 RAM (DWORD( 中的点对,包括通过汇编程序函数调用的对数 (DWORD(。如何在方括号中为 POINT GDI+ 结构分配合适的地址和点对的数量?

作为输入,Polyline()获取指向POINT结构数组的指针,以及数组中的元素数。

POINT有两个数据类型为LONG的数据成员,因此,如果内存中已有DWORD对数组,则可以在将该地址传递给Polyline()时将该地址类型转换为POINT*指针(因为DWORDLONG在 Win32 API 中具有相同的字节大小(, 例如:

DWORD *pairs = ...;
DWORD numPairs = ...;
...    
Polyline(hdc, reinterpret_cast<POINT*>(pairs), numPairs);

但是,更安全的方法是简单地在内存中分配一个单独的POINT[]数组并将DWORD值复制到其中,然后将该数组传递给Polyline(),例如:

DWORD *pairs = ...;
DWORD numPairs = ...;
...    
POINT *points = new POINT[numPairs];
for(DWORD i = 0; i < numPairs; ++i)
{
points[i].x = pairs[i*2];
points[i].y = pairs[(i*2)+1];
}
Polyline(hdc, points, numPairs);
delete[] points;

仅供参考,在您的问题中,您提到了GDI+,但Polyline()是GDI的一部分,而不是GDI+。 GDI+ 等效是Graphics::DrawLines(),但它需要一个Point类对象的数组,而不是POINT结构。 你不能安全地将一个DWORD数组类型转换为Point*,你必须实际构造单独的Point对象(使用Point(int,int)构造函数(,类似于上面,例如:

DWORD *pairs = ...;
DWORD numPairs = ...;
...    
Point *points = new Point[numPairs];
for(DWORD i = 0; i < numPairs; ++i)
points[i] = Point(static_cast<INT>(pairs[i*2]), static_cast<INT>(pairs[(i*2)+1]));
Graphics *g = Graphics::FromHDC(hdc);
Pen pen(...);
g->DrawLines(&pen, points, numPairs);
delete g;
delete[] points;