private void btnInsertImg_Click(object sender, EventArgs e) { if (!PagerSetting.IsPagerLayerCreated) { MessageBox.Show("请先设置纸张尺寸!"); return; } ImageLayer imglayer = new ImageLayer(openFileDialog.FileName, int.Parse(tbImageWidth.Text), int.Parse(tbImageHeight.Text)); panel1.Controls.Add(imglayer); imglayer.BringToFront();//一定要加这句将层放到最上面,不然默认是放在最下面的 imglayer.Left = m_startX; imglayer.Top = m_startY; }
/// <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; } } }