开发者

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 Penclass 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 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜