public static int Build(XmlDocument dom, out LabelParam label_param, out string strError) { strError = ""; int nRet = 0; label_param = new LabelParam(); XmlNode label = dom.DocumentElement.SelectSingleNode("label"); if (label != null) { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(label, "width", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.LabelWidth = fValue; // height nRet = DomUtil.GetDoubleParam(label, "height", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.LabelHeight = fValue; // lineSep nRet = DomUtil.GetDoubleParam(label, "lineSep", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.LineSep = fValue; { DecimalPadding margins; string strPaddings = DomUtil.GetAttr(label, "paddings"); nRet = GetMarginValue( strPaddings, out margins, out strError); if (nRet == -1) { strError = "<label>元素paddings属性值格式错误: " + strError; return(-1); } label_param.LabelPaddings = margins; } #if NO try { string strPaddings = DomUtil.GetAttr(label, "paddings"); string[] parts = strPaddings.Split(new char[] { ',' }); if (parts.Length > 0) { label_param.LabelPaddings.Left = Convert.ToInt32(parts[0]); } if (parts.Length > 1) { label_param.LabelPaddings.Top = Convert.ToInt32(parts[1]); } if (parts.Length > 2) { label_param.LabelPaddings.Right = Convert.ToInt32(parts[2]); } if (parts.Length > 3) { label_param.LabelPaddings.Bottom = Convert.ToInt32(parts[3]); } } catch (Exception ex) { strError = "<label>元素paddings属性值格式错误: " + ex.Message; return(-1); } #endif string strFont = DomUtil.GetAttr(label, "font"); if (String.IsNullOrEmpty(strFont) == false) { if (Global.IsVirtualBarcodeFont(ref strFont) == true) { label_param.IsBarcodeFont = true; } try { label_param.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<label>元素 font 属性值格式错误: " + ex.Message; return(-1); } } } XmlNode page = dom.DocumentElement.SelectSingleNode("page"); if (page != null) { { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(page, "width", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.PageWidth = fValue; // height nRet = DomUtil.GetDoubleParam(page, "height", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.PageHeight = fValue; } { DecimalPadding margins; string strMargins = DomUtil.GetAttr(page, "margins"); nRet = GetMarginValue( strMargins, out margins, out strError); if (nRet == -1) { strError = "<page>元素margins属性值格式错误: " + strError; return(-1); } label_param.PageMargins = margins; } #if NO try { string strMargins = DomUtil.GetAttr(page, "margins"); string[] parts = strMargins.Split(new char[] { ',' }); if (parts.Length > 0) { label_param.PageMargins.Left = Convert.ToInt32(parts[0]); } if (parts.Length > 1) { label_param.PageMargins.Top = Convert.ToInt32(parts[1]); } if (parts.Length > 2) { label_param.PageMargins.Right = Convert.ToInt32(parts[2]); } if (parts.Length > 3) { label_param.PageMargins.Bottom = Convert.ToInt32(parts[3]); } } catch (Exception ex) { strError = "<page>元素margins属性值格式错误: " + ex.Message; return(-1); } #endif label_param.DefaultPrinter = DomUtil.GetAttr(page, "defaultPrinter"); #if NO bool bValue = false; nRet = DomUtil.GetBooleanParam(page, "landscape", false, out bValue, out strError); if (nRet == -1) { strError = "<page> 元素的 landscape 属性错误: " + strError; return(-1); } label_param.Landscape = bValue; #endif int nValue = 0; DomUtil.GetIntegerParam(page, "rotate", 0, out nValue, out strError); if (nRet == -1) { strError = "<page> 元素的 rotate 属性错误: " + strError; return(-1); } label_param.RotateDegree = nValue; } XmlNodeList nodes = dom.DocumentElement.SelectNodes("lineFormats/line"); label_param.LineFormats = new List <LineFormat>(); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; string strFont = DomUtil.GetAttr(node, "font"); LineFormat format = new LineFormat(); if (Global.IsVirtualBarcodeFont(ref strFont) == true) { format.IsBarcodeFont = true; } if (string.IsNullOrEmpty(strFont) == false) { try { format.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<line>元素font属性值格式错误: " + ex.Message; return(-1); } } else { format.Font = null; // 继承页面的字体 } format.Align = DomUtil.GetAttr(node, "align"); string strOffset = DomUtil.GetAttr(node, "offset"); if (string.IsNullOrEmpty(strOffset) == false) { try { double left = 0; double right = 0; ParsetTwoDouble(strOffset, false, out left, out right); format.OffsetX = left; format.OffsetY = right; } catch (Exception ex) { strError = "<line>元素offset属性值格式错误: " + ex.Message; return(-1); } } string strStart = DomUtil.GetAttr(node, "start"); if (string.IsNullOrEmpty(strStart) == false) { try { double left = double.NaN; double right = double.NaN; ParsetTwoDouble(strStart, true, out left, out right); format.StartX = left; format.StartY = right; } catch (Exception ex) { strError = "<line>元素start属性值格式错误: " + ex.Message; return(-1); } } string strSize = DomUtil.GetAttr(node, "size"); if (string.IsNullOrEmpty(strSize) == false) { try { double left = double.NaN; double right = double.NaN; ParsetTwoDouble(strSize, true, out left, out right); format.Width = left; format.Height = right; } catch (Exception ex) { strError = "<line>元素size属性值格式错误: " + ex.Message; return(-1); } } format.ForeColor = DomUtil.GetAttr(node, "foreColor"); format.BackColor = DomUtil.GetAttr(node, "backColor"); label_param.LineFormats.Add(format); } return(0); }
// 可能会抛出异常 static void SetFormatStart(LineFormat format, string strStart) { string strLeft = ""; string strRight = ""; StringUtil.ParseTwoPart(strStart, ",", out strLeft, out strRight); if (string.IsNullOrEmpty(strLeft) == true) format.StartX = double.NaN; else format.StartX = double.Parse(strLeft); if (string.IsNullOrEmpty(strRight) == true) format.StartY = double.NaN; else format.StartY = double.Parse(strRight); }
// 因为数量单位变化,刷新 lineFormat list 的显示 void RefreshListLineFormatsUnits() { foreach (ListViewItem item in this.listView_lineFormats.Items) { LineStore store = item.Tag as LineStore; Debug.Assert(store != null, ""); if (this._currentUnit == GraphicsUnit.Display) { ListViewUtil.ChangeItemText(item, COLUMN_START, store.UniversalStart); ListViewUtil.ChangeItemText(item, COLUMN_OFFSET, store.UniversalOffset); } else { LineFormat format = new LineFormat(); SetFormatStart(format, store.UniversalStart); SetFormatOffset(format, store.UniversalOffset); string strStart = GetStartString(this._currentUnit, format.StartX, format.StartY); ListViewUtil.ChangeItemText(item, COLUMN_START, strStart); string strOffset = GetOffsetString(this._currentUnit, format.OffsetX, format.OffsetY); ListViewUtil.ChangeItemText(item, COLUMN_OFFSET, strOffset); } } }
// 可能会抛出异常 static void SetFormatOffset(LineFormat format, string strOffset) { string strLeft = ""; string strRight = ""; StringUtil.ParseTwoPart(strOffset, ",", out strLeft, out strRight); if (string.IsNullOrEmpty(strLeft) == true) format.OffsetX = 0; else format.OffsetX = double.Parse(strLeft); if (string.IsNullOrEmpty(strRight) == true) format.OffsetY = 0; else format.OffsetY = double.Parse(strRight); }
LabelParam GetLabelParam() { LabelParam param = new LabelParam(); param.PageWidth = (double)this.numericUpDown_pageWidth.UniverseValue; param.PageHeight = (double)this.numericUpDown_pageHeight.UniverseValue; // param.Landscape = this.checkBox_landscape.Checked; param.RotateDegree = this.rotateControl1.RotateDegree; try { // 可能会抛出 ArgumentException 异常 DecimalPadding padding = PaddingDialog.ParsePaddingString(this.textBox_pagePadding.UniverseText); #if NO param.PageMargins = new System.Drawing.Printing.Margins(padding.Left, padding.Right, padding.Top, padding.Bottom); #endif param.PageMargins = padding; } catch { } param.LabelWidth = (double)this.numericUpDown_labelWidth.UniverseValue; param.LabelHeight = (double)this.numericUpDown_labelHeight.UniverseValue; try { // 可能会抛出 ArgumentException 异常 DecimalPadding padding = PaddingDialog.ParsePaddingString(this.textBox_labelPadding.UniverseText); #if NO param.LabelPaddings = new System.Drawing.Printing.Margins(padding.Left, padding.Right, padding.Top, padding.Bottom); #endif param.LabelPaddings = padding; } catch { } #if NO Font font = null; if (String.IsNullOrEmpty(this.textBox_labelFont.Text) == false) { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); font = (Font)converter.ConvertFromString(this.textBox_labelFont.Text); } else { font = Control.DefaultFont; } #endif string strFontString = this.textBox_labelFont.Text; if (Global.IsVirtualBarcodeFont(ref strFontString) == true) param.IsBarcodeFont = true; else param.IsBarcodeFont = false; param.Font = Global.BuildFont(strFontString); param.LineFormats.Clear(); foreach (ListViewItem item in this.listView_lineFormats.Items) { LineStore store = item.Tag as LineStore; Debug.Assert(store != null, ""); LineFormat format = new LineFormat(); strFontString = ListViewUtil.GetItemText(item, COLUMN_FONT); if (string.IsNullOrEmpty(strFontString) == false) { if (Global.IsVirtualBarcodeFont(ref strFontString) == true) format.IsBarcodeFont = true; else format.IsBarcodeFont = false; format.Font = Global.BuildFont(strFontString); } else format.Font = null; format.Align = ListViewUtil.GetItemText(item, COLUMN_ALIGN); SetFormatStart(format, store.UniversalStart); SetFormatOffset(format, store.UniversalOffset); format.ForeColor = ListViewUtil.GetItemText(item, COLUMN_FORECOLOR); format.BackColor = ListViewUtil.GetItemText(item, COLUMN_BACKCOLOR); param.LineFormats.Add(format); } param.LineSep = (double)this.numericUpDown_lineSep.UniverseValue; param.DefaultPrinter = this.textBox_printerInfo.Text; return param; }
public static int Build(XmlDocument dom, out LabelParam label_param, out string strError) { strError = ""; int nRet = 0; label_param = new LabelParam(); XmlNode label = dom.DocumentElement.SelectSingleNode("label"); if (label != null) { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(label, "width", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.LabelWidth = fValue; // height nRet = DomUtil.GetDoubleParam(label, "height", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.LabelHeight = fValue; // lineSep nRet = DomUtil.GetDoubleParam(label, "lineSep", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.LineSep = fValue; { DecimalPadding margins; string strPaddings = DomUtil.GetAttr(label, "paddings"); nRet = GetMarginValue( strPaddings, out margins, out strError); if (nRet == -1) { strError = "<label>元素paddings属性值格式错误: " + strError; return -1; } label_param.LabelPaddings = margins; } #if NO try { string strPaddings = DomUtil.GetAttr(label, "paddings"); string[] parts = strPaddings.Split(new char[] { ',' }); if (parts.Length > 0) label_param.LabelPaddings.Left = Convert.ToInt32(parts[0]); if (parts.Length > 1) label_param.LabelPaddings.Top = Convert.ToInt32(parts[1]); if (parts.Length > 2) label_param.LabelPaddings.Right = Convert.ToInt32(parts[2]); if (parts.Length > 3) label_param.LabelPaddings.Bottom = Convert.ToInt32(parts[3]); } catch (Exception ex) { strError = "<label>元素paddings属性值格式错误: " + ex.Message; return -1; } #endif string strFont = DomUtil.GetAttr(label, "font"); if (String.IsNullOrEmpty(strFont) == false) { if (Global.IsVirtualBarcodeFont(ref strFont) == true) label_param.IsBarcodeFont = true; try { label_param.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<label>元素 font 属性值格式错误: " + ex.Message; return -1; } } } XmlNode page = dom.DocumentElement.SelectSingleNode("page"); if (page != null) { { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(page, "width", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.PageWidth = fValue; // height nRet = DomUtil.GetDoubleParam(page, "height", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.PageHeight = fValue; } { DecimalPadding margins; string strMargins = DomUtil.GetAttr(page, "margins"); nRet = GetMarginValue( strMargins, out margins, out strError); if (nRet == -1) { strError = "<page>元素margins属性值格式错误: " + strError; return -1; } label_param.PageMargins = margins; } #if NO try { string strMargins = DomUtil.GetAttr(page, "margins"); string[] parts = strMargins.Split(new char[] { ',' }); if (parts.Length > 0) label_param.PageMargins.Left = Convert.ToInt32(parts[0]); if (parts.Length > 1) label_param.PageMargins.Top = Convert.ToInt32(parts[1]); if (parts.Length > 2) label_param.PageMargins.Right = Convert.ToInt32(parts[2]); if (parts.Length > 3) label_param.PageMargins.Bottom = Convert.ToInt32(parts[3]); } catch (Exception ex) { strError = "<page>元素margins属性值格式错误: " + ex.Message; return -1; } #endif label_param.DefaultPrinter = DomUtil.GetAttr(page, "defaultPrinter"); #if NO bool bValue = false; nRet = DomUtil.GetBooleanParam(page, "landscape", false, out bValue, out strError); if (nRet == -1) { strError = "<page> 元素的 landscape 属性错误: " + strError; return -1; } label_param.Landscape = bValue; #endif int nValue = 0; DomUtil.GetIntegerParam(page, "rotate", 0, out nValue, out strError); if (nRet == -1) { strError = "<page> 元素的 rotate 属性错误: " + strError; return -1; } label_param.RotateDegree = nValue; } XmlNodeList nodes = dom.DocumentElement.SelectNodes("lineFormats/line"); label_param.LineFormats = new List<LineFormat>(); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; string strFont = DomUtil.GetAttr(node, "font"); LineFormat format = new LineFormat(); if (Global.IsVirtualBarcodeFont(ref strFont) == true) format.IsBarcodeFont = true; if (string.IsNullOrEmpty(strFont) == false) { try { format.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<line>元素font属性值格式错误: " + ex.Message; return -1; } } else format.Font = null; // 继承页面的字体 format.Align = DomUtil.GetAttr(node, "align"); string strOffset = DomUtil.GetAttr(node, "offset"); if (string.IsNullOrEmpty(strOffset) == false) { try { double left = 0; double right = 0; ParsetTwoDouble(strOffset, false, out left, out right); format.OffsetX = left; format.OffsetY = right; } catch (Exception ex) { strError = "<line>元素offset属性值格式错误: " + ex.Message; return -1; } } string strStart = DomUtil.GetAttr(node, "start"); if (string.IsNullOrEmpty(strStart) == false) { try { double left = double.NaN; double right = double.NaN; ParsetTwoDouble(strStart, true, out left, out right); format.StartX = left; format.StartY = right; } catch (Exception ex) { strError = "<line>元素start属性值格式错误: " + ex.Message; return -1; } } format.ForeColor = DomUtil.GetAttr(node, "foreColor"); format.BackColor = DomUtil.GetAttr(node, "backColor"); label_param.LineFormats.Add(format); } return 0; }
// 绘制一个标签的全部文字 // parameters: // rectContent 整个文字区域的矩形 void PaintLabelContent(Graphics g, RectangleF rectContent, List <string> lines, LabelParam label_param, float x, float y, float nXDelta, float nYDelta, bool bTestingGrid) { using (Region old_clip = g.Clip) { g.IntersectClip(rectContent); float y0 = 0; for (int k = 0; k < lines.Count; k++) { string strText = lines[k]; LineFormat format = null; if (k < label_param.LineFormats.Count) { format = label_param.LineFormats[k]; } Font this_font = null; bool bIsBarcodeFont = false; if (format != null && format.Font != null) { this_font = format.Font; bIsBarcodeFont = format.IsBarcodeFont; } else { this_font = label_param.Font; bIsBarcodeFont = label_param.IsBarcodeFont; } //if (bIsBarcodeFont == true && string.IsNullOrEmpty(strText) == false) // strText = "*" + strText + "*"; Font save_font = this_font; if (this.DesignMode) { this_font = ReCreateFont(this_font, g); } try { float nLineHeight = this_font.GetHeight(g); RectangleF rect = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top + y0 - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, nLineHeight); bool bAbsLocation = false; // 行格式的 start 和 offset if (format != null) { if (double.IsNaN(format.StartX) == false) { rect.X = (float)format.StartX; } if (double.IsNaN(format.StartY) == false) { rect.Y = (float)format.StartY; bAbsLocation = true; // Y 绝对定位后,行高度不参与累计 } rect.Offset((float)format.OffsetX, (float)format.OffsetY); y0 += (float)format.OffsetY; // Y 偏移后,累计值也跟着调整 if (double.IsNaN(format.Width) == false) { rect.Width = (float)format.Width; } if (double.IsNaN(format.Height) == false) { rect.Height = (float)format.Height; } } StringFormat s_format = new StringFormat(); if (format != null) { if (format.Align == "right") { s_format.Alignment = StringAlignment.Far; } else if (format.Align == "center") { s_format.Alignment = StringAlignment.Center; } else { s_format.Alignment = StringAlignment.Near; } s_format.Trimming = StringTrimming.EllipsisCharacter; // s_format.LineAlignment = StringAlignment.Center; } if (format != null && string.IsNullOrEmpty(format.BackColor) == false) { using (Brush brush = new SolidBrush(GetColor(format.BackColor))) { g.FillRectangle(brush, rect); } } if (bIsBarcodeFont) { // strText = strText.Trim(new char[] { '*' }); // 应该是 1/100 inch 单位 float textHeight = MeasureOcrTextHeight( g, rect, strText); RectangleF target = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height - textHeight); GraphicsUnit u = g.PageUnit; Hashtable param = new Hashtable(); param["type"] = "39"; param["code"] = strText; param["width"] = ((int)(target.Width * 2)).ToString(); param["height"] = ((int)(target.Height * 2)).ToString(); param["margin"] = "0"; using (Image image = BuildQrCodeImage(param)) { RectangleF source = new RectangleF(0, 0, image.Width, image.Height); g.DrawImage(image, target, source, GraphicsUnit.Pixel); } RectangleF rectText = new RectangleF(rect.X, rect.Y + target.Height, rect.Width, textHeight); PaintOcrFont( g, rectText, strText); } else { Brush brushText = null; try { if (format != null && string.IsNullOrEmpty(format.ForeColor) == false) { brushText = new SolidBrush(GetColor(format.ForeColor)); } else { brushText = System.Drawing.Brushes.Black; } g.DrawString(strText, this_font, brushText, rect, s_format); } finally { if (brushText != System.Drawing.Brushes.Black) { brushText.Dispose(); } } } // 文字行区域边界 // 黑色点 if (bTestingGrid == true && label_param.LineSep > 0) { using (Pen pen = new Pen(Color.Black, (float)1)) { pen.DashPattern = new float[] { 1F, 3F, 1F, 3F }; g.DrawLine(pen, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Top) ); g.DrawLine(pen, new PointF(rect.Left + 2, rect.Bottom), new PointF(rect.Right, rect.Bottom) ); } } if (bAbsLocation == false) { y0 += // nLineHeight rect.Height + (float)label_param.LineSep; } } finally { if (this_font != save_font) { this_font.Dispose(); } } } g.Clip = old_clip; } // end of using clip }
internal void DoPrintPage( IWin32Window owner, LabelParam label_param, string strStyle, PrintPageEventArgs e) { string strError = ""; int nRet = 0; if (e.Cancel == true) { return; } bool bTestingGrid = false; if (StringUtil.IsInList("TestingGrid", strStyle) == true) { bTestingGrid = true; } int nYCount = 0; int nXCount = 0; double PageWidth = label_param.PageWidth; double PageHeight = label_param.PageHeight; #if NO if (label_param.Landscape == true) { double nTemp = PageHeight; PageHeight = PageWidth; PageWidth = nTemp; } #endif #if NO if (e.PageSettings.Landscape == true) { double nTemp = PageHeight; PageHeight = PageWidth; PageWidth = nTemp; } #endif int nPageWidth = e.PageBounds.Width; // PageBounds 中已经是按照 Landscape 处理过的方向了 if (PageWidth != 0) { nPageWidth = (int)PageWidth; } int nPageHeight = e.PageBounds.Height; if (PageHeight != 0) { nPageHeight = (int)PageHeight; } DecimalPadding PageMargins = RotatePadding(label_param.PageMargins, e.PageSettings.Landscape); // label_param.Landscape #if NO // 垂直方向的个数 nYCount = (e.PageBounds.Height - label_param.PageMargins.Top - label_param.PageMargins.Bottom) / label_param.Height; // 水平方向的个数 nXCount = (e.PageBounds.Width - label_param.PageMargins.Left - label_param.PageMargins.Right) / label_param.Width; #endif // 垂直方向的个数 nYCount = (int) ( (double)(nPageHeight - PageMargins.Top - PageMargins.Bottom) / (double)label_param.LabelHeight ); // 水平方向的个数 nXCount = (int) ( (double)(nPageWidth - PageMargins.Left - PageMargins.Right) / (double)label_param.LabelWidth ); int from = 0; int to = 0; bool bOutput = true; // 如果 e.PageSettings.PrinterSettings.FromPage == 0,会被当作打印第一页 if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages && e.PageSettings.PrinterSettings.FromPage >= 1) { from = e.PageSettings.PrinterSettings.FromPage; to = e.PageSettings.PrinterSettings.ToPage; // 交换,保证from为小 if (from > to) { int temp = to; to = from; from = temp; } if (this.m_nPageNo == 0) { this.m_nPageNo = from; Debug.Assert(this.m_nPageNo >= 1, ""); long nLabelCount = (nXCount * nYCount) * (this.m_nPageNo - 1); // 从文件中跳过这么多label的内容行 for (int i = 0; i < nLabelCount; i++) { List <string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) { goto ERROR1; } if (nRet == 1) { e.Cancel = true; return; } } } /* * if (this.m_nPageNo >= from * && this.m_nPageNo <= to) * { * bOutput = true; * } * else * { * bOutput = false; * } * */ } else { if (this.m_nPageNo == 0) { this.m_nPageNo = 1; // 一般性的初始化 } } // 加快运行速度 float nXDelta = e.PageSettings.PrintableArea.Left; float nYDelta = e.PageSettings.PrintableArea.Top; /* * if (this.PrintController.IsPreview == true) * { * nXDelta = 0; * nYDelta = 0; * } * */ if (this.OriginAtMargins == true || this.PreviewMode == true) // false { // true 如果图形起始于页面边距。否则起始于可打印区域 nXDelta = 0; nYDelta = 0; } if (this.OriginPoint != null) { nXDelta -= this.OriginPoint.X; nYDelta -= this.OriginPoint.Y; } #if NO float nPrintableWidth = e.PageSettings.PrintableArea.Width; float nPrintableHeight = e.PageSettings.PrintableArea.Height; #endif if (this.IsDesignMode) { // 绘制整个纸张背景 白色 using (Brush brushBack = new SolidBrush(Color.White)) { RectangleF rectPaper = new RectangleF(0 + 1 - nXDelta, 0 + 1 - nYDelta, e.PageBounds.Width - 2, e.PageBounds.Height - 2); e.Graphics.FillRectangle(brushBack, rectPaper); } } // 绘制可打印区域 // 鲜红色 if (bTestingGrid == true && bOutput == true) { float nXOffs = 0; float nYOffs = 0; // 如果为正式打印,左上角(0,0)已经就是可以打印区域的左上角 // 如果为preview模式,则左上角要向右向下移动,才能模拟出显示效果 #if NO if (this.OriginAtMargins == true || this.PreviewMode == true) { nXOffs = e.PageSettings.PrintableArea.Left; nYOffs = e.PageSettings.PrintableArea.Top; } #endif if (this.OriginPoint != null) { nXOffs += this.OriginPoint.X; nYOffs += this.OriginPoint.Y; } RectangleF rect = RotateRectangle(e.PageSettings.PrintableArea, e.PageSettings.Landscape); // label_param.Landscape if (this.OriginAtMargins == true || this.PreviewMode == true) { } else { rect.X = 0; rect.Y = 0; } rect.Offset(nXOffs, nYOffs); using (Pen pen = new Pen(Color.Red, (float)1)) { DrawFourAngel( e.Graphics, pen, rect, 50); // 半英寸 } } // 加入变换 e.Graphics.TranslateTransform(-nXDelta, -nYDelta); nXDelta = 0; nYDelta = 0; if (label_param.RotateDegree != 0) { float x_offs, y_offs; CenterMove(label_param.RotateDegree, (float)label_param.PageWidth, // e.PageBounds.Width, (float)label_param.PageHeight, // e.PageBounds.Height, out x_offs, out y_offs); e.Graphics.TranslateTransform(x_offs, y_offs); e.Graphics.RotateTransform((float)label_param.RotateDegree); } if (this.IsDesignMode) { // 绘制配置文件的页面区域 if (PageHeight > 0 && PageWidth > 0) { using (Brush brushBack = new SolidBrush(Color.FromArgb(128, Color.LightYellow))) { RectangleF rectPaper = new RectangleF(0 - nXDelta, 0 - nYDelta, (float)PageWidth, (float)PageHeight); e.Graphics.FillRectangle(brushBack, rectPaper); } } } // 绘制内容区域边界(也就是排除了页面边空的中间部分) // 淡绿色 if (bTestingGrid == true && bOutput == true) { using (Pen pen = new Pen(Color.FromArgb(0, 100, 0), (float)2)) // 3 { #if NO e.Graphics.DrawRectangle(pen, PageMargins.Left - nXDelta, PageMargins.Top - nYDelta, e.PageBounds.Width - PageMargins.Left - PageMargins.Right, e.PageBounds.Height - PageMargins.Top - PageMargins.Bottom); #endif e.Graphics.DrawRectangle(pen, (float)PageMargins.Left - nXDelta, (float)PageMargins.Top - nYDelta, (float)nPageWidth - (float)PageMargins.Left - (float)PageMargins.Right, (float)nPageHeight - (float)PageMargins.Top - (float)PageMargins.Bottom); } } bool bEOF = false; float y = (float)PageMargins.Top; // 每一行的循环 for (int i = 0; i < nYCount; i++) { bool bDisplay = true; if (this.IsDesignMode == true) { RectangleF rectLine = new RectangleF( (float)0 - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth * nXCount, (float)label_param.LabelHeight); if (rectLine.Top > e.Graphics.ClipBounds.Bottom) { // Debug.WriteLine("break line loop at " + i.ToString()); break; // 当前行在剪裁区域的下方,可以中断循环了 } if (rectLine.IntersectsWith(e.Graphics.ClipBounds) == false) { // Debug.WriteLine("skip line " + i.ToString()); bDisplay = false; } } float x = (float)PageMargins.Left; // 每一列的循环 for (int j = 0; j < nXCount; j++) { List <string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) { goto ERROR1; } if (nRet == 1) { bEOF = true; } if (bOutput == true && bDisplay == true) { // 标签 RectangleF rectLabel = new RectangleF( (float)x - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); if (rectLabel.Left > e.Graphics.ClipBounds.Right) { // Debug.WriteLine("break label loop at i=" + i.ToString() + " j=" + j.ToString()); // 当前标签在剪裁区域的右方,可以不要显示后面的标签了 bDisplay = false; } if (this.IsDesignMode == false || rectLabel.IntersectsWith(e.Graphics.ClipBounds) == true) { // Debug.WriteLine("i="+i.ToString()+" j="+j.ToString()+" rectLabel = "+rectLabel.ToString()+", clipbounds " + e.Graphics.ClipBounds.ToString()); // 标签内容区域 RectangleF rectContent = new RectangleF( (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); // 绘制标签边界 // 灰色 if (bTestingGrid == true) { // 标签白色背景 if (this.IsDesignMode == true) { using (Brush brushBack = new SolidBrush(Color.FromArgb(200, Color.White))) { e.Graphics.FillRectangle(brushBack, rectLabel); } } // 标签边界 using (Pen pen = new Pen(Color.FromArgb(200, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectLabel.X, rectLabel.Y, rectLabel.Width, rectLabel.Height); #if NO e.Graphics.DrawRectangle(pen, x - nXDelta, y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); #endif } // 绘制标签内部文字区域边界 // 淡红色 using (Pen pen = new Pen(Color.FromArgb(255, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectContent.X, rectContent.Y, rectContent.Width, rectContent.Height); #if NO e.Graphics.DrawRectangle(pen, (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); #endif } } #if NO RectangleF clip = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom); #endif using (Region old_clip = e.Graphics.Clip) { e.Graphics.IntersectClip(rectContent); float y0 = 0; for (int k = 0; k < lines.Count; k++) { string strText = lines[k]; LineFormat format = null; if (k < label_param.LineFormats.Count) { format = label_param.LineFormats[k]; } Font this_font = null; bool bIsBarcodeFont = false; if (format != null && format.Font != null) { this_font = format.Font; bIsBarcodeFont = format.IsBarcodeFont; } else { this_font = label_param.Font; bIsBarcodeFont = label_param.IsBarcodeFont; } if (bIsBarcodeFont == true && string.IsNullOrEmpty(strText) == false) { strText = "*" + strText + "*"; } float nLineHeight = this_font.GetHeight(e.Graphics); RectangleF rect = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top + y0 - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, nLineHeight); bool bAbsLocation = false; // 行格式的 start 和 offset if (format != null) { if (double.IsNaN(format.StartX) == false) { rect.X = (float)format.StartX; } if (double.IsNaN(format.StartY) == false) { rect.Y = (float)format.StartY; bAbsLocation = true; // Y 绝对定位后,行高度不参与累计 } rect.Offset((float)format.OffsetX, (float)format.OffsetY); y0 += (float)format.OffsetY; // Y 偏移后,累计值也跟着调整 } StringFormat s_format = new StringFormat(); if (format != null) { if (format.Align == "right") { s_format.Alignment = StringAlignment.Far; } else if (format.Align == "center") { s_format.Alignment = StringAlignment.Center; } else { s_format.Alignment = StringAlignment.Near; } s_format.Trimming = StringTrimming.EllipsisCharacter; // s_format.LineAlignment = StringAlignment.Center; } if (format != null && string.IsNullOrEmpty(format.BackColor) == false) { using (Brush brush = new SolidBrush(GetColor(format.BackColor))) { e.Graphics.FillRectangle(brush, rect); } } { Brush brushText = null; try { if (format != null && string.IsNullOrEmpty(format.ForeColor) == false) { brushText = new SolidBrush(GetColor(format.ForeColor)); } else { brushText = System.Drawing.Brushes.Black; } e.Graphics.DrawString(strText, this_font, brushText, rect, s_format); } finally { if (brushText != System.Drawing.Brushes.Black) { brushText.Dispose(); } } } // 文字行区域边界 // 黑色点 if (bTestingGrid == true && label_param.LineSep > 0) { using (Pen pen = new Pen(Color.Black, (float)1)) { // pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; #if NO e.Graphics.DrawRectangle(pen, rect.Left, rect.Top, rect.Width, rect.Height); #endif pen.DashPattern = new float[] { 1F, 3F, 1F, 3F }; e.Graphics.DrawLine(pen, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Top) ); e.Graphics.DrawLine(pen, new PointF(rect.Left + 2, rect.Bottom), new PointF(rect.Right, rect.Bottom) ); } } if (bAbsLocation == false) { y0 += nLineHeight + (float)label_param.LineSep; } } e.Graphics.Clip = old_clip; } // end of using clip } // end if IntersectsWith } // end if bOutput == true x += (float)label_param.LabelWidth; } //CONTINUE_LINE: y += (float)label_param.LabelHeight; } // If more lines exist, print another page. if (bEOF == false) { if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (this.m_nPageNo >= to) { e.HasMorePages = false; return; } } } else { e.HasMorePages = false; return; } this.m_nPageNo++; e.HasMorePages = true; return; ERROR1: MessageBox.Show(owner, strError); }