From Nish.
//code starts here
namespace msclr
{
namespace interop
{
template<> System::Drawing::Rectangle
marshal_as<System::Drawing::Rectangle, RECT> (
const RECT& from)
{
return System::Drawing::Rectangle(from.left, from.top,
from.right - from.left, from.bottom - from.top);
}
template<> System::Drawing::Rectangle marshal_as<
System::Drawing::Rectangle, CRect> (
const CRect& from)
{
return System::Drawing::Rectangle(from.left, from.top,
from.Width(), from.Height());
}
template<> RECT marshal_as<RECT, System::Drawing::Rectangle>(
const System::Drawing::Rectangle& from)
{
System::Drawing::Rectangle rectangle = from;
RECT rect = {rectangle.Left, rectangle.Top,
rectangle.Right, rectangle.Bottom};
return rect;
}
template<> CRect marshal_as<CRect, System::Drawing::Rectangle>(
const System::Drawing::Rectangle& from)
{
System::Drawing::Rectangle rectangle = from;
return CRect (rectangle.Left, rectangle.Top,
rectangle.Right, rectangle.Bottom);
}
}
}//code ends here
Why cast away const? Because System::Drawing::Rect is CLS-compliant and the properties (Top, Bottom, Left Right) are not flagged as keeping the object const. We know they don't change it, so we make a copy to use the properties on, thus keeping the compiler happy about our const ref argument.
To use these, it's as you'd expect:
RECT rect = {10, 10, 110, 110};
System::Drawing::Rectangle rectangle = marshal_as<System::Drawing::Rectangle>(rect);
CRect mfcRect(20, 20, 220, 220);
rectangle = marshal_as<System::Drawing::Rectangle>(mfcRect);
RECT rectBack = marshal_as<RECT>(rectangle);
CRect mfcRectBack = marshal_as<CRect>(rectangle);
Kate