using System; using System.Drawing; namespace GoldPrinter { public class Caption : Printer { private string _text; private int _maxRows; private bool _hasborder; public string Text { get { return this._text; } set { this._text = value; } } public bool HasBorder { get { return this._hasborder; } set { this._hasborder = value; } } public int MaxRows { get { return this._maxRows; } set { this._maxRows = value; } } public Caption() { this._text = ""; this._maxRows = 1; this._hasborder = false; this.Font = new Font("宋体", 13f, FontStyle.Italic); this.IsDrawAllPage = true; } public Caption(string text) : this() { this._text = text; } public override void Draw() { base.Draw(); int num = base.PrinterMargins.X1; int num2 = base.PrinterMargins.Y1; num += base.MoveX; num2 += base.MoveY; int width = base.PrinterMargins.Width; int num3 = base.TextHeight(this._text); if (num3 > base.PrinterMargins.Height) { num3 = base.PrinterMargins.Height; } if (this._maxRows > 0) { if (num3 > this.Font.Height * this._maxRows) { num3 = this.Font.Height * this._maxRows; } } Rectangle rectangle = new Rectangle(num, num2, width, num3); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; rectangle.X = (base.PrinterMargins.Width - base.TextWidth(this.Text)) / 2 + base.PrinterMargins.Left + base.MoveX; rectangle.Y = num2; if (base.TextWidth(this.Text) < base.PrinterMargins.Width) { rectangle.Width = base.TextWidth(this.Text); } else { rectangle.Width = base.PrinterMargins.Width; } rectangle.Height = num3; base.Rectangle = rectangle; if (this._hasborder) { base.Graphics.DrawRectangle(base.Pen, base.Rectangle.X, base.Rectangle.Y, base.Rectangle.Width, base.Rectangle.Height); } rectangle.X--; rectangle.Y--; rectangle.Width += 2; rectangle.Height += 2; base.Graphics.DrawString(this._text, this.Font, base.Brush, rectangle, stringFormat); this.Height = num3; } } }