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

    }
}
时间: 2024-08-01 09:39:51

Csharp: 打印設置字符之間的間距的相关文章

Csharp 打印Word文件默認打印機設置或選擇打印機設置代碼

//打印文檔 object nullobj = Missing.Value; //aDoc = wordApp.Documents.Open(ref file, // ref nullobj, ref nullobj, ref nullobj, // ref nullobj, ref nullobj, ref nullobj, // ref nullobj, ref nullobj, ref nullobj, // ref nullobj, ref nullobj, ref nullobj, /

jboss 上如何設置 export DISPLAY=localhost:0.0

问题描述 jboss 上如何設置 export DISPLAY=localhost:0.0 jboss 上如何設置 export DISPLAY=localhost:0.0

Android XML設置屏幕方向(android:screenOrientation)详解

Android  XML設置屏幕方向(android:screenOrientation)详解 注意:Android只支持270度旋转. <activity android:name=".MyActivity" android:label="@string/app_name" android:screenOrientation="portrait"> android:screenOrientation设定该活动的方向, 该值可以是任何

如何使用win7电脑的内置字符编辑程序?

  之前,小编经常看到有朋友使用一些奇怪的字体,虽然有些字的写法不是很正规,但是中国比较聪明,人字认半边,也总是可以将字认出来的,这些字虽然错误且难写,但是不得不承认,却看起来非常的创意,用年轻人的话来说,这就是一种fashion,很多人都认为这些字体是在网上下载的,其实不然,在ghost win7中早就具备内置的字符编辑程序,这些字体,我们完全可以自己设计.下面,小编就来分享一下吧! 1.点击开始菜单,然后在搜索框中输入"专用字符编辑程序"并单击回车,这样就可以弹出咱们的编辑器窗口了

csharp customer style print

/// <summary> /// 自定格式設置打印 /// 塗聚文 /// 締友計算機信息技術有限公司 /// Geovin Du /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printDocument1_PrintPage(object sender, P

為 Microsoft Visual Studio .NET 設計工具建立可設計式元件(转贴)上

visual 為 Microsoft Visual Studio .NET 設計工具建立可設計式元件 Shawn BurkeMicrosoft Corporation 2000 年 7 月 摘要:Microsoft .NET 元件於通用語言執行階段,以管理程式碼撰寫建立而成.本文中討論 Microsoft .NET 元件如何提供開發人員一套全新的絕佳混合開發工具,不但類似於 Microsoft Visual Basic,同時提供與 ATL 或 MFC 更具關聯性的低階程式設計能力 (列印頁數共

VS2005智能敏感提示時間過短的問題!

问题描述 郁悶,怎么會碰到這樣的問題!比如我在VS2005中打個"."就彈出敏感提示,但是這敏感提示時間太短了.1秒鐘不到就消失了!請問各位大俠從哪里設置一下?我希望敏感提示時間長一點!非常謝謝了! 解决方案 解决方案二:装了其它插件吧?不然不会消失的解决方案三:裝了什么插件哦?敏感提示沒有消失!只是彈出的時間太短了!不到1秒鐘!就是刷一下彈出有消失了?這什么原因?我只有這么一點分了,希望大俠幫我看下從哪里設置一下?解决方案四:弹出广告窗口被阻止了------------^^解决方案五

水晶报表如何在一页纸中打印相似的两个报表

问题描述 我已经做了一个报表,里面在页眉,详细资料,页脚中都有数据.报表的内容比较短,一般只占A4纸的一半长.现在要在一张纸中打印2张这样的报表.有两种情况,一个是这两个报表完成一样.另外一个是两张报表只是页眉上一张标"正联",一张标"副联".其他内容一样.各位有什么好方法.用的是VS2003自带的水晶报表 解决方案 解决方案二:有两种方法可以解觉你的问题:1.你可以把你已经做的报表改了成另外一种格式,把你所有的对象都放在说细资料节中设详细资料节中每页只显示两条记录

Csharp: print Card using HiTi CS310

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; using System.Drawing.Printing; using S