如何从Windows窗体程序保存打印设置? [英] How to save print settings from a Windows Forms Program?

查看:106
本文介绍了如何从Windows窗体程序保存打印设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();
}

用户选择打印机,然后单击属性按钮,进行一些更改(纸张尺寸,方向等),然后单击确定",然后在PrintDialog上单击确定".

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)?

推荐答案

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

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

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

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...
            }
        }
    }
}

这篇关于如何从Windows窗体程序保存打印设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆