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

    }
}

目录
相关文章
|
8月前
|
人工智能 自然语言处理 数据可视化
DeepSeek使用终极指南:解锁国产大模型的隐藏实力
DeepSeek作为国产大语言模型的佼佼者,支持多模态交互,在编码、数学和逻辑推理等方面表现卓越。本文从基础操作到进阶技巧全面解析其高效使用方法,涵盖精准提问法则、文件交互技巧、高级指令应用等,并提供智能客服、数据分析、教育培训等典型场景实战案例。同时提醒用户注意提问禁忌与安全规范,帮助开发者和普通用户充分挖掘DeepSeek的潜能,提升工作效率,探索智能解决方案。
676 0
|
12月前
|
存储 人工智能 对象存储
一文详解阿里云AI大基建
一文详解阿里云AI大基建
1875 2
|
算法 IDE API
Zipline 3.0 中文文档(一)(4)
Zipline 3.0 中文文档(一)
265 3
|
编译器 C语言 Windows
windows MinGW C语言编译器安装及环境变量配置教程
MinGW被称为Windows版的GCC,安装包下载地址:提示:该安装包下载完之后,相当于安装好了MinGW,之后即可配置环境变量!所以,可以先新建好一个专门用来存放MinGW安装包的文件夹。
675 2
|
缓存 测试技术 Shell
详细解读Android开发命令行完全攻略
详细解读Android开发命令行完全攻略
161 1
|
JavaScript 前端开发 测试技术
JMeter 逻辑控制之While循环控制器(While Controller)
JMeter 逻辑控制之While循环控制器(While Controller)
2144 0
|
存储 缓存 Java
Spring Cache For Redis.
一、概述      缓存(Caching)可以存储经常会用到的信息,这样每次需要的时候,这些信息都是立即可用的。      常用的缓存数据库: Redis   使用内存存储(in-memory)的非关系数据库,字符串、列表、集合、散列表、有序集合,每种数据类型都有自己的专属命令。
3236 0
|
2天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。

热门文章

最新文章