www.pudn.com > PrinterPageSetting.rar > DrawText.cs
using System;
using System.Drawing;
using System.Drawing.Printing;
namespace GoldPrinter
{
///
/// 在绘图表面区域内绘制文本
///
public class DrawText
{
private Graphics _graphics;
private RectangleF _rectangleF;
private string _text;
private Font _font;
private Brush _brush;
private StringFormat _stringFormat;
private int _startChar;
private int _linesFilled;
private int _charsFitted;
#region 字段属性
public Graphics Graphics
{
get
{
return _graphics;
}
set
{
_graphics = value;
}
}
public RectangleF RectangleF
{
get
{
return _rectangleF;
}
set
{
_rectangleF = value;
}
}
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
public Font Font
{
get
{
return _font;
}
set
{
if (value != null)
{
_font = value;
}
}
}
public Brush Brush
{
get
{
return _brush;
}
set
{
if (value != null)
{
_brush = value;
}
}
}
public StringFormat StringFormat
{
get
{
return _stringFormat;
}
set
{
_stringFormat = value;
}
}
public int StartChar
{
get
{
return _startChar;
}
set
{
_startChar = value;
if (_startChar < 0)
{
_startChar = 0;
}
}
}
public int CharsFitted
{
get
{
return _charsFitted;
}
}
public int LinesFilled
{
get
{
return _linesFilled;
}
}
#endregion
public DrawText()
{
_text = "";
_font = new Font("宋体",11);
_rectangleF = new RectangleF(0,0,0,_font.Height);
_brush = Brushes.Black;
_startChar = 0;
_linesFilled = 0;
_charsFitted = 0;
_stringFormat = new StringFormat(StringFormatFlags.LineLimit);
}
public DrawText(string text):this()
{
_text = text;
}
public void Draw()
{
if (_graphics != null)
{
int intLinesFilled, intCharsFitted;
_graphics.MeasureString(_text.Substring(_startChar),_font,new SizeF(_rectangleF.Width, _rectangleF.Height),_stringFormat,out intCharsFitted,out intLinesFilled);
_graphics.DrawString(_text.Substring(_startChar),_font,_brush,_rectangleF,_stringFormat);
this._linesFilled = intLinesFilled;
this._charsFitted = intCharsFitted;
}
}
}//End Class
}//End Namespace