彻底解决 C# 中页面设置对话框数值变化的 Bug

文章目录

在使用 C# 做页面设置时经常会在进行打印时发现下面的问题:当你打开一个 PageSetupDialog 并保存了一个页边距后,再次打开该窗口并显示当前 PageSettings 时,页边距会自动变小。

实际上,这是 .NET Framework 的一个 Bug。实际上这是单位转换的问题。

这个问题可以如下描述:

在出现的页面设置对话框中,页边距的单位是“页边距(毫米)”,可是“左、右、上、下”后面的文本框中的数字似乎是当前页边距以“百分之一英寸”为单位的数值,而不是以“毫米”为单位数值。

但是当你按下“确定”键时,屏幕上的当前的以“百分之一英寸”为单位的页边距数值似乎被理解成了以“毫米”为单位的数值,对页边距进行设定。  

当你再次按下“页面设置”按钮(调用PageSetupDialog类进行页面设置)时,它又把新的页边距以“百分之一英寸”为单位显示在屏幕上。  

如此反复,你每次进入“页面设置”对话框,按“确定”,再进入,再“确定”,页边距不断减少,这可不是我们希望的。

那么如何解决这个问题呢?网络上有很多解决方案。最主要是按如下代码在每次打开设置窗口之后进行单位转换

PrinterUnitConvert.Convert(mg,   PrinterUnit.Display,   PrinterUnit.TenthsOfAMillimeter); 

有人说,每次保存后,手工进行单位的转换。可是问题是,美国和中国,一个是公制单位,一个是美国单位,如果在中国大部分情况下没问题,如果 “Windows 区域设置”不在中国或修改为使用美制单位,那就会产生严重的问题。因为此时该 Bug 并不存在。你却把实际正确的以0.1毫米为单位的内容以百分之一英寸的名义转成了 0.1 毫米。

所以彻底解决该方法的最好途径是:重写代码,实现自己的页面设置。

在重写自己的页面设置时,有几个问题值得注意:

如何获取所有支持的纸张大小列表

使用一个打印机对象的 PaperSizes 属性获取该打印机支持的纸张列表。

PrinterSettings s = new PrinterSettings();
pc = s.PaperSizes;

for (int i = 0; i < pc.Count; ++i)  
{  
    // pc\[i\].PaperName :名称
    // pc\[i\].Width, Height:高度和宽度。  
}

如何横向打印

PageSettings 类实例的 Landscape 属性用于设置横向打印。

如何保存设置

设置可保存在一个 PageSettings 类中。通过 .NET 的 Xml 序列化或自己保存该类的所有字段即可保存页面设置并随时用这些保存的数据创建一个一样的新类用于给 PrintDocument 提供打印设置。

下面是我实现的类供参考。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace MySpace
{
    public partial class PrintPageSettingsForm : Form
    {
        private System.Drawing.Printing.PrinterSettings.PaperSizeCollection pc = null;
        PageSettings ps = null;
        public PrintPageSettingsForm()
        {
            InitializeComponent();
            this.ps = new PageSettings();
        }
        public PrintPageSettingsForm(ref PageSettings _ps)
        {
            InitializeComponent();
            this.ps = _ps;
        }
        public static PageSettings OpenDialog(IWin32Window parent, ref PageSettings ps)
        {
            (new PrintPageSettingsForm(ref ps)).ShowDialog(parent);
            return ps;
        }
        private void PrintPageSettingsForm_Load(object sender, EventArgs e)
        {
            if (PrinterSettings.InstalledPrinters.Count == 0)
            {
                Error.ShowError("您还没有安装打印机。在您打印或页面设置之前必须安装一个打印机。");
                return;
            }
            PrinterSettings s = new PrinterSettings();
            this.label2.Text += s.PrinterName;
            pc = s.PaperSizes;
            this.comboBox1.Items.Add("<自定义纸张...>");
            for (int i = 0; i < pc.Count; ++i)
            {
                this.comboBox1.Items.Add(pc[i].PaperName);
            }
            try
            {
                if (ps.PaperSize.PaperName.StartsWith("User"))
                {
                    this.comboBox1.SelectedIndex = 0;
                    swidth.Text = InchToMM(ps.PaperSize.Width).ToString();
                    sheight.Text = InchToMM(ps.PaperSize.Height).ToString();
                }
                else
                    this.comboBox1.SelectedItem = ps.PaperSize.PaperName;
            }
            catch
            {
                try
                {
                    this.comboBox1.SelectedItem = "A4";
                }
                catch
                {
                    this.comboBox1.SelectedIndex = 0;
                }
            }
            this.mleft.Text = ((int)InchToMM(ps.Margins.Left)).ToString();
            this.mright.Text = ((int)InchToMM(ps.Margins.Right)).ToString();
            this.mtop.Text = ((int)InchToMM(ps.Margins.Top)).ToString();
            this.mbottom.Text = ((int)InchToMM(ps.Margins.Bottom)).ToString();
            if (ps.Landscape)
            {
                this.radioButton2.Checked = true;
                this.radioButton1.Checked = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedIndex == 0)
            {
                this.panel1.Visible = true;
                return;
            }
            this.panel1.Visible = false;
            ps.PaperSize = pc[this.comboBox1.SelectedIndex - 1];
            this.label3.Text = "纸张大小:  " + InchToMM(pc[this.comboBox1.SelectedIndex - 1].Width).ToString() + " 毫米 × " + InchToMM(pc[this.comboBox1.SelectedIndex - 1].Height).ToString() + " 毫米";

        }
        private double InchToMM(double inch)
        {
            return Math.Round(PrinterUnitConvert.Convert(inch, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter) / 10, 0);
        }
        private int MMToInch(double inch)
        {
            return (int)PrinterUnitConvert.Convert(inch * 10, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ps.Landscape = this.radioButton2.Checked;
            if (this.comboBox1.SelectedIndex == 0)
            {
                if(double.Parse(this.swidth.Text)<=10 || (double.Parse(this.sheight.Text) <=10))
                {
                    Error.ShowError ("您输入的纸张宽度或高度无效。");
                    return;
                }
                ps.PaperSize = new PaperSize("User Paper", MMToInch(double.Parse(this.swidth.Text)), MMToInch(double.Parse(this.sheight.Text)));
            }
            ps.Margins.Top = MMToInch(double.Parse(mtop.Text));
            ps.Margins.Left = MMToInch(double.Parse(mleft.Text));
            ps.Margins.Right = MMToInch(double.Parse(this.mright.Text));
            ps.Margins.Bottom = MMToInch(double.Parse(this.mbottom.Text));
            this.Close();
        }
    }
}

设计文件:(PrintPageSettingsForm.Designer.cs)

namespace MySpace
{
    partial class PrintPageSettingsForm
    {
        /// /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label9 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.sheight = new CeejiJXC.NumberTextBox();
            this.swidth = new CeejiJXC.NumberTextBox();
            this.mbottom = new CeejiJXC.NumberTextBox();
            this.mtop = new CeejiJXC.NumberTextBox();
            this.mright = new CeejiJXC.NumberTextBox();
            this.mleft = new CeejiJXC.NumberTextBox();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 41);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "纸张类型: ";
            // 
            // comboBox1
            // 
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(83, 38);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(343, 20);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(16, 12);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(71, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "打 印 机:  ";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(16, 70);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 3;
            this.label3.Text = "纸张大小: ";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioButton2);
            this.groupBox1.Controls.Add(this.radioButton1);
            this.groupBox1.Location = new System.Drawing.Point(18, 102);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(174, 89);
            this.groupBox1.TabIndex = 4;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "打印方向";
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(20, 52);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(47, 16);
            this.radioButton2.TabIndex = 7;
            this.radioButton2.Text = "横向";
            this.radioButton2.UseVisualStyleBackColor = true;
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Checked = true;
            this.radioButton1.Location = new System.Drawing.Point(20, 25);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(89, 16);
            this.radioButton1.TabIndex = 6;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "纵向 (默认)";
            this.radioButton1.UseVisualStyleBackColor = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(139, 209);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 5;
            this.button1.Text = "确定";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(229, 209);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 6;
            this.button2.Text = "取消";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.mbottom);
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Controls.Add(this.mtop);
            this.groupBox2.Controls.Add(this.label7);
            this.groupBox2.Controls.Add(this.mright);
            this.groupBox2.Controls.Add(this.label5);
            this.groupBox2.Controls.Add(this.mleft);
            this.groupBox2.Controls.Add(this.label4);
            this.groupBox2.Location = new System.Drawing.Point(203, 102);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(223, 89);
            this.groupBox2.TabIndex = 7;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "边距设置 (毫米)";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(114, 54);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(41, 12);
            this.label6.TabIndex = 8;
            this.label6.Text = "下(&B):";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(18, 54);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(41, 12);
            this.label7.TabIndex = 6;
            this.label7.Text = "上(&T):";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(114, 27);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(41, 12);
            this.label5.TabIndex = 4;
            this.label5.Text = "右(&R):";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(18, 27);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(41, 12);
            this.label4.TabIndex = 0;
            this.label4.Text = "左(&L):";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.label9);
            this.panel1.Controls.Add(this.sheight);
            this.panel1.Controls.Add(this.label8);
            this.panel1.Controls.Add(this.swidth);
            this.panel1.Location = new System.Drawing.Point(80, 64);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(346, 27);
            this.panel1.TabIndex = 9;
            this.panel1.Visible = false;
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(210, 6);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(65, 12);
            this.label9.TabIndex = 12;
            this.label9.Text = "毫米(高度)";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(71, 6);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(65, 12);
            this.label8.TabIndex = 10;
            this.label8.Text = "毫米(宽度)";
            // 
            // sheight
            // 
            this.sheight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.sheight.CanInputDecimal = false;
            this.sheight.Location = new System.Drawing.Point(142, 3);
            this.sheight.Name = "sheight";
            this.sheight.Size = new System.Drawing.Size(62, 21);
            this.sheight.TabIndex = 11;
            this.sheight.Text = "0";
            this.sheight.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // swidth
            // 
            this.swidth.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.swidth.CanInputDecimal = false;
            this.swidth.Location = new System.Drawing.Point(3, 3);
            this.swidth.Name = "swidth";
            this.swidth.Size = new System.Drawing.Size(62, 21);
            this.swidth.TabIndex = 9;
            this.swidth.Text = "0";
            this.swidth.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // mbottom
            // 
            this.mbottom.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.mbottom.CanInputDecimal = false;
            this.mbottom.Location = new System.Drawing.Point(160, 51);
            this.mbottom.Name = "mbottom";
            this.mbottom.Size = new System.Drawing.Size(42, 21);
            this.mbottom.TabIndex = 9;
            this.mbottom.Text = "0";
            this.mbottom.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // mtop
            // 
            this.mtop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.mtop.CanInputDecimal = false;
            this.mtop.Location = new System.Drawing.Point(65, 51);
            this.mtop.Name = "mtop";
            this.mtop.Size = new System.Drawing.Size(42, 21);
            this.mtop.TabIndex = 7;
            this.mtop.Text = "0";
            this.mtop.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // mright
            // 
            this.mright.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.mright.CanInputDecimal = false;
            this.mright.Location = new System.Drawing.Point(160, 24);
            this.mright.Name = "mright";
            this.mright.Size = new System.Drawing.Size(42, 21);
            this.mright.TabIndex = 5;
            this.mright.Text = "0";
            this.mright.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // mleft
            // 
            this.mleft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.mleft.CanInputDecimal = false;
            this.mleft.Location = new System.Drawing.Point(65, 24);
            this.mleft.Name = "mleft";
            this.mleft.Size = new System.Drawing.Size(42, 21);
            this.mleft.TabIndex = 3;
            this.mleft.Text = "0";
            this.mleft.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // PrintPageSettingsForm
            // 
            this.AcceptButton = this.button1;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(442, 248);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "PrintPageSettingsForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "页面设置";
            this.Load += new System.EventHandler(this.PrintPageSettingsForm_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label4;
        private NumberTextBox mleft;
        private System.Windows.Forms.Label label5;
        private NumberTextBox mright;
        private NumberTextBox mbottom;
        private System.Windows.Forms.Label label6;
        private NumberTextBox mtop;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Panel panel1;
        private NumberTextBox swidth;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label9;
        private NumberTextBox sheight;
    }
}

本文版权遵循 CC BY-NC-SA 4.0发布,转载需附带本文链接。

当前页阅读量为: