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; //remove const 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; //remove const 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:
//To Rectangle 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); //From Rectangle RECT rectBack = marshal_as<RECT>(rectangle); CRect mfcRectBack = marshal_as<CRect>(rectangle);
Kate
Remember Me
Page rendered at 7/23/2008 9:06:42 PM (Eastern Daylight Time, UTC-04:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.