Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created March 23, 2018 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/ad9b00543812736f141fec2f2974787e to your computer and use it in GitHub Desktop.
Save Fhernd/ad9b00543812736f141fec2f2974787e to your computer and use it in GitHub Desktop.
Gestión de cola de impresión. OrtizOL.
using System;
using System.Collections;
using System.Management;
using System.Text;
using System.Windows.Forms;
namespace R818GestionColaImpresion
{
public partial class Principal : Form
{
public Principal()
{
InitializeComponent();
}
private void btnRefrescar_Click(object sender, EventArgs e)
{
string consulta = "SELECT * FROM Win32_PrintJob";
using (ManagementObjectSearcher consultaTareas = new ManagementObjectSearcher(consulta))
{
using (ManagementObjectCollection tareas = consultaTareas.Get())
{
lbxDocumentos.Items.Clear();
txtNombreImpresion.Text = "";
foreach (ManagementObject tarea in tareas)
{
lbxDocumentos.Items.Add(tarea["JobID"]);
}
}
}
}
private void Principal_Load(object sender, EventArgs e)
{
btnRefrescar_Click(null, null);
}
private ManagementObject GetDocumentoSeleccionado()
{
try
{
string consulta = "SELECT * FROM Win32_PrintJob WHERE JobID = '" + lbxDocumentos.Text + "'";
ManagementObject tarea = null;
using (ManagementObjectSearcher consultaTarea = new ManagementObjectSearcher(consulta))
{
ManagementObjectCollection tareas = consultaTarea.Get();
IEnumerator enumerador = tareas.GetEnumerator();
enumerador.MoveNext();
tarea = (ManagementObject) enumerador.Current;
}
return tarea;
}
catch (InvalidOperationException e)
{
return null;
}
}
private void lbxDocumentos_SelectedIndexChanged(object sender, EventArgs e)
{
ManagementObject documento = GetDocumentoSeleccionado();
if (documento == null)
{
txtNombreImpresion.Text = string.Empty;
return;
}
StringBuilder infoDocumento = new StringBuilder();
infoDocumento.AppendFormat("Documento: {0}", documento["Document"].ToString());
infoDocumento.Append(Environment.NewLine);
infoDocumento.AppendFormat("Nombre driver: {0}", documento["DriverName"].ToString());
infoDocumento.Append(Environment.NewLine);
infoDocumento.AppendFormat("Estado: {0}", documento["Status"].ToString());
infoDocumento.Append(Environment.NewLine);
infoDocumento.AppendFormat("Propietario: {0}", documento["Owner"].ToString());
infoDocumento.Append(Environment.NewLine);
infoDocumento.AppendFormat("Páginas impresas: {0}", documento["PagesPrinted"].ToString());
infoDocumento.Append(Environment.NewLine);
infoDocumento.AppendFormat("Páginas totales: {0}", documento["TotalPages"].ToString());
if (documento["JobStatus"] != null)
{
txtNombreImpresion.Text += Environment.NewLine;
txtNombreImpresion.Text += "Estado cumento: " + documento["JobStatus"].ToString();
}
if (documento["StartTime"] != null)
{
infoDocumento.Append(Environment.NewLine);
infoDocumento.AppendFormat("Inicio: {0}", documento["StartTime"].ToString());
}
txtNombreImpresion.Text = infoDocumento.ToString();
}
private void btnPausar_Click(object sender, EventArgs e)
{
if (lbxDocumentos.SelectedIndex == -1)
{
return;
}
ManagementObject documento = GetDocumentoSeleccionado();
if (documento == null)
{
return;
}
int valor = Int32.Parse(documento.InvokeMethod("Pause", null).ToString());
if (valor == 0)
{
MessageBox.Show("Se detuvo la impresión del documento.");
}
else
{
MessageBox.Show("No se reconoció el código para la pausa.");
}
}
private void btnReanudar_Click(object sender, EventArgs e)
{
if (lbxDocumentos.SelectedIndex == -1)
{
return;
}
ManagementObject documento = GetDocumentoSeleccionado();
if (documento == null)
{
return;
}
if ((Int32.Parse(documento["StatusMask"].ToString()) & 1) == 1)
{
int valor = Int32.Parse(documento.InvokeMethod("Resume", null).ToString());
if (valor == 0)
{
MessageBox.Show("Continuando con la impresión del documento.");
}
if (valor == 5)
{
MessageBox.Show("No se puede reanudar.");
}
else
{
MessageBox.Show("No se puede continuar con la reanudación.");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment