private void ExecuteChangePaper(object obj)
        {
            try
            {
                var ptc = new PrintTicketConverter(CurrentPrinter.FullName, CurrentPrinter.ClientPrintSchemaVersion);
                var mainWindowPtr = new WindowInteropHelper(FullScreenPrintWindow).Handle;
                var myDevMode = ptc.ConvertPrintTicketToDevMode(CurrentPrinter.UserPrintTicket, BaseDevModeType.UserDefault);
                var pinnedDevMode = GCHandle.Alloc(myDevMode, GCHandleType.Pinned);
                var pDevMode = pinnedDevMode.AddrOfPinnedObject();
                var result = DocumentProperties(mainWindowPtr, IntPtr.Zero, CurrentPrinter.FullName, pDevMode, pDevMode, 14);
                if (result == 1)
                {
                    CurrentPrinter.UserPrintTicket = ptc.ConvertDevModeToPrintTicket(myDevMode);
                    pinnedDevMode.Free();
                    PrintCopyCount = CurrentPrinter.UserPrintTicket.CopyCount.Value;
                    SetPageOrientation(CurrentPrinter.UserPrintTicket.PageOrientation);
                    SetCurrentPaper(CurrentPrinter.UserPrintTicket.PageMediaSize);
                    ExecuteSetPrintingOptions(null);
                }
            }
            catch (Exception)
            {

            }
        }
Esempio n. 2
0
            private PrintTicket AcquirePrintTicket(IntPtr devModeHandle, string printQueueName)
            {
                PrintTicket result = null;

                byte[] array  = null;
                IntPtr intPtr = IntPtr.Zero;

                try
                {
                    intPtr = UnsafeNativeMethods.GlobalLock(devModeHandle);
                    NativeMethods.DEVMODE devmode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(intPtr, typeof(NativeMethods.DEVMODE));
                    array = new byte[(int)(devmode.dmSize + devmode.dmDriverExtra)];
                    Marshal.Copy(intPtr, array, 0, array.Length);
                }
                finally
                {
                    if (intPtr != IntPtr.Zero)
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle);
                    }
                }
                SystemDrawingHelper.NewDefaultPrintingPermission().Assert();
                try
                {
                    using (PrintTicketConverter printTicketConverter = new PrintTicketConverter(printQueueName, PrintTicketConverter.MaxPrintSchemaVersion))
                    {
                        result = printTicketConverter.ConvertDevModeToPrintTicket(array);
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
                return(result);
            }
Esempio n. 3
0
            AcquirePrintTicket(
                IntPtr devModeHandle,
                string printQueueName
                )
            {
                PrintTicket printTicket = null;

                byte[] devModeData = null;

                //
                // Copy the devmode into a byte array
                //
                IntPtr pDevMode = IntPtr.Zero;

                try
                {
                    pDevMode = UnsafeNativeMethods.GlobalLock(devModeHandle);

                    NativeMethods.DEVMODE devMode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(
                        pDevMode,
                        typeof(NativeMethods.DEVMODE));
                    devModeData = new byte[devMode.dmSize + devMode.dmDriverExtra];
                    Marshal.Copy(pDevMode, devModeData, 0, devModeData.Length);
                }
                finally
                {
                    if (pDevMode != IntPtr.Zero)
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle);
                    }
                }

                SystemDrawingHelper.NewDefaultPrintingPermission().Assert(); //BlessedAssert
                try
                {
                    //
                    // Convert the devmode data to a PrintTicket object
                    //
                    using (PrintTicketConverter ptConverter = new PrintTicketConverter(
                               printQueueName,
                               PrintTicketConverter.MaxPrintSchemaVersion))
                    {
                        printTicket = ptConverter.ConvertDevModeToPrintTicket(devModeData);
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }

                return(printTicket);
            }
Esempio n. 4
0
        private PrintTicket GetPrintTicket(PrintQueue printQueue, System.Drawing.Printing.PageSettings pageSettings)
        {
            PrintTicketConverter printTicketConverter = new PrintTicketConverter(printQueue.FullName, printQueue.ClientPrintSchemaVersion);
            IntPtr devmodeHandle = pageSettings.PrinterSettings.GetHdevmode(pageSettings);
            int    size          = (int)GlobalSize(devmodeHandle);
            IntPtr devmodePtr    = GlobalLock(devmodeHandle);

            byte[] devMode = new byte[size];
            Marshal.Copy(devmodePtr, devMode, 0, size);
            GlobalUnlock(devmodeHandle);
            GlobalFree(devmodeHandle);
            return(printTicketConverter.ConvertDevModeToPrintTicket(devMode));
        }
Esempio n. 5
0
        public GDIPrinterDeviceContext(string printername, PrintTicket ticket)
            : base(true)
        {
            PrintTicketConverter ptc = new PrintTicketConverter(printername, PrintTicketConverter.MaxPrintSchemaVersion);

            byte[] dmdata = ptc.ConvertPrintTicketToDevMode(ticket, BaseDevModeType.PrinterDefault);
            this.PrintTicket = ptc.ConvertDevModeToPrintTicket(dmdata);
            IntPtr dmptr = Marshal.AllocHGlobal(dmdata.Length);

            Marshal.Copy(dmdata, 0, dmptr, dmdata.Length);
            this.HDC = NativeMethods.CreateDC("WINSPOOL", printername, null, dmptr);
            Marshal.FreeHGlobal(dmptr);
        }
Esempio n. 6
0
        private PrintTicket OpenPrinterProperties(Window window, PrintQueue printQueue, PrintTicket printTicket)
        {
            var ptc           = new PrintTicketConverter(printQueue.FullName, printQueue.ClientPrintSchemaVersion);
            var mainWindowPtr = new WindowInteropHelper(window).Handle;

            var devMode = ptc.ConvertPrintTicketToDevMode(printTicket, BaseDevModeType.UserDefault);

            var pinnedDevMode = GCHandle.Alloc(devMode, GCHandleType.Pinned);
            var pDevMode      = pinnedDevMode.AddrOfPinnedObject();

            Win32.DocumentProperties(mainWindowPtr, IntPtr.Zero, printQueue.FullName, pDevMode, pDevMode, 14);

            var newTicket = ptc.ConvertDevModeToPrintTicket(devMode);

            pinnedDevMode.Free();

            return(newTicket);
        }
Esempio n. 7
0
 private static unsafe PrintTicket ShowPrintPropertiesDialog(IntPtr hwnd, PrintQueue printQueue, PrintDialogDelegate dlg, PrintTicket printTicket)
 {
     using (var printTicketConverter = new PrintTicketConverter(printQueue.Name, PrintTicketConverter.MaxPrintSchemaVersion))
     {
         var bDevModeIn  = printTicketConverter.ConvertPrintTicketToDevMode(printTicket, BaseDevModeType.UserDefault, PrintTicketScope.JobScope);
         var bDevModeOut = new byte[bDevModeIn.Length];
         fixed(byte *pDevModeOut = bDevModeOut, pDevModeIn = bDevModeIn)
         {
             if (dlg(hwnd, printQueue.Name, new IntPtr(pDevModeIn), new IntPtr(pDevModeOut)))
             {
                 return(printTicketConverter.ConvertDevModeToPrintTicket(bDevModeOut, PrintTicketScope.JobScope));
             }
             else
             {
                 return(null);
             }
         }
     }
 }         // func ShowPrintPropertiesDialog
Esempio n. 8
0
            AcquirePrintTicket(
                IntPtr devModeHandle,
                string printQueueName
                )
            {
                PrintTicket printTicket = null;

                byte[] devModeData = null;

                //
                // Copy the devmode into a byte array
                //
                IntPtr pDevMode = IntPtr.Zero;

                try
                {
                    pDevMode = UnsafeNativeMethods.GlobalLock(devModeHandle);

                    NativeMethods.DEVMODE devMode = Marshal.PtrToStructure <NativeMethods.DEVMODE>(pDevMode);
                    devModeData = new byte[devMode.dmSize + devMode.dmDriverExtra];
                    Marshal.Copy(pDevMode, devModeData, 0, devModeData.Length);
                }
                finally
                {
                    if (pDevMode != IntPtr.Zero)
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle);
                    }
                }

                //
                // Convert the devmode data to a PrintTicket object
                //
                using (PrintTicketConverter ptConverter = new PrintTicketConverter(
                           printQueueName,
                           PrintTicketConverter.MaxPrintSchemaVersion))
                {
                    printTicket = ptConverter.ConvertDevModeToPrintTicket(devModeData);
                }

                return(printTicket);
            }
Esempio n. 9
0
        }         // proc SetPrintTicket

        /// <summary>Copy printer settings from System.Drawing to System.Printing</summary>
        /// <param name="printerSettings"></param>
        /// <param name="printQueue"></param>
        /// <param name="printTicket"></param>
        public static unsafe void SetPrinterSettings(this PrinterSettings printerSettings, out PrintQueue printQueue, out PrintTicket printTicket)
        {
            using (var printTicketConverter = new PrintTicketConverter(printerSettings.PrinterName, PrintTicketConverter.MaxPrintSchemaVersion))
                using (var printServer = new LocalPrintServer())
                {
                    printQueue = printServer.GetPrintQueue(printerSettings.PrinterName);

                    var hDevMode = printerSettings.GetHdevmode();
                    try
                    {
                        var pDevMode = NativeMethods.GlobalLock(hDevMode);
                        var bDevMode = new byte[NativeMethods.GlobalSize(hDevMode).ToInt32()];
                        Marshal.Copy(pDevMode, bDevMode, 0, bDevMode.Length);
                        NativeMethods.GlobalUnlock(hDevMode);

                        printTicket = printTicketConverter.ConvertDevModeToPrintTicket(bDevMode, PrintTicketScope.JobScope);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(hDevMode);
                    }
                }
        }         // proc SetPrinterSettings
Esempio n. 10
0
            /// <summary>
            /// Find the PrintTicket for the queue
            /// </summary>
            /// <param name="dModeHnd"></param>
            /// <param name="printQueueName"></param>
            /// <returns></returns>
            PrintTicket FindPrintTicket(IntPtr dModeHnd, string printQueueName)
            {
                byte[] dModeBytes = null;
                IntPtr ptr        = IntPtr.Zero;

                try
                {
                    //Convert the native DevMode to a managed array of bytes
                    ptr = NativeMethods.GlobalLock(dModeHnd);
                    NativeMethods.DEVMODE dmode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(ptr, typeof(NativeMethods.DEVMODE));
                    dModeBytes = new byte[dmode.dmSize + dmode.dmDriverExtra];
                    Marshal.Copy(ptr, dModeBytes, 0, dModeBytes.Length);
                }
                finally
                {
                    if (ptr != IntPtr.Zero)
                    {
                        NativeMethods.GlobalUnlock(dModeHnd);
                    }
                }

                new PrintingPermission(PrintingPermissionLevel.DefaultPrinting).Assert();

                try
                {
                    //Convert the bytes to a native PrintTicket
                    using (PrintTicketConverter converter = new PrintTicketConverter(printQueueName, PrintTicketConverter.MaxPrintSchemaVersion))
                    {
                        return(converter.ConvertDevModeToPrintTicket(dModeBytes));
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
            }
Esempio n. 11
0
        // Handles printer properties click
        private void OnPrinterPropertiesClick(object sender, RoutedEventArgs e)
        {
            PrintQueue selectedPrintQueue = (printersComboBox.SelectedItem as PrinterInfo).Queue;
            selectedPrintQueue.UserPrintTicket.PageOrientation = orientationComboBox.SelectedIndex == 0 ?
              PageOrientation.Portrait : PageOrientation.Landscape;
            PrintTicketConverter ptc = new PrintTicketConverter(selectedPrintQueue.FullName, selectedPrintQueue.ClientPrintSchemaVersion);
            IntPtr mainWindowPtr = new WindowInteropHelper(Window.GetWindow(this)).Handle;

            byte[] myDevMode = ptc.ConvertPrintTicketToDevMode(selectedPrintQueue.UserPrintTicket, BaseDevModeType.UserDefault);
            GCHandle pinnedDevMode = GCHandle.Alloc(myDevMode, GCHandleType.Pinned);
            IntPtr pDevMode = pinnedDevMode.AddrOfPinnedObject();
            DocumentProperties(mainWindowPtr, IntPtr.Zero, selectedPrintQueue.FullName, pDevMode, pDevMode, 14);
            selectedPrintQueue.UserPrintTicket = ptc.ConvertDevModeToPrintTicket(myDevMode);
            pinnedDevMode.Free();
        }
Esempio n. 12
0
			/// <summary>
			/// Find the PrintTicket for the queue
			/// </summary>
			/// <param name="dModeHnd"></param>
			/// <param name="printQueueName"></param>
			/// <returns></returns>
			PrintTicket FindPrintTicket(IntPtr dModeHnd, string printQueueName)
			{
				byte[] dModeBytes = null;
				IntPtr ptr = IntPtr.Zero;

				try
				{
					//Convert the native DevMode to a managed array of bytes
					ptr = NativeMethods.GlobalLock(dModeHnd);
					NativeMethods.DEVMODE dmode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(ptr, typeof(NativeMethods.DEVMODE));
					dModeBytes = new byte[dmode.dmSize + dmode.dmDriverExtra];
					Marshal.Copy(ptr, dModeBytes, 0, dModeBytes.Length);
				}
				finally
				{
					if (ptr != IntPtr.Zero)
						NativeMethods.GlobalUnlock(dModeHnd);
				}

				new PrintingPermission(PrintingPermissionLevel.DefaultPrinting).Assert();

				try
				{
					//Convert the bytes to a native PrintTicket
					using (PrintTicketConverter converter = new PrintTicketConverter(printQueueName, PrintTicketConverter.MaxPrintSchemaVersion))
					{
						return converter.ConvertDevModeToPrintTicket(dModeBytes);
					}
				}
				finally
				{
					CodeAccessPermission.RevertAssert();
				}
			}
Esempio n. 13
0
        /// <summary>
        /// Invoke the printer option window
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="printer"></param>
        /// <returns></returns>
        public bool OpenPrintOptions(Window parent, PrintQueue printer)
        {
            if (parent == null)
                throw new ArgumentNullException("Parent window of printer dialog is null.");

            using (PrintTicketConverter ptc = new PrintTicketConverter(printer.FullName, printer.ClientPrintSchemaVersion))
            {
                IntPtr mainWindowPtr = new WindowInteropHelper(parent).Handle;
                byte[] myDevMode = ptc.ConvertPrintTicketToDevMode(printer.UserPrintTicket, BaseDevModeType.UserDefault);
                GCHandle pinnedDevMode = GCHandle.Alloc(myDevMode, GCHandleType.Pinned);
                IntPtr pDevMode = pinnedDevMode.AddrOfPinnedObject();
                int result = DocumentPropertiesFunc(mainWindowPtr, IntPtr.Zero, printer.FullName, pDevMode, pDevMode, 14);
                if (result == 1)
                {
                    printer.UserPrintTicket = ptc.ConvertDevModeToPrintTicket(myDevMode);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
Esempio n. 14
0
            AcquirePrintTicket(
                IntPtr devModeHandle,
                string printQueueName
                ) 
            {
                PrintTicket printTicket = null; 
                byte[] devModeData = null; 

                // 
                // Copy the devmode into a byte array
                //
                IntPtr pDevMode = IntPtr.Zero;
                try 
                {
                    pDevMode = UnsafeNativeMethods.GlobalLock(devModeHandle); 
 
                    NativeMethods.DEVMODE devMode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(
                        pDevMode, 
                        typeof(NativeMethods.DEVMODE));
                    devModeData = new byte[devMode.dmSize + devMode.dmDriverExtra];
                    Marshal.Copy(pDevMode, devModeData, 0, devModeData.Length);
                } 
                finally
                { 
                    if (pDevMode != IntPtr.Zero) 
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle); 
                    }
                }

                (new PrintingPermission(PrintingPermissionLevel.DefaultPrinting)).Assert(); //BlessedAssert 
                try
                { 
                    // 
                    // Convert the devmode data to a PrintTicket object
                    // 
                    using (PrintTicketConverter ptConverter = new PrintTicketConverter(
                                printQueueName,
                                PrintTicketConverter.MaxPrintSchemaVersion))
                    { 
                        printTicket = ptConverter.ConvertDevModeToPrintTicket(devModeData);
                    } 
                } 
                finally
                { 
                    PrintingPermission.RevertAssert();
                }

                return printTicket; 
            }
Esempio n. 15
0
        public void ShowSettings()
        {
            PrintTicketConverter ptc = new PrintTicketConverter(PrinterName, printqueue.ClientPrintSchemaVersion);
            IntPtr mainWindowPtr = this.Handle;

            byte[] myDevMode = ptc.ConvertPrintTicketToDevMode(ticket, BaseDevModeType.PrinterDefault);
            GCHandle pinnedDevMode = GCHandle.Alloc(myDevMode, GCHandleType.Pinned);
            IntPtr pDevMode = pinnedDevMode.AddrOfPinnedObject();
            int res = Helpers.DocumentProperties(mainWindowPtr, IntPtr.Zero, printqueue.FullName, pDevMode, pDevMode, 14);
            if (res == 1)
            {
                ticket = ptc.ConvertDevModeToPrintTicket(myDevMode);
                pinnedDevMode.Free();
                NotifyPropertyChanged("PageOrientation");
                NotifyPropertyChanged("NumberOfCopies");
                GenerateFixedDocument(false);
            }
        }
        private void ExecuteChangePaper(object obj)
        {
            try
            {
                var ptc = new PrintTicketConverter(CurrentPrinter.FullName, CurrentPrinter.ClientPrintSchemaVersion);
                var mainWindowPtr = new WindowInteropHelper(FullScreenPrintWindow).Handle;
                var myDevMode = ptc.ConvertPrintTicketToDevMode(CurrentPrinter.UserPrintTicket, BaseDevModeType.UserDefault);
                var pinnedDevMode = GCHandle.Alloc(myDevMode, GCHandleType.Pinned);
                var pDevMode = pinnedDevMode.AddrOfPinnedObject();
                var result = DocumentProperties(mainWindowPtr, IntPtr.Zero, CurrentPrinter.FullName, pDevMode, pDevMode, 14);
                if (result == 1)
                {
                    CurrentPrinter.UserPrintTicket = ptc.ConvertDevModeToPrintTicket(myDevMode);
                    pinnedDevMode.Free();
                    PrintCopyCount = CurrentPrinter.UserPrintTicket.CopyCount.Value;
                    SetPageOrientation(CurrentPrinter.UserPrintTicket.PageOrientation);
                    SetCurrentPaper(CurrentPrinter.UserPrintTicket.PageMediaSize);
                    ExecuteSetPrintingOptions(null);
                }
            }
            catch (Exception)
            {

            }
        }
Esempio n. 17
0
 private PrintTicket GetPrintTicket(PrintQueue printQueue, System.Drawing.Printing.PageSettings pageSettings)
 {
     PrintTicketConverter printTicketConverter = new PrintTicketConverter(printQueue.FullName, printQueue.ClientPrintSchemaVersion);
     IntPtr devmodeHandle = pageSettings.PrinterSettings.GetHdevmode(pageSettings);
     int size = (int)GlobalSize(devmodeHandle);
     IntPtr devmodePtr = GlobalLock(devmodeHandle);
     byte[] devMode = new byte[size];
     Marshal.Copy(devmodePtr, devMode, 0, size);
     GlobalUnlock(devmodeHandle);
     GlobalFree(devmodeHandle);
     return printTicketConverter.ConvertDevModeToPrintTicket(devMode);
 }