jeudi 17 avril 2014

.NET - C# impression : Comment remise sur les paramètres de l'imprimante - Stack Overflow


I have two forms:


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

PrintDocument PrintDoc = new PrintDocument();

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

// here i need the printer settings...


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


}
}

Form2:


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


}
}
}

My problem is, that i dont know how i get the printersettings [PrinterName,Copie,Color,PaperSize] (which i can choose in the secound form) into my form1, so that it is printing with the right settings...



I have two forms:


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

PrintDocument PrintDoc = new PrintDocument();

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

// here i need the printer settings...


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


}
}

Form2:


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


}
}
}

My problem is, that i dont know how i get the printersettings [PrinterName,Copie,Color,PaperSize] (which i can choose in the secound form) into my form1, so that it is printing with the right settings...


0 commentaires:

Enregistrer un commentaire