Advertisement
Fhernd

Principal.cs

Mar 22nd, 2018
1,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Management;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace R818GestionColaImpresion
  8. {
  9.     public partial class Principal : Form
  10.     {
  11.         public Principal()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.  
  16.         private void btnRefrescar_Click(object sender, EventArgs e)
  17.         {
  18.             string consulta = "SELECT * FROM Win32_PrintJob";
  19.  
  20.             using (ManagementObjectSearcher consultaTareas = new ManagementObjectSearcher(consulta))
  21.             {
  22.                 using (ManagementObjectCollection tareas = consultaTareas.Get())
  23.                 {
  24.                     lbxDocumentos.Items.Clear();
  25.                     txtNombreImpresion.Text = "";
  26.  
  27.                     foreach (ManagementObject tarea in tareas)
  28.                     {
  29.                         lbxDocumentos.Items.Add(tarea["JobID"]);
  30.                     }
  31.                 }
  32.             }
  33.            
  34.         }
  35.  
  36.         private void Principal_Load(object sender, EventArgs e)
  37.         {
  38.             btnRefrescar_Click(null, null);
  39.         }
  40.  
  41.         private ManagementObject GetDocumentoSeleccionado()
  42.         {
  43.             try
  44.             {
  45.                 string consulta = "SELECT * FROM Win32_PrintJob WHERE JobID = '" + lbxDocumentos.Text + "'";
  46.  
  47.                 ManagementObject tarea = null;
  48.  
  49.                 using (ManagementObjectSearcher consultaTarea = new ManagementObjectSearcher(consulta))
  50.                 {
  51.                     ManagementObjectCollection tareas = consultaTarea.Get();
  52.                     IEnumerator enumerador = tareas.GetEnumerator();
  53.                     enumerador.MoveNext();
  54.                     tarea = (ManagementObject) enumerador.Current;
  55.                 }
  56.  
  57.                 return tarea;
  58.             }
  59.             catch (InvalidOperationException e)
  60.             {
  61.                 return null;
  62.             }
  63.         }
  64.  
  65.         private void lbxDocumentos_SelectedIndexChanged(object sender, EventArgs e)
  66.         {
  67.             ManagementObject documento = GetDocumentoSeleccionado();
  68.  
  69.             if (documento == null)
  70.             {
  71.                 txtNombreImpresion.Text = string.Empty;
  72.                 return;
  73.             }
  74.  
  75.             StringBuilder infoDocumento = new StringBuilder();
  76.             infoDocumento.AppendFormat("Documento: {0}", documento["Document"].ToString());
  77.             infoDocumento.Append(Environment.NewLine);
  78.             infoDocumento.AppendFormat("Nombre driver: {0}", documento["DriverName"].ToString());
  79.             infoDocumento.Append(Environment.NewLine);
  80.             infoDocumento.AppendFormat("Estado: {0}", documento["Status"].ToString());
  81.             infoDocumento.Append(Environment.NewLine);
  82.             infoDocumento.AppendFormat("Propietario: {0}", documento["Owner"].ToString());
  83.             infoDocumento.Append(Environment.NewLine);
  84.  
  85.             infoDocumento.AppendFormat("Páginas impresas: {0}", documento["PagesPrinted"].ToString());
  86.             infoDocumento.Append(Environment.NewLine);
  87.             infoDocumento.AppendFormat("Páginas totales: {0}", documento["TotalPages"].ToString());
  88.  
  89.             if (documento["JobStatus"] != null)
  90.             {
  91.                 txtNombreImpresion.Text += Environment.NewLine;
  92.                 txtNombreImpresion.Text += "Estado cumento: " + documento["JobStatus"].ToString();
  93.             }
  94.  
  95.             if (documento["StartTime"] != null)
  96.             {
  97.                 infoDocumento.Append(Environment.NewLine);
  98.                 infoDocumento.AppendFormat("Inicio: {0}", documento["StartTime"].ToString());
  99.             }
  100.  
  101.             txtNombreImpresion.Text = infoDocumento.ToString();
  102.         }
  103.  
  104.         private void btnPausar_Click(object sender, EventArgs e)
  105.         {
  106.             if (lbxDocumentos.SelectedIndex == -1)
  107.             {
  108.                 return;
  109.             }
  110.  
  111.             ManagementObject documento = GetDocumentoSeleccionado();
  112.  
  113.             if (documento == null)
  114.             {
  115.                 return;
  116.             }
  117.  
  118.             int valor = Int32.Parse(documento.InvokeMethod("Pause", null).ToString());
  119.  
  120.             if (valor == 0)
  121.             {
  122.                 MessageBox.Show("Se detuvo la impresión del documento.");
  123.             }
  124.             else
  125.             {
  126.                 MessageBox.Show("No se reconoció el código para la pausa.");
  127.             }
  128.         }
  129.  
  130.         private void btnReanudar_Click(object sender, EventArgs e)
  131.         {
  132.             if (lbxDocumentos.SelectedIndex == -1)
  133.             {
  134.                 return;
  135.             }
  136.  
  137.             ManagementObject documento = GetDocumentoSeleccionado();
  138.  
  139.             if (documento == null)
  140.             {
  141.                 return;
  142.             }
  143.  
  144.             if ((Int32.Parse(documento["StatusMask"].ToString()) & 1) == 1)
  145.             {
  146.                 int valor = Int32.Parse(documento.InvokeMethod("Resume", null).ToString());
  147.  
  148.                 if (valor == 0)
  149.                 {
  150.                     MessageBox.Show("Continuando con la impresión del documento.");
  151.                 }
  152.                 if (valor == 5)
  153.                 {
  154.                     MessageBox.Show("No se puede reanudar.");
  155.                 }
  156.                 else
  157.                 {
  158.                     MessageBox.Show("No se puede continuar con la reanudación.");
  159.                 }
  160.             }
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement