PrinterCombox.cs 1.5 KB

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