Csharp: 打印設置字符之間的間距

简介: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Drawing.Imaging; using System.Drawing.Printing; using System.Drawing.Draw
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Geovin.Du.ControlLibrary
{
    /// <summary>
    /// 設置字符之間的間距
    /// 
    /// </summary>
    public class GdiPlusMethods
    {
        #region Declare

        [System.Runtime.InteropServices.DllImport("Gdiplus.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
        internal extern static int GdipMeasureDriverString(IntPtr g, string pText, int pLength, IntPtr pFont, System.Drawing.PointF[] pPositions, int pFlags, IntPtr pMatrix, ref System.Drawing.RectangleF pBounds);

        [System.Runtime.InteropServices.DllImport("Gdiplus.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
        internal extern static int GdipDrawDriverString(IntPtr g, string pText, int pLength, IntPtr pFont, IntPtr pBrush, System.Drawing.PointF[] pPositions, int pFlags, IntPtr pMatrix);

        #endregion Declare

        #region GdiPlusMethods
        /// <summary>
        /// 
        /// </summary>
        private GdiPlusMethods()
        {
        }
        #endregion GdiPlusMethods

        #region DriverStringOptions
        /// <summary>
        /// 
        /// </summary>
        private enum DriverStringOptions
        {
            CmapLookup = 1,
            Vertical = 2,
            Advance = 4,
            LimitSubpixel = 8,
        }
        #endregion DriverStringOptions

        #region DrawDriverString
        /// <summary>
        /// 字符間距
        /// </summary>
        /// <param name="g"></param>
        /// <param name="pText"></param>
        /// <param name="pFont"></param>
        /// <param name="pBrush"></param>
        /// <param name="pPositions"></param>
        private static void DrawDriverString(System.Drawing.Graphics g, string pText, System.Drawing.Font pFont, System.Drawing.Brush pBrush, System.Drawing.PointF[] pPositions)
        {
            try
            {
                DrawDriverString(g, pText, pFont, pBrush, pPositions, null);

            }
            catch (NullReferenceException NullEx)
            {
                throw NullEx;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        #endregion DrawDriverString

        #region DrawDriverString
        /// <summary>
        /// 
        /// </summary>
        /// <param name="g"></param>
        /// <param name="pText"></param>
        /// <param name="pFont"></param>
        /// <param name="pBrush"></param>
        /// <param name="pRect"></param>
        /// <param name="pHeight"></param>
        private static void DrawDriverString(System.Drawing.Graphics g, string pText, System.Drawing.Font pFont, System.Drawing.Brush pBrush, System.Drawing.Rectangle pRect, int pHeight)
        {
            try
            {
                System.Drawing.PointF[] mPositions = new System.Drawing.PointF[pText.Length];
                System.Drawing.Size mSize = g.MeasureString(pText, pFont).ToSize();

                //int mTextHeight = mSize.Height;
                int mRow = 1;
                int mPosX = 0;

                for (int i = 0; i < pText.Length; i++)
                {
                    mSize = g.MeasureString(pText.Substring(i, 1), pFont).ToSize();

                    if ((mPosX + mSize.Width) > pRect.Width)
                    {
                        mPosX = 0;
                        mRow = mRow + 1;
                    }

                    mPositions[i] = new System.Drawing.PointF(pRect.Left + mPosX, pRect.Top + mRow * pHeight);

                    mPosX = mPosX + mSize.Width;

                }

                DrawDriverString(g, pText, pFont, pBrush, mPositions, null);

            }
            catch (NullReferenceException NullEx)
            {
                throw NullEx;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        #endregion DrawDriverString

        #region DrawDriverString
        /// <summary>
        /// 
        /// </summary>
        /// <param name="g"></param>
        /// <param name="pText"></param>
        /// <param name="pFont"></param>
        /// <param name="pBrush"></param>
        /// <param name="pPositions"></param>
        /// <param name="pMatrix"></param>
        private static void DrawDriverString(System.Drawing.Graphics g, string pText, System.Drawing.Font pFont, System.Drawing.Brush pBrush, System.Drawing.PointF[] pPositions, System.Drawing.Drawing2D.Matrix pMatrix)
        {
            try
            {

                if (g == null)
                    throw new ArgumentNullException("graphics");
                if (pText == null)
                    throw new ArgumentNullException("text");
                if (pFont == null)
                    throw new ArgumentNullException("font");
                if (pBrush == null)
                    throw new ArgumentNullException("brush");
                if (pPositions == null)
                    throw new ArgumentNullException("positions");

                // Get hGraphics
                System.Reflection.FieldInfo field = typeof(System.Drawing.Graphics).GetField("nativeGraphics", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                IntPtr hGraphics = (IntPtr)field.GetValue(g);

                // Get hFont
                field = typeof(System.Drawing.Font).GetField("nativeFont", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                IntPtr hFont = (IntPtr)field.GetValue(pFont);

                // Get hBrush
                field = typeof(System.Drawing.Brush).GetField("nativeBrush", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                IntPtr hBrush = (IntPtr)field.GetValue(pBrush);

                // Get hMatrix
                IntPtr hMatrix = IntPtr.Zero;
                if (pMatrix != null)
                {
                    field = typeof(System.Drawing.Drawing2D.Matrix).GetField("nativeMatrix", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    hMatrix = (IntPtr)field.GetValue(pMatrix);
                }

                int result = GdipDrawDriverString(hGraphics, pText, pText.Length, hFont, hBrush, pPositions, (int)DriverStringOptions.CmapLookup, hMatrix);
            }
            catch (NullReferenceException NullEx)
            {
                throw NullEx;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// 設置字符間距
        /// 塗聚文
        /// </summary>
        /// <param name="g">Drawing.Graphics</param>
        /// <param name="pText">字符</param>
        /// <param name="x">X坐標</param>
        /// <param name="y">Y坐標</param>
        /// <param name="width">字符寬度</param>
        /// <param name="pFont">字體</param>
        /// <param name="pBrush"></param>
        public static void DrawStringCharacterSpacing(System.Drawing.Graphics g, string pText, float x, float y, float width, System.Drawing.Font pFont, System.Drawing.Brush pBrush)
        {

            //Get the height of myFont. 
            float height = pFont.GetHeight(g);
            //當大於兩個字符時進行設置
            if (pText.Length >= 2)
            {
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                SizeF size = g.MeasureString(pText, pFont, int.MaxValue);//int.MaxValue
                float h = size.Height;
                float w = size.Width;

                PointF[] positions = new PointF[pText.Length];
                for (int i = 0; i < pText.Length; i++)
                {
                    positions[i] = new PointF(i * width + x, y); //20 為字間距離

                }

                DrawDriverString(g, pText, pFont, pBrush, positions);
            }

        }
        #endregion DrawDriverString

    }
}

目录
相关文章
|
存储 算法
探索字符与字符串:基本库函数的使用(二)
探索字符与字符串:基本库函数的使用(二)
37 0
|
2月前
|
开发框架 .NET 程序员
C# 去掉字符串最后一个字符的 4 种方法
在实际业务中,我们经常会遇到在循环中拼接字符串的场景,循环结束之后拼接得到的字符串的最后一个字符往往需要去掉,看看 C# 提供了哪4种方法可以高效去掉字符串的最后一个字符
238 0
|
2月前
|
Python
[oeasy]python035_根据序号得到字符_chr函数_字符_character_
本文介绍了Python中的`ord()`和`chr()`函数。`ord()`函数通过字符找到对应的序号,而`chr()`函数则根据序号找到对应的字符。两者互为逆运算,可以相互转换。文章还探讨了单双引号在字符串中的作用,并解释了中文字符和emoji也有对应的序号。最后总结了`ord()`和`chr()`函数的特点,并提供了学习资源链接。
28 4
|
6月前
字符拼接的深入理解
字符拼接的深入理解
29 0
字符串格式化输出
字符串格式化输出
|
6月前
|
JavaScript 前端开发 API
|
6月前
|
存储 安全 C语言
c++字符串
c++字符串
53 1
|
6月前
|
Java
Java正则表达式去掉非汉字字符
【5月更文挑战第11天】Java正则表达式去掉非汉字字符
125 3
|
6月前
|
C#
C#字符串
C#字符串
43 0
|
6月前
QT去除QString字符串中空格的方法
QT去除QString字符串中空格的方法
230 0