嗨,我想保存我在打印机首选项中所做的更改,但它们被忽略了。我在c#中这样做请帮助我谢谢
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
PrintDocument pd = new PrintDocument();
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)] static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalFree(IntPtr hMem);
[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,[MarshalAs(UnmanagedType.LPWStr)] string pDeviceName,IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);
private const int DM_IN_BUFFER = 8;
private const int DM_OUT_BUFFER = 2;
private const int DM_IN_PROMPT = 4;
private void ShowPrinterProperties(PrinterSettings settings)
{
IntPtr hDevMode = settings.GetHdevmode(settings.DefaultPageSettings);
IntPtr pDevMode = GlobalLock(hDevMode);
DocumentProperties(this.Handle, IntPtr.Zero, settings.PrinterName, pDevMode, pDevMode, DM_IN_PROMPT);
GlobalUnlock(hDevMode);
settings.SetHdevmode(hDevMode);
settings.DefaultPageSettings.SetHdevmode(hDevMode);
GlobalFree(hDevMode);
}
private void Form1_Load(object sender, EventArgs e)
{
// Add list of installed printers found to the combo box.
// The pkInstalledPrinters string will be used to provide the display string.
String pkInstalledPrinters;
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
comboBox1.Items.Add(pkInstalledPrinters);
//selectedPrinter = comboBox1.SelectedItem.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (pd.PrinterSettings.IsValid)
ShowPrinterProperties(pd.PrinterSettings);
else
MessageBox.Show("Invalid printer name");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
pd.PrinterSettings.PrinterName = comboBox1.Text;
// selectedPrinter = comboBox1.SelectedItem.ToString();
}
}