private void KopSoftPrint_Load(object sender, EventArgs e) { string sDefault = printDocument.PrinterSettings.PrinterName; //设置默认打印机名 foreach (string sPrint in PrinterSettings.InstalledPrinters) //获取所有打印机名称 { cbPrint.Items.Add(sPrint); if (sPrint == sDefault) { cbPrint.SelectedIndex = cbPrint.Items.IndexOf(sPrint); } } try { PageMargins = int.Parse(tbPageMargins.Text);//页面边距 PagerSetting.PagePadding = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageMargins, 1); PageWidth = int.Parse(tbPageWidth.Text); PageHeight = int.Parse(tbPageHeight.Text); if (PageWidth > 0 && PageHeight > 0)//进行尺码单位毫米(mm)转换成像素(Px) { PagerSetting.PageHeight = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageHeight, 2); PagerSetting.PageWidth = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageWidth, 1); } //重绘pager纸张 PageLayer pl = new PageLayer(PagerSetting.PageWidth, PagerSetting.PageHeight, PagerSetting.PagePadding); //先把旧纸张去掉 if (panel1.Controls.Count > 0) { for (int i = 0; i < panel1.Controls.Count; i++) { if (((ImageLayerBase)panel1.Controls[i]).layerType == LayerType.Paper) { panel1.Controls.RemoveAt(i); break; } } } panel1.Controls.Add(pl); PagerSetting.IsPagerLayerCreated = true; //计算控件起始坐标 int startX = (int)((panel1.Width - PagerSetting.PageWidth) / 2); int startY = (int)((panel1.Height - PagerSetting.PageHeight) / 2); pl.Left = startX; pl.Top = startY; m_startX = startX + PagerSetting.PagePadding; m_startY = startY + PagerSetting.PagePadding; lbMessage.Text = "设置成功,纸张尺寸" + PagerSetting.PageWidth + "*" + PagerSetting.PageHeight + "px 页面边距" + PagerSetting.PagePadding + "px"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void tbPageHeight_TextChanged(object sender, EventArgs e) { try { PageMargins = int.Parse(tbPageMargins.Text);//页面边距 PagerSetting.PagePadding = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageMargins, 1); PageWidth = int.Parse(tbPageWidth.Text); PageHeight = int.Parse(tbPageHeight.Text); if (PageWidth > 0 && PageHeight > 0)//进行尺码单位毫米(mm)转换成像素(Px) { PagerSetting.PageHeight = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageHeight, 2); PagerSetting.PageWidth = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageWidth, 1); } //重绘pager纸张 PageLayer pl = new PageLayer(PagerSetting.PageWidth, PagerSetting.PageHeight, PagerSetting.PagePadding); //先把旧纸张去掉 if (panel1.Controls.Count > 0) { for (int i = 0; i < panel1.Controls.Count; i++) { if (((ImageLayerBase)panel1.Controls[i]).layerType == LayerType.Paper) { panel1.Controls.RemoveAt(i); break; } } } panel1.Controls.Add(pl); PagerSetting.IsPagerLayerCreated = true; //计算控件起始坐标 int startX = (int)((panel1.Width - PagerSetting.PageWidth) / 2); int startY = (int)((panel1.Height - PagerSetting.PageHeight) / 2); pl.Left = startX; pl.Top = startY; m_startX = startX + PagerSetting.PagePadding; m_startY = startY + PagerSetting.PagePadding; lbMessage.Text = "设置成功,纸张尺寸" + PagerSetting.PageWidth + "*" + PagerSetting.PageHeight + "px 页面边距" + PagerSetting.PagePadding + "px"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 从配置还原到控件 /// </summary> /// <param name="cfg"></param> /// <param name="parentCtl"></param> public static void ReturnContrl(PrintConfig cfg, Control parentCtl) { if (cfg == null || cfg.Layers == null || cfg.Layers.Count == 0) { return; } parentCtl.Controls.Clear(); PagerSetting.PageHeight = cfg.PageHeight; PagerSetting.PageWidth = cfg.PageWidth; PagerSetting.PagePadding = cfg.PagePadding; //根据配置还原图 PageLayer pl = new PageLayer(PagerSetting.PageWidth, PagerSetting.PageHeight, PagerSetting.PagePadding); parentCtl.Controls.Add(pl); //计算控件起始坐标 int startX = (int)((parentCtl.Width - PagerSetting.PageWidth) / 2); int startY = (int)((parentCtl.Height - PagerSetting.PageHeight) / 2); pl.Left = startX; pl.Top = startY; for (int i = cfg.Layers.Count - 1; i >= 0; i--) { string layertype = cfg.Layers[i].GetType().ToString(); switch (layertype.Substring(layertype.LastIndexOf(".") + 1)) { case "BarCodeLayerConfig": BarCodeLayerConfig bc = (BarCodeLayerConfig)cfg.Layers[i]; BarCodeLayer barlayer = new BarCodeLayer(bc.Content, bc.Width, bc.Height, bc.CodeType, bc.IsShowText); parentCtl.Controls.Add(barlayer); barlayer.BringToFront(); //一定要加这句将层放到最上面 不然默认是放在最下面的 barlayer.Left = bc.X + startX; barlayer.Top = bc.Y + startY; break; case "ImageLayerConfig": ImageLayerConfig imglc = (ImageLayerConfig)cfg.Layers[i]; ImageLayer imglayer = new ImageLayer(imglc.ImageFilePath, imglc.Width, imglc.Height); parentCtl.Controls.Add(imglayer); imglayer.BringToFront(); //一定要加这句将层放到最上面 不然默认是放在最下面的 imglayer.Left = imglc.X + startX; imglayer.Top = imglc.Y + startY; break; case "TextLayerConfig": TextLayerConfig tlc = (TextLayerConfig)cfg.Layers[i]; TextLayer tl = new TextLayer(tlc.Content, true, tlc.ContentFont, tlc.ContentColor); parentCtl.Controls.Add(tl); tl.BringToFront(); //一定要加这句将层放到最上面 不然默认是放在最下面的 tl.Left = tlc.X + startX; tl.Top = tlc.Y + startY; break; case "LineLayerConfig": LineLayerConfig lc = (LineLayerConfig)cfg.Layers[i]; LineLayer ll = new LineLayer(lc.lineWidth, lc.lineLength, lc.lineDirect); parentCtl.Controls.Add(ll); ll.BringToFront(); //一定要加这句将层放到最上面 不然默认是放在最下面的 ll.Left = lc.X + startX; ll.Top = lc.Y + startY; break; default: break; } } }