使用C#确定当前打印作业的颜色 [英] Determine current print job color using C#

查看:199
本文介绍了使用C#确定当前打印作业的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来SO一个答案,我花了近2天或3天在谷歌尝试不同的问题形式的尝试,并得到这个工作之前。

Before coming to SO for an answer I've spent the last 2 or 3 days on Google trying different question forms to try and get this to work.

我需要获得当前打印作业的颜色设置,以确定有多少色彩或灰度打印用户已执行。但是每一个颜色属性我试图访问(通过ManagementObjectSearcher,守望者,以及内置的C#打印机类)总是返回颜色,永不灰度。

I need to get the color setting of the current print job to determine how many color or grayscale prints a user has performed. However every single color property I've tried to access (through ManagementObjectSearcher, ""Watcher, and the built-in printer classes of C#) always return color, never grayscale.

任何帮助将不胜感激,因为我已经停下来制作上的解决方案取得进展。谢谢。下面是代码我已经(记住这是我的原型代码,所以有可能是除了我要问,你的回答我的问题,请只提供建议,许多问题)。

Any help would be greatly appreciated, since I've come to a stop on making progress with solutions. Thanks. Below is the code I have (keep in mind it's my prototyping code so there might be many issues apart from what I'm asking. Please only provide suggestions WITH your answer to my question).

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
}

}

推荐答案

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

I spent a lot of time to weigh out how to get the real value of the defined color printing and discovered that Win32_PrintJob provides this information in this way:

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;
}

这篇关于使用C#确定当前打印作业的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆