Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In my application, i need to custumize printer dialog, then I don't want to use
printdialog.ShowDialog()

I've made my own dialog, and i wonder if it is possible to call only the window which configures "advanced properties" for a given printer ?

Thanks for all response.

Aurore
Posted

Hello,

Yes it is possible.

One way to do it is to use the win32 API.

Valery

These are the API you need to use:

C#
[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);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GlobalLock(IntPtr handle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool GlobalUnlock(IntPtr handle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GlobalFree(IntPtr handle);


And this is how to call them:

C#
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.PrinterName = "HP Deskjet D2400 series";

IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
IntPtr pDevMode = GlobalLock(hDevMode);
var mainWindowPtr = new WindowInteropHelper(this).Handle;

int sizeNeeded = DocumentProperties(mainWindowPtr, IntPtr.Zero, printerSettings.PrinterName, pDevMode, pDevMode, 0);
IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
DocumentProperties(mainWindowPtr, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, 14);
GlobalUnlock(hDevMode);
printerSettings.SetHdevmode(devModeData);
printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
GlobalFree(hDevMode);
Marshal.FreeHGlobal(devModeData);


Good luck.

Valery.
 
Share this answer
 
Hello,

Thanks a lot, it works great !

still have an issue.
I obtain a PrinterSettings object, and i need to use PrintTicket.

Do you know how to convert one to other ?

Thanks
Aurore
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900