You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
10 KiB

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