如何从内存中分配GDI+ POINT类地址?
How to assign GDI+ POINT Class address from memory?
在C++中,我想使用来自准备好的点对 x, y 的函数折线(hdc、apt、cpt(,但我需要在
POINT apt[156];
因为我有存储在 RAM (DWORD( 中的点对,包括通过汇编程序函数调用的对数 (DWORD(。如何在方括号中为 POINT GDI+ 结构分配合适的地址和点对的数量?
作为输入,Polyline()
获取指向POINT
结构数组的指针,以及数组中的元素数。
POINT
有两个数据类型为LONG
的数据成员,因此,如果内存中已有DWORD
对数组,则可以在将该地址传递给Polyline()
时将该地址类型转换为POINT*
指针(因为DWORD
和LONG
在 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;
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- ReadProcessMemory() 不适用于像 0x2840C6C68D8 这样的长地址
- CUDA:统一内存和指针地址的更改
- 当我们从/tp地址中添加/减去一个整数时会发生什么
- 如何从内存中分配GDI+ POINT类地址?