C#打印:如何切换打印机设置

时间:2014-04-04 21:22:47

标签: c# .net printing

我有两种形式:

  namespace Drucken
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Print()
            {

                PrintDocument PrintDoc = new PrintDocument();

                Drucker DR = new Drucker(this);
                DR.ShowDialog();

                  PrintDoc.PrinterSettings.PrinterName = DR.SelectedSetting1.PrinterName;
                  PrintDoc.PrinterSettings.Copies = DR.SelectedSetting1.Copies;

                  // here i need the other printer settings [Color,PaperSize]...


                PrintDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                PrintDoc.Print();
            }

            void PrintPage(object sender, PrintPageEventArgs e)
            {           
               e.Graphics.DrawLine(new Pen(Color.Black), new Point(0, 0), new Point(100, 100));
               e.Graphics.DrawString("Hier Steht ein Text.", new Font("Times New Roman", 12), new SolidBrush(Color.Black), new Point(45, 45));

            }
            private void btnDrucken_Click(object sender, EventArgs e)
            {
                Print();
            }


        }
    }

窗体2:

namespace Drucken
{
    public partial class Drucker : Form
    {
        string Name1;
        int Anzahl1;
        PaperSize Papiergröße1;
        Form1 fh;
        private PrinterSettings m_SelectedSetting;
        public PrinterSettings SelectedSetting
        {
            get { return m_SelectedSetting; }
            set { m_SelectedSetting = value; }
        }
        public Drucker(Form1 aufrufer)
        {
            fh = aufrufer;
            InitializeComponent();
             m_SelectedSetting = new PrinterSettings();
        }        

        private void Drucker_Load(object sender, EventArgs e)
        {
            cmbDrucker.DataSource = PrinterSettings.InstalledPrinters.Cast<string>().ToList<string>();
            cmbDrucker.SelectedItem = m_SelectedSetting.PrinterName;
            SetPrinter();
        }

        private void cmbDrucker_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetPrinter();
        }

        private void SetPrinter()
        {
            PaperSize currentSize = cmbFormat.SelectedItem == null ? m_SelectedSetting.DefaultPageSettings.PaperSize : (PaperSize)cmbFormat.SelectedItem;

            if (cmbDrucker.SelectedItem != null)
            {
                cmbFormat.DataSource = m_SelectedSetting.PaperSizes.Cast<PaperSize>().ToList<PaperSize>();
                cmbFormat.DisplayMember = "PaperName";
                cmbFormat.ValueMember = "PaperName";
            }

            cmbFormat.SelectedValue = currentSize.PaperName;


            if (m_SelectedSetting.MaximumCopies < Convert.ToInt16(nudExemplare.Value))
            {
                nudExemplare.Maximum = Convert.ToDecimal(m_SelectedSetting.MaximumCopies);
                nudExemplare.Value = m_SelectedSetting.MaximumCopies;
            }

            if (m_SelectedSetting.DefaultPageSettings.Color)
                radFarbig.Checked = true;
            else
                radSchwarz.Checked = true;


        }

        private void btnOk_Click(object sender, EventArgs e)
        {

            try
            {
                m_SelectedSetting.Copies = Convert.ToInt16(nudExemplare.Value);
                Anzahl1 = Convert.ToInt16(nudExemplare.Value);

                if (cmbDrucker.SelectedItem != null)
                {
                    m_SelectedSetting.PrinterName = (string)cmbDrucker.SelectedItem;
                    Name1 = (string)cmbDrucker.SelectedItem;
                }

                if (cmbFormat.SelectedItem != null)
                {
                    m_SelectedSetting.DefaultPageSettings.PaperSize = (PaperSize)cmbFormat.SelectedItem;
                    Papiergröße1 = (PaperSize)cmbFormat.SelectedItem;
                }

                if (radFarbig.Checked)
                    m_SelectedSetting.DefaultPageSettings.Color = true;
                else
                    m_SelectedSetting.DefaultPageSettings.Color = false;

                DialogResult = DialogResult.OK;
                Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }
}

我的问题是,我不知道如何将打印机设置[Color,PaperSize](我可以选择在secound形式中)放入我的form1中,以便使用正确的设置打印...

编辑:

PrintDoc.PrinterSettings.PrinterName = DR.SelectedSetting1.PrinterName;
PrintDoc.PrinterSettings.Copies = DR.SelectedSetting1.Copies;

现在我只需要另外两个设置[Color,PaperSize]

0 个答案:

没有答案