RECT,将自定义rect结构传递给需要RECT的函数

时间:2013-11-23 05:22:32

标签: c++ rect

好的,所以我有一个自定义的rect函数。自定义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等。

如果您需要关于此或其他任何信息,请告诉我。

谢谢

2 个答案:

答案 0 :(得分:1)

只需从RECT内传递RectEx

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

或者,您可以RectEx继承RECT并删除dimensions

或者,像dbasic建议的那样添加转换运算符operator RECT*() const。然后你会使用:

windowrect = GetWindowRect(hWnd,windowrectex);

答案 1 :(得分:1)

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

使用RectEx可以做什么,您可以按如下方式重载类型转换操作符

operator LPRECT () const 
{
   return &dimensions;
}

但是,由于不希望的类型转换,不建议重载类型转换。只有在你确定的时候才会这样做。