コード例 #1
0
ファイル: LabelDefControl.cs プロジェクト: renyh1013/dp2
        // 选择打印机和纸张
        // parameters:
        //      bForceDialog    是否迫使出现打印机对话框
        // return:
        //      null    选择打印机的对话框被取消
        //      其他  返回 PrintDocument 对象
        PrintDocument SelectPrinterAndPaper(bool bForceDialog)
        {
            string strError = "";
            int nRet = 0;

            PrintDialog printDialog1 = new PrintDialog();

            bool bDisplayPrinterDialog = false;
            PrintDocument document = this.labelDesignControl1.PrintDocument;

            // new System.Drawing.Printing.PrintDocument();

            printDialog1.Document = document;

            PrinterInfo printerInfo = new PrinterInfo("", this.textBox_printerInfo.Text);

            if (printerInfo != null)
            {
                if (string.IsNullOrEmpty(printerInfo.PrinterName) == false
                    && string.IsNullOrEmpty(printerInfo.PaperName) == false)
                {
                    // 按照存储的打印机名选定打印机
                    nRet = LabelPrintForm.SelectPrinterByName(document,
                        printerInfo.PrinterName,
                        out  strError);
                    if (nRet == 1)
                    {
                        MessageBox.Show(this, "打印机 " + printerInfo.PrinterName + " 当前不可用,请重新选定打印机");
                        printerInfo.PrinterName = "";
                        bDisplayPrinterDialog = true;
                    }

                    if (bDisplayPrinterDialog == false
                        && string.IsNullOrEmpty(printerInfo.PaperName) == false)
                    {
                        nRet = LabelPrintForm.SelectPaperByName(document,
                            printerInfo.PaperName,
                            printerInfo.Landscape,
                            true,   // false,
                            out strError);
                        if (nRet == 1)
                        {
                            MessageBox.Show(this, "打印机 " + printerInfo.PrinterName + " 的纸张类型 " + printerInfo.PaperName + " 当前不可用,请重新选定纸张");
                            printerInfo.PaperName = "";
                            bDisplayPrinterDialog = true;
                        }
                    }

                    // 只要有一个打印机事项没有确定,就要出现打印机对话框
                    if (string.IsNullOrEmpty(printerInfo.PrinterName) == true
                        || string.IsNullOrEmpty(printerInfo.PaperName) == true)
                        bDisplayPrinterDialog = true;
                }
            }
            else
            {
                // 没有首选配置的情况下要出现打印对话框
                bDisplayPrinterDialog = true;
            }

#if NO
            // 如果一开始没有打印机信息 textbox 内容,则依 label_param 中的方向
            if (string.IsNullOrEmpty(this.textBox_printerInfo.Text) == true)
                document.DefaultPageSettings.Landscape = this.LabelParam.Landscape;
#endif

            DialogResult result = DialogResult.OK;
            if (bDisplayPrinterDialog == true
                || bForceDialog == true)
            {
                result = printDialog1.ShowDialog();

                if (result == DialogResult.OK)
                {
                    // 记忆打印参数
                    if (printerInfo == null)
                        printerInfo = new PrinterInfo();
                    printerInfo.PrinterName = document.PrinterSettings.PrinterName;
                    printerInfo.PaperName = PrintUtil.GetPaperSizeString(document.DefaultPageSettings.PaperSize);
                    printerInfo.Landscape = document.DefaultPageSettings.Landscape;

                    // 2014/3/27
                    // this.document.DefaultPageSettings = document.PrinterSettings.DefaultPageSettings;
                    nRet = LabelPrintForm.SelectPaperByName(document,
printerInfo.PaperName,
printerInfo.Landscape,
true,
out strError);
                    if (nRet == 1)
                    {
                        SelectPaperDialog paper_dialog = new SelectPaperDialog();
                        MainForm.SetControlFont(paper_dialog, this.Font, false);
                        paper_dialog.Comment = "纸张 " + printerInfo.PaperName + " 不在打印机 " + printerInfo.PrinterName + " 的可用纸张列表中。\r\n请重新选定纸张";
                        paper_dialog.Document = document;
                        paper_dialog.StartPosition = FormStartPosition.CenterParent;
                        paper_dialog.ShowDialog(this);

                        if (paper_dialog.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                            return document;    // null

                    }

                    printerInfo.PaperName = PrintUtil.GetPaperSizeString(document.DefaultPageSettings.PaperSize);
                    printerInfo.Landscape = document.DefaultPageSettings.Landscape;

                    this.textBox_printerInfo.Text = printerInfo.GetText();

                    // document.DefaultPageSettings = document.PrinterSettings.DefaultPageSettings;
                }
                else
                    return null;
            }

            return document;
        }
コード例 #2
0
ファイル: LabelPrintForm.cs プロジェクト: paopaofeng/dp2
        /// <summary>
        /// 打印或打印预览
        /// </summary>
        /// <param name="strLabelDefFileName">标签定义文件</param>
        /// <param name="strLabelFileName">标签内容文件</param>
        /// <param name="bDisplayPrinterDialog">是否显示打印机设置对话框</param>
        /// <param name="bPrintPreview">是否进行打印预览。false 表示进行打印</param>
        /// <param name="strError">返回出错信息</param>
        /// <returns>-1: 出错; 0: 成功</returns>
        public int Print(
            string strLabelDefFileName,
            string strLabelFileName,
            bool bDisplayPrinterDialog,
            bool bPrintPreview,
            out string strError)
        {
            strError = "";
            int nRet = 0;

            this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始执行打印"
                + (bPrintPreview == true ? "预览" : "")
                +"</div>");

            try
            {
                nRet = this.BeginPrint(
                    strLabelFileName,
                    strLabelDefFileName,
                    out strError);
                if (nRet == -1)
                    goto ERROR1;

                this.document.PreviewMode = bPrintPreview;

                this.EnableControls(false);
                Cursor oldCursor = this.Cursor;
                if (bPrintPreview == false)
                    this.Cursor = Cursors.WaitCursor;
                try
                {
                    bool bCustomPaper = false;

                    if (bPrintPreview == false)
                    {
                        // Allow the user to choose the page range he or she would
                        // like to print.
                        printDialog1.AllowSomePages = true;

                        // Show the help button.
                        printDialog1.ShowHelp = true;
                    }

                    printDialog1.Document = this.document;

                    if (this.PrinterInfo != null)
                    {

                        // this.OutputText("恢复以前的打印机名: " + this.PrinterInfo.PrinterName + ", 纸张名: " + this.PrinterInfo.PaperName);

#if NO
                        string strPrinterName = document.PrinterSettings.PrinterName;
                        if (string.IsNullOrEmpty(this.PrinterInfo.PrinterName) == false
                            && this.PrinterInfo.PrinterName != strPrinterName)
                        {
                            this.document.PrinterSettings.PrinterName = this.PrinterInfo.PrinterName;
                            if (this.document.PrinterSettings.IsValid == false)
                            {
                                this.document.PrinterSettings.PrinterName = strPrinterName;
                                MessageBox.Show(this, "打印机 " + this.PrinterInfo.PrinterName + " 当前不可用,请重新选定打印机");
                                this.PrinterInfo.PrinterName = "";
                                bDisplayPrinterDialog = true;
                            }
                        }
#endif
                        // 按照存储的打印机名选定打印机
                        nRet = SelectPrinterByName(this.document,
                            this.PrinterInfo.PrinterName,
                            out  strError);
                        if (nRet == 1)
                        {
                            MessageBox.Show(this, "打印机 " + this.PrinterInfo.PrinterName + " 当前不可用,请重新选定打印机");
                            this.PrinterInfo.PrinterName = "";
                            bDisplayPrinterDialog = true;
                        }

#if NO
                        PaperSize old_papersize = document.DefaultPageSettings.PaperSize;
                        if (string.IsNullOrEmpty(this.PrinterInfo.PaperName) == false
                            && this.PrinterInfo.PaperName != document.DefaultPageSettings.PaperSize.PaperName)
                        {
                            PaperSize found = null;
                            foreach (PaperSize ps in this.document.PrinterSettings.PaperSizes)
                            {
                                if (ps.PaperName.Equals(this.PrinterInfo.PaperName))
                                {
                                    found = ps;
                                    break;
                                }
                            }

                            if (found != null)
                                this.document.DefaultPageSettings.PaperSize = found;
                            else
                            {
                                MessageBox.Show(this, "打印机 " + this.PrinterInfo.PrinterName + " 的纸张类型 " + this.PrinterInfo.PaperName + " 当前不可用,请重新选定纸张");
                                document.DefaultPageSettings.PaperSize = old_papersize;
                                this.PrinterInfo.PaperName = "";
                                bDisplayPrinterDialog = true;
                            }
                        }
#endif

                        // 需要自定义纸张
                        if (string.IsNullOrEmpty(this.label_param.DefaultPrinter) == true
                            && this.label_param.PageWidth > 0
                            && this.label_param.PageHeight > 0)
                        {
                            bCustomPaper = true;

                            PaperSize paper_size = new PaperSize("Custom Label", 
                                (int)label_param.PageWidth,
                                (int)label_param.PageHeight);
                            this.document.DefaultPageSettings.PaperSize = paper_size;
                        }


                        if (// bDisplayPrinterDialog == false && 
                            bCustomPaper == false
                            && string.IsNullOrEmpty(this.PrinterInfo.PaperName) == false)
                        {
                            nRet = SelectPaperByName(this.document,
                                this.PrinterInfo.PaperName,
                                this.PrinterInfo.Landscape,
                                true,   // false,
                                out strError);
                            if (nRet == 1)
                            {
                                MessageBox.Show(this, "打印机 " + this.PrinterInfo.PrinterName + " 的纸张类型 " + this.PrinterInfo.PaperName + " 当前不可用,请重新选定纸张");
                                this.PrinterInfo.PaperName = "";
                                bDisplayPrinterDialog = true;
                            }
                        }

                        // 只要有一个打印机事项没有确定,就要出现打印机对话框
                        if (bCustomPaper == false)
                        {
                            if (string.IsNullOrEmpty(this.PrinterInfo.PrinterName) == true
                                || string.IsNullOrEmpty(this.PrinterInfo.PaperName) == true)
                                bDisplayPrinterDialog = true;
                        }
                    }
                    else
                    {
                        // 没有首选配置的情况下要出现打印对话框
                        bDisplayPrinterDialog = true;
                    }

                    // this.document.DefaultPageSettings.Landscape = label_param.Landscape;

                    DialogResult result = DialogResult.OK;
                    if (bDisplayPrinterDialog == true)
                    {
                        result = printDialog1.ShowDialog();

                        if (result == DialogResult.OK)
                        {
                            if (bCustomPaper == true)
                            {
                                PaperSize paper_size = new PaperSize("Custom Label",
                                    (int)label_param.PageWidth,
                                    (int)label_param.PageHeight);
                                this.document.DefaultPageSettings.PaperSize = paper_size;
                            }

                            // 记忆打印参数
                            if (this.PrinterInfo == null)
                                this.PrinterInfo = new PrinterInfo();

                            // this.OutputText("打印机对话框返回后,新选定的打印机名: " + document.PrinterSettings.PrinterName + ", 纸张名: " + document.DefaultPageSettings.PaperSize.PaperName);

                            this.PrinterInfo.PrinterName = document.PrinterSettings.PrinterName;
                            // this.PrinterInfo.PaperName = document.PrinterSettings.DefaultPageSettings.PaperSize.PaperName;  // document.DefaultPageSettings.PaperSize.PaperName
                            this.PrinterInfo.PaperName = PrintUtil.GetPaperSizeString(document.DefaultPageSettings.PaperSize);
                            this.PrinterInfo.Landscape = document.DefaultPageSettings.Landscape;

                            if (bCustomPaper == false)
                            {
                                // 2014/3/27
                                // this.document.DefaultPageSettings = document.PrinterSettings.DefaultPageSettings;
                                nRet = SelectPaperByName(this.document,
                                    this.PrinterInfo.PaperName,
                                    this.PrinterInfo.Landscape,
                                    true,
                                    out strError);
                                if (nRet == 1)
                                {
                                    // MessageBox.Show(this, "打印机 " + this.PrinterInfo.PrinterName + " 的纸张类型 " + this.PrinterInfo.PaperName + " 当前不可用,请重新选定纸张");
                                    //this.PrinterInfo.PaperName = "";
                                    //bDisplayPrinterDialog = true;

                                    this.OutputText("打印机对话框返回后,经过检查,纸张 " + this.PrinterInfo.PaperName + " 不在打印机 " + this.PrinterInfo.PrinterName + " 的可用纸张列表中。出现对话框让用户重新选择纸张");


                                    SelectPaperDialog paper_dialog = new SelectPaperDialog();
                                    MainForm.SetControlFont(paper_dialog, this.Font, false);
                                    paper_dialog.Comment = "纸张 " + this.PrinterInfo.PaperName + " 不在打印机 " + this.PrinterInfo.PrinterName + " 的可用纸张列表中。\r\n请重新选定纸张";
                                    paper_dialog.Document = this.document;
                                    this.MainForm.AppInfo.LinkFormState(paper_dialog, "paper_dialog_state");
                                    paper_dialog.ShowDialog(this);
                                    this.MainForm.AppInfo.UnlinkFormState(paper_dialog);

                                    if (paper_dialog.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                                        return 0;

                                    this.OutputText("对话框中新选定的纸张名: " + document.DefaultPageSettings.PaperSize.PaperName);
                                }
                            }

                            this.PrinterInfo.PaperName = PrintUtil.GetPaperSizeString(document.DefaultPageSettings.PaperSize);
                            this.PrinterInfo.Landscape = document.DefaultPageSettings.Landscape;

                            SetTitle();
                        }
                        else
                            return 0;
                    }

                    TracePrinterInfo();

                    if (bPrintPreview == true)
                    {
                        printPreviewDialog1.Document = this.document;

                        this.MainForm.AppInfo.LinkFormState(printPreviewDialog1, "labelprintform_printpreviewdialog_state");
                        printPreviewDialog1.ShowDialog(this);
                        this.MainForm.AppInfo.UnlinkFormState(printPreviewDialog1);
                    }
                    else
                    {
                        this.document.Print();
                    }
                }
                finally
                {
                    if (bPrintPreview == false)
                        this.Cursor = oldCursor;
                    this.EnableControls(true);

                    this.EndPrint();    // 关闭标签文件。后面才能删除
                }
            }
            finally
            {
                this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行打印"
                + (bPrintPreview == true ? "预览" : "")
                + "</div>");
            }

            return 0;
        ERROR1:
            return -1;
        }