Thursday, December 13, 2007

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

posted on 12/13/2007 5:05:57 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]

Related Posts:
HFONT and System::Drawing::Font
_GUID and System::Guid
Some handy file system flags

Tracked by:
http://www.google.com/search?q=tsjolpyw [Pingback]
http://www.surfrider.org/press_releases.aspx?PR_Id=223 [Pingback]
http://www.landandfarm.com/lf/news/display_article.asp?cid=681 [Pingback]
http://www.datcp.state.wi.us/press_release/result.jsp?prid=2103 [Pingback]
http://www.surfrider.org/press_releases.aspx?PR_Id=225 [Pingback]
http://www.ovt.com/products/part_detail.asp?id=189 [Pingback]
http://www.datcp.state.wi.us/press_release/result.jsp?prid=2112 [Pingback]
http://www.landandfarm.com/lf/news/display_article.asp?cid=663 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=189 [Pingback]
http://www.datcp.state.wi.us/press_release/result.jsp?prid=2086 [Pingback]
http://www.xbrl.org/us/nmpxbrl.aspx?id=217 [Pingback]
http://yesihavemoneyy.com [Pingback]
http://tubepornoss.com [Pingback]
http://realvideopornoo.com [Pingback]
http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/nexium/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cymbalta/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/clomid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/rainbow-brite/index.html [Pingback]
http://blastpr.com/wiki/js/pages/celebrex/index.html [Pingback]
http://blastpr.com/wiki/js/pages/prozac/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/accutane/index.html [Pingback]
http://blastpr.com/wiki/js/pages/coumadin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/hoodia/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prilosec/index.html [Pingback]
http://blastpr.com/wiki/js/pages/nexium/index.html [Pingback]
http://blastpr.com/wiki/js/pages/clomid/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/soma/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cialis/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/synthroid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prozac/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cymbalta/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celexa/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/wellbutrin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/tramadol/index.html [Pingback]
http://blastpr.com/wiki/js/pages/prilosec/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cialis/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celebrex/index.html [Pingback]
http://blastpr.com/wiki/js/pages/synthroid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lipitor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/effexor/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/ultram/index.html [Pingback]
http://blastpr.com/wiki/js/pages/viagra/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lipitor/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/claritin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/tramadol/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/coumadin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/effexor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/soma/index.html [Pingback]