Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

293 строки
10 KiB

  1. using HKCardManager.UserPages;
  2. using UHFHelper;
  3. namespace HKCardManager
  4. {
  5. public partial class Form1 : Form
  6. {
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. splitContainer1.SplitterDistance = 240;
  11. DataGridViewInit();
  12. ShowPage(new PersonnelEntryPage());
  13. Task.Factory.StartNew(() =>
  14. {
  15. var res = UHFCardHelper.GetInstance().OpenPort();
  16. if (res != null && res.Res)
  17. MessageLogNotify.GetInstance.Show("制卡设备已连接!");
  18. else MessageLogNotify.GetInstance.ShowError($"制卡设备未连接!");
  19. });
  20. this.SizeChanged += Form1_SizeChanged;
  21. this.button1.SizeChanged += Button1_SizeChanged;
  22. this.splitContainer2.SplitterMoved += SplitContainer1_SizeChanged;
  23. MessageLogNotify.GetInstance.Info = new Action<string, Color>((s, br) =>
  24. {
  25. this.Invoke(() =>
  26. {
  27. dataGridView1.Rows.Insert(0, new string[] { "", $"{DateTime.Now.ToString("HH:mm:ss")} {s}" });
  28. dataGridView1.Rows[0].DefaultCellStyle.ForeColor = br;
  29. dataGridView1.Rows[0].DefaultCellStyle.Padding = new Padding(0, 0, 0, 10);
  30. });
  31. });
  32. }
  33. private void SplitContainer1_SizeChanged(object? sender, EventArgs e)
  34. {
  35. if (tempUserControl != null)
  36. {
  37. tempUserControl.Width = this.panel1.Width;
  38. tempUserControl.Height = this.panel1.Height;
  39. }
  40. }
  41. private void DataGridViewInit()
  42. {
  43. dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
  44. dataGridView1.AutoGenerateColumns = false;//禁止自动添加列
  45. dataGridView1.AllowUserToResizeColumns = false;//是否可以调整列的大小
  46. dataGridView1.AllowUserToResizeRows = false;//是否可以调整行的大小
  47. dataGridView1.BorderStyle = BorderStyle.None;//DataGridView边框样式
  48. dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
  49. dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
  50. dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
  51. dataGridView1.SelectionChanged += DataGridView1_SelectionChanged;
  52. }
  53. private void DataGridView1_SelectionChanged(object? sender, EventArgs e)
  54. {
  55. dataGridView1.ClearSelection();//禁止选择行
  56. }
  57. private void Write(string msg)
  58. {
  59. //bool条件默认为配置文件里面定义,可以自定义任何保存数据条件
  60. //bool temp = Convert.ToBoolean(iniFile.IniReadValue("设置", "保存运行信息", AppDomain.CurrentDomain.BaseDirectory + "设置.ini"));
  61. //if (temp)
  62. //{
  63. // string logPath = Path.GetDirectoryName(Application.ExecutablePath);
  64. // // System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "/" + DateTime.Now.ToString("yyMMdd") + ".txt");
  65. // System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "\\log\\" + DateTime.Now.ToString("yyMMdd") + ".txt");
  66. // sw.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + msg);
  67. // sw.Close();
  68. // sw.Dispose();
  69. //}
  70. }
  71. private void AddListToListBox(ListBox rtbox, string text, Brush foreColor, bool addTime = true)
  72. {
  73. rtbox.DrawMode = DrawMode.OwnerDrawVariable;
  74. rtbox.Font = new Font("楷体", 12);
  75. if (rtbox.ItemHeight != 24)
  76. {
  77. rtbox.ItemHeight = 1;
  78. rtbox.DrawItem += ((sender, e) => //控件重绘事件
  79. {
  80. if (e.Index == -1) return;
  81. e.DrawBackground();
  82. ListBoxItem item = (ListBoxItem)rtbox.Items[e.Index];
  83. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  84. {
  85. e.DrawFocusRectangle();
  86. Brush b = Brushes.Khaki;//设置选中项颜色
  87. e.Graphics.FillRectangle(b, e.Bounds);
  88. }
  89. if (item != null)
  90. {
  91. Brush brush = item.ForeColor;
  92. Rectangle r = e.Bounds;
  93. r.Height = e.Bounds.Height;
  94. r.Width = e.Bounds.Width + 100;
  95. e.Graphics.DrawString(item.Text, e.Font, brush, r, StringFormat.GenericDefault);
  96. }
  97. else
  98. {
  99. e.Graphics.DrawString(item.Text, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
  100. }
  101. });
  102. }
  103. rtbox.ItemHeight = 25;//设置单行高度
  104. if (rtbox.InvokeRequired)
  105. {
  106. rtbox.Invoke(new Action<ListBox, string, Brush, bool>((rtb, str, color, addtime) => AddListToListBox(rtb, str, color, addtime)), rtbox, text, foreColor, addTime);
  107. return;
  108. }
  109. //text = string.Format("{0}-{1}", rtbox.Items.Count.ToString("000"), text);
  110. if (addTime)
  111. {
  112. //text = string.Format("[{0}]:{1}", DateTime.Now.ToString("HH:mm:ss"), text);
  113. text = $"{DateTime.Now.ToString("HH:mm:ss")}:{text}";
  114. }
  115. if (rtbox.Items.Count > 0)
  116. rtbox.Items.Insert(0, new ListBoxItem(text, foreColor));
  117. else
  118. rtbox.Items.Add(new ListBoxItem(text, foreColor));
  119. try
  120. {
  121. Write(text);
  122. }
  123. catch { }
  124. //清除过多的数据
  125. if (rtbox.Items.Count > 100)
  126. {
  127. int count = rtbox.Items.Count - 100;
  128. List<object> temp = new List<object>();
  129. for (int i = 100; i < rtbox.Items.Count; i++)
  130. {
  131. if (rtbox.Items.Count >= i - 1) temp.Add(rtbox.Items[i]);
  132. }
  133. foreach (object obj in temp)
  134. {
  135. rtbox.Items.Remove(obj);
  136. }
  137. }
  138. //rtbox.SelectedIndex = rtbox.Items.Count - 1;
  139. //if (rtbox.Items.Count > 100)
  140. //{
  141. // int count = rtbox.Items.Count - 100;
  142. // List<object> temp = new List<object>();
  143. // for (int i = 0; i < count; i++)
  144. // {
  145. // temp.Add(rtbox.Items[i]);
  146. // }
  147. // foreach (object obj in temp)
  148. // {
  149. // rtbox.Items.Remove(obj);
  150. // }
  151. //}
  152. //rtbox.SelectedIndex = rtbox.Items.Count - 1;
  153. }
  154. private void Button1_SizeChanged(object? sender, EventArgs e)
  155. {
  156. groupBox1.Width = button1.Width;
  157. groupBox2.Width = button1.Width;
  158. groupBox3.Width = button1.Width;
  159. groupBox4.Width = button1.Width;
  160. button2.Left = 10;
  161. button2.Width = (groupBox1.Width - 20);
  162. button3.Left = 10;
  163. button3.Width = (groupBox1.Width - 20);
  164. button4.Left = 10;
  165. button4.Width = (groupBox2.Width - 20);
  166. button5.Left = 10;
  167. button5.Width = (groupBox2.Width - 20);
  168. button6.Left = 10;
  169. button6.Width = (groupBox2.Width - 20);
  170. button7.Left = 10;
  171. button7.Width = (groupBox2.Width - 20);
  172. button9.Left = 10;
  173. button9.Width = (groupBox3.Width - 20);
  174. button10.Left = 10;
  175. button10.Width = (groupBox3.Width - 20);
  176. button11.Left = 10;
  177. button11.Width = (groupBox3.Width - 20);
  178. button12.Left = 10;
  179. button12.Width = (groupBox4.Width - 20);
  180. }
  181. UserControl tempUserControl;
  182. private void Form1_SizeChanged(object? sender, EventArgs e)
  183. {
  184. if (tempUserControl != null)
  185. {
  186. tempUserControl.Width = this.panel1.Width;
  187. tempUserControl.Height = this.panel1.Height;
  188. }
  189. splitContainer1.SplitterDistance = 240;
  190. if (this.WindowState == FormWindowState.Maximized || this.WindowState == FormWindowState.Normal)
  191. {
  192. Task.Factory.StartNew(() =>
  193. {
  194. Thread.Sleep(100);
  195. this.Invoke(() =>
  196. {
  197. if (tempUserControl != null)
  198. {
  199. tempUserControl.Width = this.panel1.Width;
  200. tempUserControl.Height = this.panel1.Height;
  201. }
  202. splitContainer1.SplitterDistance = 240;
  203. });
  204. });
  205. }
  206. }
  207. //人员录入
  208. private void button2_Click(object sender, EventArgs e)
  209. {
  210. ShowPage(new PersonnelEntryPage());
  211. }
  212. //机构录入
  213. private void button3_Click(object sender, EventArgs e)
  214. {
  215. ShowPage(new InstitutionalEntryPage());
  216. }
  217. //发卡
  218. private void button5_Click(object sender, EventArgs e)
  219. {
  220. ShowPage(new CarMangerPage());
  221. }
  222. //销户,挂失,解挂
  223. private void button4_Click(object sender, EventArgs e)
  224. {
  225. ShowPage(new CancellationPage(), ((Button)sender)?.Tag.ToString());
  226. }
  227. //月餐段汇总表
  228. private void button11_Click(object sender, EventArgs e)
  229. {
  230. ShowPage(new ReportFormPage(), ((Button)sender)?.Tag.ToString());
  231. }
  232. //餐段设置
  233. private void button12_Click(object sender, EventArgs e)
  234. {
  235. ShowPage(new MealSegmentSetPage());
  236. }
  237. private void ShowPage(UserControl userControl, string? name = "")
  238. {
  239. panel1.Controls.Clear();
  240. userControl.Width = panel1.Width;
  241. userControl.Height = panel1.Height;
  242. tempUserControl = userControl;
  243. userControl.Tag = name;
  244. userControl.Show();
  245. panel1.Controls.Add(userControl);
  246. }
  247. private void button8_Click(object sender, EventArgs e)
  248. {
  249. ShowPage(new AdvertisingSetPage());
  250. }
  251. }
  252. }