コード例 #1
0
    private void SetPrinterDevMode(IntPtr hPrinter, string printerName, string paperName)
    {
        // INIT
        structDevMode  devMode     = new structDevMode();
        PRINTER_INFO_9 printerInfo = new PRINTER_INFO_9();

        printerInfo.pDevMode = IntPtr.Zero;
        IntPtr hDevMode = GetDevMode(hPrinter, printerName, ref devMode);

        // SET THE FORM NAME FIELDS TO INDICATE THAT THIS FIELD WILL BE MODIFIED
        devMode.dmFields = 0x10000;     // DM_FORMNAME
        // SET THE FORM NAME
        devMode.dmFormName = paperName;
        ChangeDevMode(hPrinter, printerName, paperName, devMode, hDevMode, ref printerInfo);
        IntPtr hPrinterInfo = GetPrinterInfo(hPrinter);

        // FILL THE PRINTER INFO STRUCTURE
        printerInfo          = (PRINTER_INFO_9)Marshal.PtrToStructure(hPrinterInfo, printerInfo.GetType());
        printerInfo.pDevMode = hDevMode;
        // GET A POINTER TO THE PRINTER INFO STRUCTURE
        Marshal.StructureToPtr(printerInfo, hPrinterInfo, true);
        // SET THE PRINTER SETTINGS
        bool bSuccess = SetPrinter(hPrinter, 9, hPrinterInfo, 0);

        if (!bSuccess)
        {
            throw new Exception("Set printer failed!");
        }
    }
コード例 #2
0
    private static void ChangeDevMode(IntPtr hPrinter, string printerName, string paperName, structDevMode devMode, IntPtr hDevMode, ref PRINTER_INFO_9 printerInfo)
    {
        // PUT THE DEV_MODE STRUCTURE BACK INTO THE POINTER
        Marshal.StructureToPtr(devMode, hDevMode, true);
        // MERGE THE NEW CHAGES WITH THE OLD
        int iRet = DocumentProperties(IntPtr.Zero, hPrinter, printerName,
                                      printerInfo.pDevMode, printerInfo.pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);

        if (iRet < 0)
        {
            throw new ApplicationException("Unable to set the orientation setting for this printer.");
        }
    }
コード例 #3
0
        /// <summary>
        /// Method Name : SetPrinterDuplex
        /// Programmatically set the Duplex flag for the specified printer driver's default properties.
        /// </summary>
        /// <param name="sPrinterName"> sPrinterName - The name of the printer to be used. </param>
        /// <param name="nDuplexSetting">
        /// nDuplexSetting - One of the following standard settings:
        /// 1 = None
        /// 2 = Duplex on long edge (book)
        /// 3 = Duplex on short edge (legal)
        /// </param>
        ///  <param name="errorMessage"> this will contain error messsage if any. </param>
        /// <returns>
        /// Returns: True on success, False on error.
        /// </returns>
        /// <remarks>
        ///
        /// </remarks>
        public bool SetPrinterDuplex(string sPrinterName, int nDuplexSetting, out string errorMessage)
        {
            errorMessage = string.Empty;
            bool             functionReturnValue = false;
            IntPtr           hPrinter            = default(IntPtr);
            PRINTER_DEFAULTS pd             = default(PRINTER_DEFAULTS);
            PRINTER_INFO_9   pinfo          = new PRINTER_INFO_9();
            DEVMODE          dm             = new DEVMODE();
            IntPtr           ptrPrinterInfo = default(IntPtr);
            int   nBytesNeeded = 0;
            int   nRet         = 0;
            Int32 nJunk        = default(Int32);

            if ((nDuplexSetting < 1) | (nDuplexSetting > 3))
            {
                errorMessage = "Error: dwDuplexSetting is incorrect.";
                return(functionReturnValue);
            }
            pd.DesiredAccess = PRINTER_ACCESS_USE;
            nRet             = OpenPrinter(sPrinterName, out hPrinter, ref pd);
            if ((nRet == 0) | (hPrinter.ToInt32() == 0))
            {
                if (GetLastError() == 5)
                {
                    errorMessage = "Access denied -- See the article for more info.";
                }
                else
                {
                    errorMessage = "Cannot open the printer specified " + "(make sure the printer name is correct).";
                }
                return(functionReturnValue);
            }
            nRet = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, IntPtr.Zero, IntPtr.Zero, 0);
            if ((nRet < 0))
            {
                errorMessage = "Cannot get the size of the DEVMODE structure.";
                goto cleanup;
            }
            IntPtr iparg = Marshal.AllocCoTaskMem(nRet + 100);

            nRet = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, iparg, IntPtr.Zero, DM_OUT_BUFFER);
            if ((nRet < 0))
            {
                errorMessage = "Cannot get the DEVMODE structure.";
                goto cleanup;
            }
            dm = (DEVMODE)Marshal.PtrToStructure(iparg, dm.GetType());
            if (!Convert.ToBoolean(dm.dmFields & DM_DUPLEX))
            {
                errorMessage = "You cannot modify the duplex flag for this printer " + "because it does not support duplex or the driver " + "does not support setting it from the Windows API.";
                goto cleanup;
            }
            dm.dmDuplex = (short)nDuplexSetting;
            Marshal.StructureToPtr(dm, iparg, true);
            nRet = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, pinfo.pDevMode, pinfo.pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
            if ((nRet < 0))
            {
                errorMessage = "Unable to set duplex setting to this printer.";
                goto cleanup;
            }
            GetPrinter(hPrinter, 9, IntPtr.Zero, 0, ref nBytesNeeded);
            if ((nBytesNeeded == 0))
            {
                errorMessage = "GetPrinter failed.";
                goto cleanup;
            }
            ptrPrinterInfo = Marshal.AllocCoTaskMem(nBytesNeeded + 100);
            nRet           = GetPrinter(hPrinter, 9, ptrPrinterInfo, nBytesNeeded, ref nJunk) ? 1 : 0;
            if ((nRet == 0))
            {
                errorMessage = "Unable to get shared printer settings.";
                goto cleanup;
            }
            pinfo                     = (PRINTER_INFO_9)Marshal.PtrToStructure(ptrPrinterInfo, pinfo.GetType());
            pinfo.pDevMode            = iparg;
            pinfo.pSecurityDescriptor = 0;
            Marshal.StructureToPtr(pinfo, ptrPrinterInfo, true);
            nRet = SetPrinter(hPrinter, 9, ptrPrinterInfo, 0) ? 1 : 0;
            if ((nRet == 0))
            {
                errorMessage = "Unable to set shared printer settings.";
            }
            functionReturnValue = Convert.ToBoolean(nRet);
cleanup:
            if ((hPrinter.ToInt32() != 0))
            {
                ClosePrinter(hPrinter);
            }
            return(functionReturnValue);
        }