locked
Multiple Pages X Sheet RRS feed

  • Question

  • hi All,

    using the Winforms PrintDialog, I'd like to know if and how the user set the "Multiple Pages per Sheet" property... I can see the .Duplex property, but nothing about the pages/sheet, so I can conclude the PriterSettings class has not been completely forged...

    so I'm trying to read the dmNup value from DEVMODE, and I thought it should be as simple as

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
    
            [Flags()]
            public enum DM : int
            {
                Orientation = 0x1,
                PaperSize = 0x2,
                PaperLength = 0x4,
                PaperWidth = 0x8,
                Scale = 0x10,
                Position = 0x20,
                NUP = 0x40,
                DisplayOrientation = 0x80,
                Copies = 0x100,
                DefaultSource = 0x200,
                PrintQuality = 0x400,
                Color = 0x800,
                Duplex = 0x1000,
                YResolution = 0x2000,
                TTOption = 0x4000,
                Collate = 0x8000,
                FormName = 0x10000,
                LogPixels = 0x20000,
                BitsPerPixel = 0x40000,
                PelsWidth = 0x80000,
                PelsHeight = 0x100000,
                DisplayFlags = 0x200000,
                DisplayFrequency = 0x400000,
                ICMMethod = 0x800000,
                ICMIntent = 0x1000000,
                MediaType = 0x2000000,
                DitherType = 0x4000000,
                PanningWidth = 0x8000000,
                PanningHeight = 0x10000000,
                DisplayFixedOutput = 0x20000000
            }
            public struct POINTL
              {
              public Int32 x;
              public Int32 y;
              }
                   
            [StructLayout(LayoutKind.Explicit)]
            public struct DEVMODE
            {
                    public const int CCHDEVICENAME = 32;
                    public const int CCHFORMNAME = 32;
                    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
                    [System.Runtime.InteropServices.FieldOffset(0)]
                    public string dmDeviceName;
                    [System.Runtime.InteropServices.FieldOffset(32)]
                    public Int16 dmSpecVersion;
                    [System.Runtime.InteropServices.FieldOffset(34)]
                    public Int16 dmDriverVersion;
                    [System.Runtime.InteropServices.FieldOffset(36)]
                    public Int16 dmSize;
                    [System.Runtime.InteropServices.FieldOffset(38)]
                    public Int16 dmDriverExtra;
                    [System.Runtime.InteropServices.FieldOffset(40)]
                    public DM dmFields;
                    [System.Runtime.InteropServices.FieldOffset(44)]
                    Int16 dmOrientation;
                    [System.Runtime.InteropServices.FieldOffset(46)]
                    Int16 dmPaperSize;
                    [System.Runtime.InteropServices.FieldOffset(48)]
                    Int16 dmPaperLength;
                    [System.Runtime.InteropServices.FieldOffset(50)]
                    Int16 dmPaperWidth;
                    [System.Runtime.InteropServices.FieldOffset(52)]
                    Int16 dmScale;
                    [System.Runtime.InteropServices.FieldOffset(54)]
                    Int16 dmCopies;
                    [System.Runtime.InteropServices.FieldOffset(56)]
                    Int16 dmDefaultSource;
                    [System.Runtime.InteropServices.FieldOffset(58)]
                    Int16 dmPrintQuality;
                    [System.Runtime.InteropServices.FieldOffset(44)]
                    public POINTL dmPosition;
                    [System.Runtime.InteropServices.FieldOffset(52)]
                    public Int32 dmDisplayOrientation;
                    [System.Runtime.InteropServices.FieldOffset(56)]
                    public Int32 dmDisplayFixedOutput;
                    [System.Runtime.InteropServices.FieldOffset(60)]
                    public short dmColor;
                    [System.Runtime.InteropServices.FieldOffset(62)]
                    public short dmDuplex;
                    [System.Runtime.InteropServices.FieldOffset(64)]
                    public short dmYResolution;
                    [System.Runtime.InteropServices.FieldOffset(66)]
                    public short dmTTOption;
                    [System.Runtime.InteropServices.FieldOffset(68)]
                    public short dmCollate;
                    [System.Runtime.InteropServices.FieldOffset(72)]
                    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
                    public string dmFormName;
                    [System.Runtime.InteropServices.FieldOffset(102)]
                    public Int16 dmLogPixels;
                    [System.Runtime.InteropServices.FieldOffset(104)]
                    public Int32 dmBitsPerPel;
                    [System.Runtime.InteropServices.FieldOffset(108)]
                    public Int32 dmPelsWidth;
                    [System.Runtime.InteropServices.FieldOffset(112)]
                    public Int32 dmPelsHeight;
                    [System.Runtime.InteropServices.FieldOffset(116)]
                    public Int32 dmDisplayFlags;
                    [System.Runtime.InteropServices.FieldOffset(116)]
                    public Int32 dmNup;
                    [System.Runtime.InteropServices.FieldOffset(120)]
                    public Int32 dmDisplayFrequency;
                }
    
            [DllImport("kernel32.dll")]
            static extern IntPtr GlobalLock(IntPtr hMem);
            [DllImport("kernel32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool GlobalUnlock(IntPtr hMem);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                PrintDialog PrintDialog1 = new PrintDialog();
                PrintDialog1.ShowDialog();
    
    
                IntPtr hDevMode = PrintDialog1.PrinterSettings.GetHdevmode();
                IntPtr pDevMode = GlobalLock(hDevMode);
                DEVMODE devmode = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE));
    
                GlobalUnlock(hDevMode);
    
            }
           
        }
    }
    

    where I first get a pointer to the structure already populated via the PrintDialog visual element, and finally read devmode.dmDup field to get user's choice on the dialog, but what I actually get is... "nothing", and the structure seems not to be correctly populated as well...

    do you have any hint on how to read that property?

    tia


    http://www.asql.biz - DbaMgr2k - DbaMgr and further SQL Tools http://www.hotelsole.com/

    • Moved by OmegaMan Friday, November 2, 2012 7:11 PM (From:Common Language Runtime)
    Friday, November 2, 2012 3:05 PM

Answers

  • hi,

    thank you for your involvement... your words are stressing that, from the "traditional" Winforms.PrintDialog, the PagesXsheet information can not be returned, even via a query based on API? so the marshalled DEVMODE structure will never include that info, am I correctly understanding you?

    tia and regards


    http://www.asql.biz - DbaMgr2k - DbaMgr and further SQL Tools http://www.hotelsole.com/

    Use the System.Printing.Interop.PrintTicketConverter.
    • Marked as answer by Bob Wu-MT Tuesday, November 27, 2012 11:35 AM
    Tuesday, November 6, 2012 1:04 PM

All replies

  • http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/90e26033-eb5d-4460-ad51-75629724a460/
    Monday, November 5, 2012 11:16 AM
  • hi,

    thank you for your input, but I do not actually see the point... nor in the WPF usage (which can not be used in the current project) nor in Helen's answer, as the pointed article actually "expects" the "pagesPerSheet" parameter in the class constructor...

    regards


    http://www.asql.biz - DbaMgr2k - DbaMgr and further SQL Tools http://www.hotelsole.com/

    Monday, November 5, 2012 11:59 AM
  • thank you for your input, but I do not actually see the point... nor in the WPF usage (which can not be used in the current project) nor in Helen's answer, as the pointed article actually "expects" the "pagesPerSheet" parameter in the class constructor...

    The point is you are trying to use a later framework than the .NET 2.0 framework of Windows.Forms.  This statement "which can not be used in the current project" doesn't seem to be true as you are using System.Linq.  Why wouldn't the "pagesPerSheet" parameter be expected in the constructor?  It's a parameter that has to be gotten from a PrintSchema that didn't exist when Windows.Forms existed.  Making your own PrintPage event to handle multiple pages per sheet seems like an involved undertaking.
    Monday, November 5, 2012 6:06 PM
  • hi,

    thank you for your involvement... your words are stressing that, from the "traditional" Winforms.PrintDialog, the PagesXsheet information can not be returned, even via a query based on API? so the marshalled DEVMODE structure will never include that info, am I correctly understanding you?

    tia and regards


    http://www.asql.biz - DbaMgr2k - DbaMgr and further SQL Tools http://www.hotelsole.com/

    Tuesday, November 6, 2012 10:57 AM
  • hi,

    thank you for your involvement... your words are stressing that, from the "traditional" Winforms.PrintDialog, the PagesXsheet information can not be returned, even via a query based on API? so the marshalled DEVMODE structure will never include that info, am I correctly understanding you?

    tia and regards


    http://www.asql.biz - DbaMgr2k - DbaMgr and further SQL Tools http://www.hotelsole.com/

    Use the System.Printing.Interop.PrintTicketConverter.
    • Marked as answer by Bob Wu-MT Tuesday, November 27, 2012 11:35 AM
    Tuesday, November 6, 2012 1:04 PM