Hi, I’d like to show printer properties dialog in my code.
The first possibility I have is to use the standard windows dialog like:
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
printDialog.Document = this.printDocument;
printDialog.ShowDialog();
Then the user has to click the „Properties“ button and the dialog I wanna have pops up.
But I’d like to show this dialog directly without using System.Windows.Forms.PrintDialog.
Here is the code I have from codeproject.net:
[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, ref IntPtr pDevModeInput, int fMode);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern bool GlobalFree(IntPtr hMem);
public void OpenPrinterPropertiesDialog(PrinterSettings printerSettings, System.IntPtr pHandle)
{
IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.Defaul tPageSettings);
IntPtr pDevMode = GlobalLock(hDevMode);
int sizeNeeded = DocumentProperties(pHandle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, ref pDevMode, 0);
IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
DocumentProperties(pHandle, IntPtr.Zero, printerSettings.PrinterName, devModeData, ref pDevMode, 4);
GlobalUnlock(hDevMode);
printerSettings.SetHdevmode(devModeData);
printerSettings.DefaultPageSettings.SetHdevmode(de vModeData);
GlobalFree(hDevMode);
Marshal.FreeHGlobal(devModeData);
}
The function OpenPrinterProperties calls the dialog I need. The only problem is:
-the dialog does not represents the settings printerSettings.DefaultPageSettings
-the changes you make in this dialog are not kept when you close the dialog
(normally the changes should be written in printerSettings.DefaultPageSettings)
What’s wrong with this code?
The first possibility I have is to use the standard windows dialog like:
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
printDialog.Document = this.printDocument;
printDialog.ShowDialog();
Then the user has to click the „Properties“ button and the dialog I wanna have pops up.
But I’d like to show this dialog directly without using System.Windows.Forms.PrintDialog.
Here is the code I have from codeproject.net:
[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, ref IntPtr pDevModeInput, int fMode);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern bool GlobalFree(IntPtr hMem);
public void OpenPrinterPropertiesDialog(PrinterSettings printerSettings, System.IntPtr pHandle)
{
IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.Defaul tPageSettings);
IntPtr pDevMode = GlobalLock(hDevMode);
int sizeNeeded = DocumentProperties(pHandle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, ref pDevMode, 0);
IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
DocumentProperties(pHandle, IntPtr.Zero, printerSettings.PrinterName, devModeData, ref pDevMode, 4);
GlobalUnlock(hDevMode);
printerSettings.SetHdevmode(devModeData);
printerSettings.DefaultPageSettings.SetHdevmode(de vModeData);
GlobalFree(hDevMode);
Marshal.FreeHGlobal(devModeData);
}
The function OpenPrinterProperties calls the dialog I need. The only problem is:
-the dialog does not represents the settings printerSettings.DefaultPageSettings
-the changes you make in this dialog are not kept when you close the dialog
(normally the changes should be written in printerSettings.DefaultPageSettings)
What’s wrong with this code?