查找局域网的打印机2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Drawing.Printing;
using System.Collections;
using DevComponents.Editors;
using System.Threading;

namespace Zipper
{
    public partial class Form1 : Office2007RibbonForm
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void printerCommand_Executed(object sender, EventArgs e)
        {
            this.comboBoxItem3.Enabled = false;

            ButtonItem buttonItem = sender as ButtonItem;
            
            if (buttonItem != null)
            {
                string cmdParameter = buttonItem.CommandParameter.ToString().ToLower();
                PrinterCommandParamter pcp = EnumHelper.Contains<PrinterCommandParamter>(cmdParameter);
                switch (pcp)
                {
                    case PrinterCommandParamter.None:
                        ShowError();
                        break;
                    case PrinterCommandParamter.Search:
                        backgroundWorker1.RunWorkerAsync();
                        break;
                    default:
                        break;
                }

            }
            else
            {
                MessageBox.Show("没有找到按钮", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        PrinterEnumFlags pef1 = PrinterEnumFlags.PRINTER_ENUM_DEFAULT;
        PrinterEnumFlags pef2 = PrinterEnumFlags.PRINTER_ENUM_DEFAULT;

        List<PRINTER_INFO_1> printers = null;
        private void SearchPrinter()
        {
            //PrinterEnumFlags pef = EnumHelper.Contains<PrinterEnumFlags>(((ComboItem)(this.comboBoxItem2.SelectedItem)).Tag.ToString()) | EnumHelper.Contains<PrinterEnumFlags>(((ComboItem)(this.comboBoxItem3.SelectedItem)).Tag.ToString());
            PrinterEnumFlags pef = pef1 | pef2;
            List<PRINTER_INFO_1> list = PrinterHelper.GetPrinters(pef);


            printers = list;

            //try
            //{
            //    while (true)
            //    {
            //        PrinterEnumFlags pef = EnumHelper.Contains<PrinterEnumFlags>(((ComboItem)(this.comboBoxItem2.SelectedItem)).Tag.ToString()) | EnumHelper.Contains<PrinterEnumFlags>(((ComboItem)(this.comboBoxItem3.SelectedItem)).Tag.ToString());
            //        List<PRINTER_INFO_1> list = PrinterHelper.GetPrinters(pef);
            //        ShowSearchPrintResult(list);
            //        Application.DoEvents();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
           

        }

     
        private void ShowError()
        {
            MessageBox.Show("操作参数有误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var query = EnumHelper.EnumToDescriptionList<PrinterEnumFlags>().ToArray<EnumDescription>();
            ComboItem ci = null;
            foreach (var item in query)
            {
                ci = new ComboItem { Text = item.Description, Tag = item.Name };
                this.comboBoxItem2.Items.Add(ci);
            }

        }
        
        private void comboBoxItem2_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<EnumDescription> list = EnumHelper.EnumToDescriptionList<PrinterEnumFlags>();

            var query = from item in list
                        where item.Name != ((ComboItem)(this.comboBoxItem2.SelectedItem)).Tag.ToString()
                        select new { item.Name,item.Description };
            ComboItem ci = null;
            this.comboBoxItem3.Items.Clear();
            foreach (var item in query)
            {
                ci = new ComboItem { Text = item.Description, Tag = item.Name };
                this.comboBoxItem3.Items.Add(ci);
            }
            pef1 = EnumHelper.Contains<PrinterEnumFlags>(((ComboItem)(this.comboBoxItem2.SelectedItem)).Tag.ToString());
            this.comboBoxItem3.Enabled = true;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SearchPrinter();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var query = from printer in printers
                        select new { printer.pName, printer.pComment, printer.pDescription };

            this.dataGridViewX1.DataSource = query.ToList();
        }

        private void comboBoxItem3_SelectedIndexChanged(object sender, EventArgs e)
        {
            pef2 = EnumHelper.Contains<PrinterEnumFlags>(((ComboItem)(this.comboBoxItem3.SelectedItem)).Tag.ToString());
        }

    }

    enum PrinterCommandParamter
    { 
        None,
        Search
    }
    //Alignment a = (Alignment)Enum.Parse(typeof(Alignment), name, false);
    //foreach(string name in Enum.GetNames(typeof(Day))) Console.WriteLine(name);   还有一个Enum.GetName()


}

点击查看原图

知识共享许可协议
《查找局域网的打印机2》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     152.94ms