Tuesday, October 16, 2007

i Started C++ Cli

found a site http://www.voidnish.com/
the marshal_as library that is introduced in Orcas and how it’s very useful for mixed-mode string conversions. While the built-in functionality only allows string conversions, it’s also possible to extend marshal_as functionality to support other type conversions. As an example of doing this, I have written a sample extension that supports converting between the Windows Forms Rectangle structure and the Win32 RECT structure. For the sake of completion I have also added specializations for the MFC CRect wrapper (which is a thin wrapper around the RECT structure).

namespace msclr
{
namespace interop
{
template<> System::Drawing::Rectangle
marshal_as (
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(
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(
const System::Drawing::Rectangle& from)
{
System::Drawing::Rectangle rectangle = from; //remove const
return CRect (rectangle.Left, rectangle.Top,
rectangle.Right, rectangle.Bottom);
}
}
}

1 comment:

Anonymous said...

Thanks for your blog but one thing I want to mention
if you are writing a code block
please make it neat and tidy.
It is too hard to understand the code that you written here