コード例 #1
0
        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                #region 参数校验

                if (this.cmbPrinter.SelectedItem == null)
                {
                    XMessageBox.Warning("请选择打印机");
                    return;
                }
                if (this.cmbPaper.SelectedItem == null)
                {
                    XMessageBox.Warning("请选择打印纸张");
                    return;
                }

                if (!double.TryParse(this.tbPaperWidth.Text, out double nw) || !double.TryParse(this.tbPaperHeight.Text, out double nh) || nw <= 0 || nh <= 0)
                {
                    XMessageBox.Warning("请输入正确的纸张高度和宽度,宽度和高度必须大于零");
                    return;
                }

                if (!double.TryParse(this.tbMarginBottom.Text, out double mb) || !double.TryParse(this.tbMarginRight.Text, out double mr) || !double.TryParse(this.tbMarginLeft.Text, out double ml) || !double.TryParse(this.tbMarginTop.Text, out double mt) || mt < 0 || mb < 0 || ml < 0 || mr < 0)
                {
                    XMessageBox.Warning("请输入正确的页边距,上下左右边距必须大于等于零");
                    return;
                }

                if (nh - mt - mb <= 0 || Math.Abs(nh - mt - mb) / (double)nh < 0.2)
                {
                    XMessageBox.Warning("上下边距太大,可打印区域太小,请重新设置上下边距");
                    return;
                }

                if (nw - mr - ml <= 0 || Math.Abs(nw - mr - ml) / (double)nw < 0.2)
                {
                    XMessageBox.Warning("左右边距太大,可打印区域太小,请重新设置左右边距");
                    return;
                }

                #endregion

                PrinterSettings ps = cmbPrinter.SelectedItem as PrinterSettings;
                var             pz = cmbPaper.SelectedItem as PaperSize;
                ps.DefaultPageSettings.PaperSize = pz;
                if (pz.PaperName.Equals("自定义纸张"))
                {
                    pz.Width  = (int)Config.Mm2Inch(Convert.ToDouble(tbPaperWidth.Text));
                    pz.Height = (int)Config.Mm2Inch(Convert.ToDouble(tbPaperHeight.Text));
                }
                ps.DefaultPageSettings.Margins = new Margins
                {
                    Top    = (int)Config.Mm2Inch(Convert.ToDouble(tbMarginTop.Text)),
                    Bottom = (int)Config.Mm2Inch(Convert.ToDouble(tbMarginBottom.Text)),
                    Left   = (int)Config.Mm2Inch(Convert.ToDouble(tbMarginLeft.Text)),
                    Right  = (int)Config.Mm2Inch(Convert.ToDouble(tbMarginRight.Text))
                };
                ps.DefaultPageSettings.Landscape = this.rbLanscape.Checked;

                RawPrintSetting rps = new RawPrintSetting();
                rps.LandScape   = this.rbLanscape.Checked;
                rps.PageMargins = ps.DefaultPageSettings.Margins;
                rps.PaperName   = ps.DefaultPageSettings.PaperSize.PaperName;
                rps.PaperSize   = pz;
                rps.PrinterName = ps.PrinterName;



                XMessageBox.Info(Config.SaveSetting(rps));
            }
            catch (Exception exp)
            {
                XMessageBox.Error(string.Format("保存打印设置失败:{0}", exp.Message));
            }
        }