Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

317 wiersze
11 KiB

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