如何从 Windows 窗体程序保存打印设置?

发布于 2024-10-12 22:18:31 字数 462 浏览 5 评论 0原文

我有打印标签的程序,我必须允许用户保存/记住打印机的设置。所以我有这样的代码:

private void printerToolStripButton_Click(object sender, EventArgs e)
{
     PrintDialog dialog = new PrintDialog();
     dialog.ShowDialog();
}

用户选择打印机并单击属性按钮,进行一些更改(纸张尺寸、方向等),然后单击“确定”,然后单击 PrintDialog 上的“确定”。

我的问题是,这些更改不会被记住...当我再次单击按钮或重新启动应用程序时,它们会消失...

有谁知道如何将它们保留在应用程序范围内?或者,如果应用程序范围不可能,那么也许如何将它们保存在系统中(所以当我进入控制面板 -> 打印机 -> 右键单击​​打印机 -> 首选项时,它们就会在那里)?

I have program that print labels and I have to allow user to save/remember settings for printer. So I have this code:

private void printerToolStripButton_Click(object sender, EventArgs e)
{
     PrintDialog dialog = new PrintDialog();
     dialog.ShowDialog();
}

User selects printer and clicks properties button, do some changes (paper size, orientation, etc.) and then click 'OK' and then click 'OK' on PrintDialog.

My problem is that those changes are not remembered... When I click button again or restart application they disappear...

Does anyone know how to persist them in application scope? Or if application scope is impossible, then maybe how to save them in the system (so when I go to control panel -> printers -> right click on printer -> preferences they will be there)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-10-19 22:18:31

Yu 可以使用我自己的接口驱动的序列化。 ;)

您可以使用我的 xml 序列化属性来扩展接口驱动的序列化。顺便说一句,当您使用接口继承时,接口驱动的序列化很酷;)

using System;
using System.IO;
using System.Windows.Forms;

// download at [http://xmlserialization.codeplex.com/]
using System.Xml.Serialization;
namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [XmlRootSerializer("PrinterSettings")]
        public interface IPrinterSettings
        {
            bool PrintToFile { get; set; }
        }

        private static readonly string PrinterConfigurationFullName = Path.Combine(Application.StartupPath, "PrinterSettings.xml");

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(PrinterConfigurationFullName))
            {
                XmlObjectSerializer.Load<IPrinterSettings>(File.ReadAllText(PrinterConfigurationFullName), printDialog1);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            File.WriteAllText(PrinterConfigurationFullName, XmlObjectSerializer.Save<IPrinterSettings>(printDialog1));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // do required stuff here...
            }
        }
    }
}

Yu can use my own interface-driven serialization. ;)

You can extend interface-driven serialization using my xml serialization properties. By the way, interface-driven serialization is cool when you are using interface inheritance ;)

using System;
using System.IO;
using System.Windows.Forms;

// download at [http://xmlserialization.codeplex.com/]
using System.Xml.Serialization;
namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [XmlRootSerializer("PrinterSettings")]
        public interface IPrinterSettings
        {
            bool PrintToFile { get; set; }
        }

        private static readonly string PrinterConfigurationFullName = Path.Combine(Application.StartupPath, "PrinterSettings.xml");

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(PrinterConfigurationFullName))
            {
                XmlObjectSerializer.Load<IPrinterSettings>(File.ReadAllText(PrinterConfigurationFullName), printDialog1);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            File.WriteAllText(PrinterConfigurationFullName, XmlObjectSerializer.Save<IPrinterSettings>(printDialog1));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // do required stuff here...
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文