示例#1
0
        int PageWidth = 0, PageHeight = 0, PageMargins = 0; //毫米为单位

        private void btnInsertBarCode_Click(object sender, EventArgs e)
        {
            if (!PagerSetting.IsPagerLayerCreated)
            {
                MessageBox.Show("请先设置纸张尺寸!");
                return;
            }
            if (string.IsNullOrEmpty(tbBarCodeContent.Text))
            {
                MessageBox.Show("内容不能为空!");
                return;
            }
            if (rbText.Checked)
            {
                TextLayer tl;
                if (hasSetFont)
                {
                    tl = new TextLayer(tbBarCodeContent.Text, true, fontDialog.Font, fontDialog.Color);
                }
                else
                {
                    tl = new TextLayer(tbBarCodeContent.Text);
                }
                panel1.Controls.Add(tl);
                tl.BringToFront();//一定要加这句将层放到最上面,不然默认是放在最下面的
                tl.Left = m_startX;
                tl.Top  = m_startY;
            }
            else
            {
                int type = 1;
                if (rbBarCode.Checked)
                {
                    type = 2;
                }
                BarCodeLayer barlayer = new BarCodeLayer(tbBarCodeContent.Text, int.Parse(tbBarWidth.Text), int.Parse(tbBarHeight.Text), type, cbIsShowText.Checked);
                panel1.Controls.Add(barlayer);
                barlayer.BringToFront();//一定要加这句将层放到最上面,不然默认是放在最下面的
                barlayer.Left = m_startX;
                barlayer.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;
                }
            }
        }