Convert Pen to IntPtr
Is there a simple way to convert a System.Drawing.Pen into its unmanaged count开发者_JAVA技巧erpart? Like, if you had a Pen like this:
Pen p = new Pen(Color.Blue, 1f);
IntPtr ptr = p.ToPtr();
I know this code doesn't work, but is there a way to do it similarly?
The Pen
class has an internal property NativePen
which contains exactly what you want. You can access this property through reflection (if your code has the appropriate permissions) using:
Pen p = new Pen(Color.Blue, 1f);
PropertyInfo pi = typeof(Pen).GetProperty("NativePen", BindingFlags.Instance | BindingFlags.NonPublic);
IntPtr ip = (IntPtr)pi.GetValue(p, null);
Be aware that theoretically this might not work in future versions of the .NET-Framework, since internal properties could change ...
精彩评论