Use different implementations for accessing GDI+ internals on .Net and Mono.

This commit is contained in:
the_fiddler 2008-11-03 23:34:54 +00:00
parent f4700cb676
commit f218071fa7
4 changed files with 108 additions and 21 deletions

View file

@ -18,6 +18,8 @@ namespace OpenTK.Platform
{ {
internal static class GdiPlus internal static class GdiPlus
{ {
static IGdiPlusInternals internals;
const string gdi_plus_library = "gdiplus.dll"; const string gdi_plus_library = "gdiplus.dll";
static readonly PropertyInfo native_graphics_property, native_font_property; static readonly PropertyInfo native_graphics_property, native_font_property;
static readonly FieldInfo native_string_format_field; static readonly FieldInfo native_string_format_field;
@ -26,33 +28,29 @@ namespace OpenTK.Platform
static GdiPlus() static GdiPlus()
{ {
native_graphics_property = if (Configuration.RunningOnWindows)
typeof(System.Drawing.Graphics).GetProperty("NativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic); internals = new Windows.WinGdiPlusInternals();
else
native_font_property = internals = new X11.X11GdiPlusInternals();
typeof(System.Drawing.Font).GetProperty("NativeFont", BindingFlags.Instance | BindingFlags.NonPublic);
native_string_format_field =
typeof(System.Drawing.StringFormat).GetField("nativeFormat", BindingFlags.Instance | BindingFlags.NonPublic);
} }
#endregion #endregion
#region --- Reflection --- #region --- Public Methods ---
public static IntPtr GetNativeGraphics(System.Drawing.Graphics graphics) public static IntPtr GetNativeGraphics(System.Drawing.Graphics graphics)
{ {
return (IntPtr)native_graphics_property.GetValue(graphics, null); return internals.GetNativeGraphics(graphics);
} }
public static IntPtr GetNativeFont(Font font) public static IntPtr GetNativeFont(Font font)
{ {
return (IntPtr)native_font_property.GetValue(font, null); return internals.GetNativeFont(font);
} }
public static IntPtr GetNativeStringFormat(StringFormat format) public static IntPtr GetNativeStringFormat(StringFormat format)
{ {
return (IntPtr)native_string_format_field.GetValue(format); return internals.GetNativeStringFormat(format);
} }
public static int MaxMeasurableCharacterRanges public static int MaxMeasurableCharacterRanges
@ -63,11 +61,7 @@ namespace OpenTK.Platform
} }
} }
#endregion [DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint = "GdipSetStringFormatMeasurableCharacterRanges")]
#region --- Methods ---
[DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint="GdipSetStringFormatMeasurableCharacterRanges")]
public static extern int SetStringFormatMeasurableCharacterRanges(HandleRef format, int rangeCount, [In, Out] CharacterRange[] range); public static extern int SetStringFormatMeasurableCharacterRanges(HandleRef format, int rangeCount, [In, Out] CharacterRange[] range);
[DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint = "GdipGetStringFormatMeasurableCharacterRangeCount")] [DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint = "GdipGetStringFormatMeasurableCharacterRangeCount")]
@ -87,6 +81,7 @@ namespace OpenTK.Platform
#endregion #endregion
#if false
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct GPRECTF internal struct GPRECTF
{ {
@ -122,9 +117,6 @@ namespace OpenTK.Platform
return new RectangleF(this.X, this.Y, this.Width, this.Height); return new RectangleF(this.X, this.Y, this.Width, this.Height);
} }
} }
#endif
} }
} }

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Platform
{
// Provides methods to access internal GdiPlus fields. This is necessary for
// managed <-> native GdiPlus interoperability.
// Note that the fields are named differently between .Net and Mono.
// GdiPlus is considered deprecated by Microsoft - it is highly unlikely that
// future framework upgrades will break this code, but it is something to
// keep in mind.
interface IGdiPlusInternals
{
IntPtr GetNativeGraphics(System.Drawing.Graphics graphics);
IntPtr GetNativeFont(System.Drawing.Font font);
IntPtr GetNativeStringFormat(System.Drawing.StringFormat format);
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Reflection;
namespace OpenTK.Platform.Windows
{
class WinGdiPlusInternals : IGdiPlusInternals
{
const string gdi_plus_library = "gdiplus.dll";
static readonly PropertyInfo native_graphics_property, native_font_property;
static readonly FieldInfo native_string_format_field;
static WinGdiPlusInternals()
{
native_graphics_property =
typeof(System.Drawing.Graphics).GetProperty("NativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic);
native_font_property =
typeof(System.Drawing.Font).GetProperty("NativeFont", BindingFlags.Instance | BindingFlags.NonPublic);
native_string_format_field =
typeof(System.Drawing.StringFormat).GetField("nativeFormat", BindingFlags.Instance | BindingFlags.NonPublic);
}
#region --- IGdiPlusInternals Members ---
public IntPtr GetNativeGraphics(System.Drawing.Graphics graphics)
{
return (IntPtr)native_graphics_property.GetValue(graphics, null);
}
public IntPtr GetNativeFont(Font font)
{
return (IntPtr)native_font_property.GetValue(font, null);
}
public IntPtr GetNativeStringFormat(StringFormat format)
{
return (IntPtr)native_string_format_field.GetValue(format);
}
#endregion
}
}

View file

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Platform.X11
{
class X11GdiPlusInternals : IGdiPlusInternals
{
#region IGdiPlusInternals Members
public IntPtr GetNativeGraphics(System.Drawing.Graphics graphics)
{
throw new NotImplementedException();
}
public IntPtr GetNativeFont(System.Drawing.Font font)
{
throw new NotImplementedException();
}
public IntPtr GetNativeStringFormat(System.Drawing.StringFormat format)
{
throw new NotImplementedException();
}
#endregion
}
}