Пример #1
0
        /// <summary>
        /// 加载用户打印设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void PrintSetting_Load(object sender, EventArgs e)
        {
            try
            {
                var fileName = Config.FilePath;

                if (File.Exists(fileName))
                {
                    RawPrintSetting rps = Config.GetRawPrintSettings();
                    if (rps != null)
                    {
                        //打印机
                        foreach (PrinterSettings ps in this.cmbPrinter.Items)
                        {
                            if (ps.PrinterName.Equals(rps.PrinterName, StringComparison.OrdinalIgnoreCase))
                            {
                                ps.DefaultPageSettings.Landscape = rps.LandScape;
                                ps.DefaultPageSettings.Margins   = rps.PageMargins;
                                this.cmbPrinter.SelectedItem     = ps;
                                PrinterSelectChange(this.cmbPrinter, new EventArgs());
                                break;
                            }
                        }
                        //纸张
                        foreach (PaperSize ps in this.cmbPaper.Items)
                        {
                            if (ps.PaperName.Equals(rps.PaperName, StringComparison.OrdinalIgnoreCase))
                            {
                                if (ps.PaperName.Equals("自定义纸张"))
                                {
                                    ps.Height = rps.PaperSize.Height;
                                    ps.Width  = rps.PaperSize.Width;
                                }
                                this.cmbPaper.SelectedItem = ps;
                                PaperSelectChange(this.cmbPaper, new EventArgs());
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                XMessageBox.Warning(string.Format("加载打印设置出错,您需要重新设置并保存"));
            }
        }
Пример #2
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));
            }
        }