PrinterCombox.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing.Printing;
  4. namespace UAS_LabelMachine.CustomControl
  5. {
  6. public partial class PrinterCombox : UserControl
  7. {
  8. public PrinterCombox()
  9. {
  10. InitializeComponent();
  11. }
  12. public delegate void OnSelectIndexChange(object sender, EventArgs e);
  13. public event OnSelectIndexChange UserOnSelectIndexChange;
  14. public override string Text
  15. {
  16. get
  17. {
  18. return PrinterList.Text;
  19. }
  20. set
  21. {
  22. PrinterList.Text = value;
  23. }
  24. }
  25. bool Loading = true;
  26. private void PrinterCombox_Load(object sender, EventArgs e)
  27. {
  28. PrintDocument print = new PrintDocument();
  29. string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
  30. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  31. {
  32. PrinterList.Items.Add(sPrint);
  33. if (sPrint == sDefault)
  34. PrinterList.SelectedIndex = PrinterList.Items.IndexOf(sPrint);
  35. }
  36. Loading = false;
  37. }
  38. private void PrinterList_SelectedIndexChanged(object sender, EventArgs e)
  39. {
  40. if (!Loading)
  41. UserOnSelectIndexChange.Invoke(sender, e);
  42. }
  43. }
  44. }