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 /// <summary> 17 /// 設置字符之間的間距 18 /// 19 /// </summary> 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 /// <summary> 34 /// 35 /// </summary> 36 private GdiPlusMethods() 37 { 38 } 39 #endregion GdiPlusMethods 40 41 #region DriverStringOptions 42 /// <summary> 43 /// 44 /// </summary> 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 /// <summary> 56 /// 字符間距 57 /// </summary> 58 /// <param name="g"></param> 59 /// <param name="pText"></param> 60 /// <param name="pFont"></param> 61 /// <param name="pBrush"></param> 62 /// <param name="pPositions"></param> 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 /// <summary> 83 /// 84 /// </summary> 85 /// <param name="g"></param> 86 /// <param name="pText"></param> 87 /// <param name="pFont"></param> 88 /// <param name="pBrush"></param> 89 /// <param name="pRect"></param> 90 /// <param name="pHeight"></param> 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 /// <summary> 134 /// 135 /// </summary> 136 /// <param name="g"></param> 137 /// <param name="pText"></param> 138 /// <param name="pFont"></param> 139 /// <param name="pBrush"></param> 140 /// <param name="pPositions"></param> 141 /// <param name="pMatrix"></param> 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 /// <summary> 191 /// 設置字符間距 192 /// 塗聚文 193 /// </summary> 194 /// <param name="g">Drawing.Graphics</param> 195 /// <param name="pText">字符</param> 196 /// <param name="x">X坐標</param> 197 /// <param name="y">Y坐標</param> 198 /// <param name="width">字符寬度</param> 199 /// <param name="pFont">字體</param> 200 /// <param name="pBrush"></param> 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 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)