using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Reflection;
namespace GdiplusMethods
{
public class GdiplusMethods
{
private GdiplusMethods() { }
private enum DriverStringOptions
{
CmapLookup = 1,
Vertical = 2,
Advance = 4,
LimitSubpixel = 8,
}
public static void DrawDriverString(Graphics graphics, string text,
Font font, Brush brush, PointF[] positions)
{
DrawDriverString(graphics, text, font, brush, positions, null);
}
public static void DrawDriverString(Graphics graphics, string text,
Font font, Brush brush, PointF[] positions, Matrix matrix)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (text == null)
throw new ArgumentNullException("text");
if (font == null)
throw new ArgumentNullException("font");
if (brush == null)
throw new ArgumentNullException("brush");
if (positions == null)
throw new ArgumentNullException("positions");
// Get hGraphics
FieldInfo field = typeof(Graphics).GetField("nativeGraphics",
BindingFlags.Instance | BindingFlags.NonPublic);
IntPtr hGraphics = (IntPtr)field.GetValue(graphics);
// Get hFont
field = typeof(Font).GetField("nativeFont",
BindingFlags.Instance | BindingFlags.NonPublic);
IntPtr hFont = (IntPtr)field.GetValue(font);
// Get hBrush
field = typeof(Brush).GetField("nativeBrush",
BindingFlags.Instance | BindingFlags.NonPublic);
IntPtr hBrush = (IntPtr)field.GetValue(brush);
// Get hMatrix
IntPtr hMatrix = IntPtr.Zero;
if (matrix != null)
{
field = typeof(Matrix).GetField("nativeMatrix",
BindingFlags.Instance | BindingFlags.NonPublic);
hMatrix = (IntPtr)field.GetValue(matrix);
}
int result = GdipDrawDriverString(hGraphics, text, text.Length,
hFont, hBrush, positions, (int)DriverStringOptions.CmapLookup, hMatrix);
}
[DllImport("Gdiplus.dll", CharSet = CharSet.Unicode)]
internal extern static int GdipMeasureDriverString(IntPtr graphics,
string text, int length, IntPtr font, PointF[] positions,
int flags, IntPtr matrix, ref RectangleF bounds);
[DllImport("Gdiplus.dll", CharSet = CharSet.Unicode)]
internal extern static int GdipDrawDriverString(IntPtr graphics,
string text, int length, IntPtr font, IntPtr brush,
PointF[] positions, int flags, IntPtr matrix);
}
}
//オブジェクトを取得します。
Graphics g = e.Graphics;
string sPrint =“但是dssadfSS”;
float LeftMargin =5.0f;
float topMargin =5.0f;
for (int n = 0; n < sPrint.Length; n++ )
{
string fortext = sPrint.Substring(0, n);
int XPos = Encoding.Default.GetBytes(fortext).Length;
positions[n] = new PointF(XPos * (fnt.Size - 5.0f) + LeftMargin, topMargin + ((count + m) * fnt.GetHeight(g)));
} //行の表示位置を計算
GdiplusMethods.DrawDriverString(g, sPrint, fnt, Brushes.Black, positions);
浙公网安备 33010602011771号