RECT,将自定义的RECT结构传递到需要RECT的函数中

RECT , passing a custom rect struct into a function that is requireing RECT

本文关键字:RECT 函数 自定义 结构      更新时间:2023-10-16

好的,所以我有一个自定义的rect函数。自定义矩形如下:

typedef struct tagRECTEx{
// long left;
// long top;
// long right;
// long bottom;
RECT dimensions;
int width()) {
    return dimensions.right-dimensions.left;
}
int height(){
    return dimensions.bottom - dimensions.top;
}
} RectEx;

现在不用说:

RECT windowrect;
windowrect = GetWindowRect(hWnd,&windowrect);

我希望它是这样的:

RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex);
....

现在它不会编译,因为它无法将rectex转换为tagRECT,好吧,我明白了,我理解了。

所以在过去的几天里,我一直在搜索自定义强制转换和重写运算符。

我甚至在尝试实现这样的东西:

GetWindowRect(hWnd, (RectEx)&windowrectex);

但不管我在做什么,我就是不知道如何让它发挥作用。

我想使用我自己的rect结构,因为它会自动为我获取rect的宽度和高度,而不是做rect.right-rect.left等。

如果你需要更多关于这个或任何事情的信息,请告诉我。

感谢

由于GetWindowRectLPRECT作为第二个参数,因此无法传递RectEx

您可以使用RectEx做些什么,您可以重载typecasting操作符,如下所示

operator LPRECT () const 
{
   return &dimensions;
}

但是,由于不希望进行类型转换,因此不建议重载类型转换。只有当你确信的时候才这样做。

只需从RectEx内部传递RECT

RectEx windowrectex;
windowrect = GetWindowRect(hWnd,&windowrectex.dimensions);

或者,可以使RectExRECT继承并删除dimensions

或者,添加一个转换运算符,类似于dbasic建议的operator RECT*() const。然后你会使用:

windowrect = GetWindowRect(hWnd,windowrectex);