回答を得るためにSOに来る前に、私は過去2〜3日間Googleでさまざまな質問フォームを試し、これを機能させるように努めてきました。
現在の印刷ジョブの色設定を取得して、ユーザーが実行したカラーまたはグレースケール印刷の数を確認する必要があります。ただし、(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
}
}