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

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Drawing.Imaging;
  7 using System.Drawing.Printing;
  8 using System.Drawing.Drawing2D;
  9 using System.Drawing.Text;
 10 using System.Diagnostics;
 11 using System.Runtime.InteropServices;
 12 using System.Drawing;
 13 
 14 namespace Geovin.Du.ControlLibrary
 15 {
 16     /// 
 17     /// 設置字符之間的間距
 18     /// 
 19     /// 
 20     public class GdiPlusMethods
 21     {
 22         #region Declare
 23 
 24         [System.Runtime.InteropServices.DllImport("Gdiplus.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
 25         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);
 26 
 27         [System.Runtime.InteropServices.DllImport("Gdiplus.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
 28         internal extern static int GdipDrawDriverString(IntPtr g, string pText, int pLength, IntPtr pFont, IntPtr pBrush, System.Drawing.PointF[] pPositions, int pFlags, IntPtr pMatrix);
 29 
 30         #endregion Declare
 31 
 32         #region GdiPlusMethods
 33         /// 
 34         /// 
 35         /// 
 36         private GdiPlusMethods()
 37         {
 38         }
 39         #endregion GdiPlusMethods
 40 
 41         #region DriverStringOptions
 42         /// 
 43         /// 
 44         /// 
 45         private enum DriverStringOptions
 46         {
 47             CmapLookup = 1,
 48             Vertical = 2,
 49             Advance = 4,
 50             LimitSubpixel = 8,
 51         }
 52         #endregion DriverStringOptions
 53 
 54         #region DrawDriverString
 55         /// 
 56         /// 字符間距
 57         /// 
 58         /// 
 59         /// 
 60         /// 
 61         /// 
 62         /// 
 63         private static void DrawDriverString(System.Drawing.Graphics g, string pText, System.Drawing.Font pFont, System.Drawing.Brush pBrush, System.Drawing.PointF[] pPositions)
 64         {
 65             try
 66             {
 67                 DrawDriverString(g, pText, pFont, pBrush, pPositions, null);
 68 
 69             }
 70             catch (NullReferenceException NullEx)
 71             {
 72                 throw NullEx;
 73             }
 74             catch (Exception Ex)
 75             {
 76                 throw Ex;
 77             }
 78         }
 79         #endregion DrawDriverString
 80 
 81         #region DrawDriverString
 82         /// 
 83         /// 
 84         /// 
 85         /// 
 86         /// 
 87         /// 
 88         /// 
 89         /// 
 90         /// 
 91         private static void DrawDriverString(System.Drawing.Graphics g, string pText, System.Drawing.Font pFont, System.Drawing.Brush pBrush, System.Drawing.Rectangle pRect, int pHeight)
 92         {
 93             try
 94             {
 95                 System.Drawing.PointF[] mPositions = new System.Drawing.PointF[pText.Length];
 96                 System.Drawing.Size mSize = g.MeasureString(pText, pFont).ToSize();
 97 
 98                 //int mTextHeight = mSize.Height;
 99                 int mRow = 1;
100                 int mPosX = 0;
101 
102                 for (int i = 0; i < pText.Length; i++)
103                 {
104                     mSize = g.MeasureString(pText.Substring(i, 1), pFont).ToSize();
105 
106                     if ((mPosX + mSize.Width) > pRect.Width)
107                     {
108                         mPosX = 0;
109                         mRow = mRow + 1;
110                     }
111 
112                     mPositions[i] = new System.Drawing.PointF(pRect.Left + mPosX, pRect.Top + mRow * pHeight);
113 
114                     mPosX = mPosX + mSize.Width;
115 
116                 }
117 
118                 DrawDriverString(g, pText, pFont, pBrush, mPositions, null);
119 
120             }
121             catch (NullReferenceException NullEx)
122             {
123                 throw NullEx;
124             }
125             catch (Exception Ex)
126             {
127                 throw Ex;
128             }
129         }
130         #endregion DrawDriverString
131 
132         #region DrawDriverString
133         /// 
134         /// 
135         /// 
136         /// 
137         /// 
138         /// 
139         /// 
140         /// 
141         /// 
142         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)
143         {
144             try
145             {
146 
147                 if (g == null)
148                     throw new ArgumentNullException("graphics");
149                 if (pText == null)
150                     throw new ArgumentNullException("text");
151                 if (pFont == null)
152                     throw new ArgumentNullException("font");
153                 if (pBrush == null)
154                     throw new ArgumentNullException("brush");
155                 if (pPositions == null)
156                     throw new ArgumentNullException("positions");
157 
158                 // Get hGraphics
159                 System.Reflection.FieldInfo field = typeof(System.Drawing.Graphics).GetField("nativeGraphics", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
160                 IntPtr hGraphics = (IntPtr)field.GetValue(g);
161 
162                 // Get hFont
163                 field = typeof(System.Drawing.Font).GetField("nativeFont", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
164                 IntPtr hFont = (IntPtr)field.GetValue(pFont);
165 
166                 // Get hBrush
167                 field = typeof(System.Drawing.Brush).GetField("nativeBrush", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
168                 IntPtr hBrush = (IntPtr)field.GetValue(pBrush);
169 
170                 // Get hMatrix
171                 IntPtr hMatrix = IntPtr.Zero;
172                 if (pMatrix != null)
173                 {
174                     field = typeof(System.Drawing.Drawing2D.Matrix).GetField("nativeMatrix", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
175                     hMatrix = (IntPtr)field.GetValue(pMatrix);
176                 }
177 
178                 int result = GdipDrawDriverString(hGraphics, pText, pText.Length, hFont, hBrush, pPositions, (int)DriverStringOptions.CmapLookup, hMatrix);
179             }
180             catch (NullReferenceException NullEx)
181             {
182                 throw NullEx;
183             }
184             catch (Exception Ex)
185             {
186                 throw Ex;
187             }
188         }
189 
190         /// 
191         /// 設置字符間距
192         /// 塗聚文
193         /// 
194         /// Drawing.Graphics
195         /// 字符
196         /// X坐標
197         /// Y坐標
198         /// 字符寬度
199         /// 字體
200         /// 
201         public static void DrawStringCharacterSpacing(System.Drawing.Graphics g, string pText, float x, float y, float width, System.Drawing.Font pFont, System.Drawing.Brush pBrush)
202         {
203 
204             //Get the height of myFont. 
205             float height = pFont.GetHeight(g);
206             //當大於兩個字符時進行設置
207             if (pText.Length >= 2)
208             {
209                 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
210                 SizeF size = g.MeasureString(pText, pFont, int.MaxValue);//int.MaxValue
211                 float h = size.Height;
212                 float w = size.Width;
213 
214                 PointF[] positions = new PointF[pText.Length];
215                 for (int i = 0; i < pText.Length; i++)
216                 {
217                     positions[i] = new PointF(i * width + x, y); //20 為字間距離
218 
219                 }
220 
221                 DrawDriverString(g, pText, pFont, pBrush, positions);
222             }
223 
224         }
225         #endregion DrawDriverString
226 
227     }
228 }

 

你可能感兴趣的:(Csharp: 打印設置字符之間的間距)