0

在来 SO 寻求答案之前,我在 Google 上花了 2 或 3 天时间尝试不同的问题形式来尝试让它发挥作用。

我需要获取当前打印作业的颜色设置,以确定用户执行了多少彩色或灰度打印。但是,我尝试访问的每个颜色属性(通过 ManagementObjectSearcher、“”Watcher 和 C# 的内置打印机类)总是返回颜色,而不是灰度。

任何帮助将不胜感激,因为我已经停止在解决方案方面取得进展。谢谢。下面是我的代码(请记住,这是我的原型代码,所以除了我要问的问题之外,可能还有很多问题。请仅在您对我的问题的回答中提供建议)。

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Printing;
using System.Management;
using System.Management.Instrumentation;
using System.Threading;
using System.Windows.Threading;

using System.Diagnostics;

namespace PrintPlus {
    public partial class PrintPlus : Form {

    #region Objects
    // Mgmt 
    ManagementEventWatcher watcher;
    ManagementObjectSearcher searcher;

    // Thread
    Thread jobCheck;

    // Printer Objects
    PrintQueue printQ;
    PrintJobInfoCollection printJobCollection;

    // Timer
    private System.Windows.Forms.Timer timeLogged;

    #endregion

    #region Paths And Names

    string localMachineName;

    #endregion

    #region Costs

    private decimal timeCost;
    private decimal printCost;

    #endregion

    #region Print Variables

    private int color;

    private bool jobIsProcessing;

    private int numberOfPrints;
    private int colorPrints;
    private int greyScalePrints;

    private int printJobCount;

    #endregion

    #region Time Variables

    private float tSecs;
    private float tMins;
    private float tHrs;

    #endregion

    #region Constructor

    public PrintPlus() {
        InitializeComponent();
        initObjects();

        /* searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PrintJob");

        watcher = new ManagementEventWatcher("SELECT * FROM  __InstanceCreationEvent WITHIN 0.01 WHERE TargetInstance ISA \"Win32_PrintJob\"");
        watcher.EventArrived += new EventArrivedEventHandler(getColorSetting);
        watcher.Start();

        localMachineName = Environment.MachineName;*/
    }

    #endregion

    #region Initializers

    private void initObjects() {
        initPrinterObjects();
        initTimer();
    }

    private void initPrinterObjects() {
        LocalPrintServer lps = new LocalPrintServer();
        printQ = new PrintQueue(lps, lps.DefaultPrintQueue.Name);
    }

    private void initTimer() {
        timeLogged = new System.Windows.Forms.Timer();
        timeLogged.Interval = 1000;
        timeLogged.Tick += new EventHandler(onTick);
        timeLogged.Start();
    }

    #endregion

    #region Delegates

    private void onTick(object sender, EventArgs e) {
        updateTime();
        updateInfo();
    }

    private void onMove(object sender, EventArgs e) {
        this.Location = initialPosition;
    }

    private void onLoseFocus(object sender, EventArgs e) {
        this.MinimizeBox = true;
    }

    #endregion

    #region Updates

    private void updateInfo() {
        printJobCount = printQ.GetPrintJobInfoCollection().Count<PrintSystemJobInfo>();

        if (printJobCount >= 1 && !jobIsProcessing) {
            jobIsProcessing = true;
            jobCheck = new Thread(new ThreadStart(processJobs));
            jobCheck.Start();
        }

        numberOfPrints = (colorPrints + greyScalePrints);

        timeCostLbl.Text = "Time: $" + timeCost.ToString();
        printCostLbl.Text = "Print: $" + printCost.ToString();
        totalCostLbl.Text = "Total: $" + (timeCost + printCost).ToString();
        printedPagesLbl.Text = "Printed Pages: " + numberOfPrints.ToString() + " Colour: " + colorPrints.ToString() + " B&W: " + greyScalePrints.ToString();
   }

    private void updateTime() {
        tSecs += timeLogged.Interval / 1000;

        if (tSecs == 60) {
            timeCost += FEES.COST_PER_MIN;
            tMins += 1;
            if (tMins == 60) {
                tHrs += 1;
            }
            tSecs = 0;
        }
        int i = 0;

        String hrs = ((tHrs >= 10) ? tHrs.ToString() : i + tHrs.ToString());
        String mins = ((tMins >= 10) ? tMins.ToString() : i + tMins.ToString());
        String secs = ((tSecs >= 10) ? tSecs.ToString() : i + tSecs.ToString());

        this.timeElapsedLbl.Text = "Time Logged: " + hrs + " : " + mins + " : " + secs;
    }

    public void processJobs() {
        LocalPrintServer lps = new LocalPrintServer();
        PrintQueue printQ = new PrintQueue(lps, lps.DefaultPrintQueue.Name);
        PrintJobInfoCollection printJobCollection = printQ.GetPrintJobInfoCollection();
        PrintSystemJobInfo[] jobArray = printJobCollection.ToArray<PrintSystemJobInfo>();

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PrintJob");
        ManagementObjectCollection searchCollection = searcher.Get();

        foreach (ManagementObject job in searchCollection) {
            foreach (PropertyData prop in job.Properties) {
                Debug.WriteLine(prop.Name + ": " + prop.Value);
            }
        }

        try {
            for (int i = 0; i < jobArray.Length; ++i) {
                if (jobArray[i].PositionInPrintQueue == 1) {
                    while (jobArray[i].JobStatus != PrintJobStatus.Deleted) {
                        jobArray[i].Refresh();
                    }
                }

                Debug.WriteLine(printQ.CurrentJobSettings.CurrentPrintTicket.OutputColor.Value);

                /*if (jobArray[i].PropertiesCollection. == "Color") {
                    colorPrints += jobArray[i].NumberOfPagesPrinted;
                }
                else if (jobArray[i].PropertiesCollection["Color"].ToString() == "Monochrome") {
                    greyScalePrints += jobArray[i].NumberOfPagesPrinted;
                }*/
            }
        }
        finally {
            jobIsProcessing = false;
            lps.Dispose();
            printQ.Dispose();
            printJobCollection.Dispose();
            jobCheck.Abort();
        }
    }

    private void getPrintWatcher(int jobID) {

    }

    private void getColorSetting(object sender, EventArrivedEventArgs e) {
       /* foreach (PropertyData data in e.NewEvent.Properties) {
            ManagementBaseObject mbo = data.Value as ManagementBaseObject;

            if (mbo.Properties["Color"].Value == "Color") {
                color = COLOR_VALUES.COLOR;
            }
            else if (mbo.Properties["Color"].Value == "Monochrome") {
                color = COLOR_VALUES.MONO;
            }
        }*/
    }

    #endregion
}

}

4

3 回答 3

0

我相信您遇到的问题是因为打印机能够打印彩色和黑白。当您通过彩色打印机打印黑色字母文本文档时,WIN32_PrintJob 仍然会说该文档是彩色文档,除非您通过本地机器的“打印机首选项”对话框明确告诉打印机以黑白方式打印。

于 2013-03-17T18:00:41.220 回答
0

每个打印机制造商和型号的颜色计数器的工作方式不同。您最好的选择是在处理打印之前,检查打印材料并确定彩色页面和黑色页面。如果没有,您将不得不中继仅适用于多功能打印机的机器计数器,您可以通过 SNMP 获取此信息。

于 2015-05-17T02:19:03.560 回答
0

我花了很多时间权衡如何获得定义的彩色打印的真正价值,发现 Win32_PrintJob 以这种方式提供了这些信息:

public static bool PausePrintJob(string printerName, int printJobID)
{
    bool isActionPerformed = false;
    string searchQuery = "SELECT * FROM Win32_PrintJob";
    ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(searchQuery);
    ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();

    foreach (ManagementObject prntJob in prntJobCollection)
    {
        System.String jobName = prntJob.Properties["Name"].Value.ToString();

        //Job name would be of the format [Printer name], [Job ID]
        char[] splitArr = new char[1];
        splitArr[0] = Convert.ToChar(",");
        string prnterName = jobName.Split(splitArr)[0];
        int prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
        string documentName = prntJob.Properties["Document"].Value.ToString();

        if (String.Compare(prnterName, printerName, true) == 0)
        {
            if (prntJobID == printJobID)
            {
                // MessageBox.Show("PAGINAS : " + prntJob.Properties["TotalPages"].Value.ToString() + documentName + " " + prntJobID);
                prntJob.InvokeMethod("Pause", null);
                MessageBox.Show(prntJob.Properties["Color"].Value.ToString());

                //prntJob.InvokeMethod("Resume", null);
                isActionPerformed = true;
                break;
            }
        }
    }
    return isActionPerformed;
}
于 2015-06-05T18:00:28.127 回答