我有一个关于打印机的问题,如何能够获得打印机的相关属性,如打印机的状态,类型,位置?急!

解决方案 »

  1.   

    调API 好像是GetDC吧,自己查一下
      

  2.   

    public class PrintingExample : System.Windows.Forms.Form 
    {
        private System.ComponentModel.Container components;
        private System.Windows.Forms.Button printButton;
        private Font printFont;
        private StreamReader streamToPrint;   public PrintingExample() : base() 
       {
          // The Windows Forms Designer requires the following call.
          InitializeComponent();
       }   // The Click event is raised when the user clicks the Print button.
       private void printButton_Click(object sender, EventArgs e) 
       {
          try 
          {
              streamToPrint = new StreamReader
                 ("C:\\My Documents\\MyFile.txt");
              try 
              {
                 printFont = new Font("Arial", 10);
                 PrintDocument pd = new PrintDocument();
                 pd.PrintPage += new PrintPageEventHandler
                    (this.pd_PrintPage);
                 pd.Print();
              }  
              finally 
              {
                 streamToPrint.Close();
              }
          }  
          catch(Exception ex) 
          {
              MessageBox.Show(ex.Message);
          }
       }   // The PrintPage event is raised for each page to be printed.
       private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
       {
          float linesPerPage = 0;
          float yPos = 0;
          int count = 0;
          float leftMargin = ev.MarginBounds.Left;
          float topMargin = ev.MarginBounds.Top;
          string line = null;      // Calculate the number of lines per page.
          linesPerPage = ev.MarginBounds.Height / 
             printFont.GetHeight(ev.Graphics);      // Print each line of the file.
          while(count < linesPerPage && 
             ((line=streamToPrint.ReadLine()) != null)) 
          {
             yPos = topMargin + (count * 
                printFont.GetHeight(ev.Graphics));
             ev.Graphics.DrawString(line, printFont, Brushes.Black, 
                leftMargin, yPos, new StringFormat());
             count++;
          }      // If more lines exist, print another page.
          if(line != null)
             ev.HasMorePages = true;
          else
             ev.HasMorePages = false;
       }
       // The Windows Forms Designer requires the following procedure.
       private void InitializeComponent() 
       {
          this.components = new System.ComponentModel.Container();
          this.printButton = new System.Windows.Forms.Button();      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
          this.ClientSize = new System.Drawing.Size(504, 381);
          this.Text = "Print Example";      printButton.ImageAlign = 
             System.Drawing.ContentAlignment.MiddleLeft;
          printButton.Location = new System.Drawing.Point(32, 110);
          printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
          printButton.TabIndex = 0;
          printButton.Text = "Print the file.";
          printButton.Size = new System.Drawing.Size(136, 40);
          printButton.Click += new System.EventHandler(printButton_Click);      this.Controls.Add(printButton);
       }   // This is the main entry point for the application.
       public static void Main(string[] args) 
       {
          Application.Run(new PrintingExample());
       }
    }
      

  3.   

    从Windows 2000开始,Windows操作系统内建了WMI管理对象。通过它,我们可以获取系统中各个组件的情况。对于打印机也一样,查询WMI的Win32_Printer对象即可获取到打印机相关的状态信息,请看如下的示例代码:
    //---------------------------------------------------------
    string searchQuery = "SELECT * FROM Win32_Printer";
    ManagementObjectSearcher searchPrinters = 
    new ManagementObjectSearcher(searchQuery);
    ManagementObjectCollection printerCollection = searchPrinters.Get();
    ManagementObject currentPrinter = null;
    foreach(ManagementObject printer in printerCollection)
    {
    Console.WriteLine( printer.Properties["PrinterStatus"].Value.ToString() ); 
    }
      

  4.   

    redbb(....DB & DEV....抵制日货,人人有责...)
    高,支持
      

  5.   

    以上代码会把操作系统上所有的打印机状态信息都打印出来,只要参考如下的文档,即可知道PrinterStatus各个数值代表的状态意思:
    http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_printer.asp
      

  6.   

    楼上的高手
    我机器上连接了5台打印机
    全都是用的打印服务器
    也就说每一台打印机对应一台打印服务器
    用你给的MSDN里的方法能不能获得每一台打印机的状态???
    我现在机器上只有一台打印机,还是用的USB端口,开着打印机与关着打印机的时候
    各种方法返回的值都是一样的啊~
    为什么会这样???