frmMain.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. using lyfzAttendance.Controller;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace lyfzAttendance
  13. {
  14. public partial class frmMain : LYFZ.ComponentLibrary.BaseContentsFormMain
  15. {
  16. private int nTimes = 0;
  17. private object objlock = new object();
  18. private string AdminPassword { get; set; } = "";
  19. List<Model.UserInfo> listUser = new List<Model.UserInfo>();
  20. System.Windows.Forms.Timer timerAutoUpload = new System.Windows.Forms.Timer();
  21. static LogHelper log = new LogHelper(typeof(frmMain));
  22. public frmMain()
  23. {
  24. InitializeComponent();
  25. // 托盘必须禁用关闭窗口;
  26. this.IsCloseForm = false;
  27. this.btnAppFormExit.Click += BtnAppFormExit_Click1;
  28. // 按钮事件;
  29. this.btnAppFormMenu.Click += BtnAppFormMenu_Click;
  30. this.btnAppFormExit.Click += BtnAppFormExit_Click;
  31. this.btn_Connect.Click += Btn_Connect_Click;
  32. this.btn_DisConnect.Click += Btn_DisConnect_Click;
  33. this.btn_clear_all_log.Click += Btn_clear_all_log_Click;
  34. this.btn_upload_log.Click += Btn_upload_log_Click;
  35. this.btn_get_all_log.Click += Btn_get_all_log_Click;
  36. this.btn_InitMachine.Click += Btn_InitMachine_Click;
  37. this.btn_Import.Click += Btn_Import_Click;
  38. this.btn_Export.Click += Btn_Export_Click;
  39. this.btn_BenumbAllManager.Click += Btn_BenumbAllManager_Click;
  40. this.cb_autoupload.CheckedChanged += Cb_autoupload_CheckedChanged;
  41. // 定时器;
  42. timerAutoUpload.Interval = 15000;
  43. timerAutoUpload.Enabled = false;
  44. timerAutoUpload.Tick += TimerAutoUpload_Tick;
  45. // 设置按钮tooltip提示;
  46. SetButtonToolTip();
  47. if (IsAutoRun())
  48. this.toolStripMenuItemAuto.Checked = true;
  49. }
  50. /// <summary>
  51. /// 是否开机启动;
  52. /// </summary>
  53. /// <returns></returns>
  54. public bool IsAutoRun()
  55. {
  56. try
  57. {
  58. bool run = false;
  59. string path = Application.ExecutablePath;
  60. Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine;
  61. Microsoft.Win32.RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  62. string[] ValueNames = rk2.GetValueNames();
  63. foreach (string valueName in ValueNames)
  64. {
  65. if (valueName == "lyfzAttendanceEx")
  66. {
  67. run = true;
  68. break;
  69. }
  70. }
  71. return run;
  72. }
  73. catch { }
  74. return false;
  75. }
  76. /// <summary>
  77. /// 设置开机启动;
  78. /// </summary>
  79. /// <param name="run"></param>
  80. public void SetAutoRun(bool run = true)
  81. {
  82. try
  83. {
  84. if (run) //设置开机自启动
  85. {
  86. string path = Application.ExecutablePath;
  87. Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine;
  88. Microsoft.Win32.RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  89. rk2.SetValue("lyfzAttendanceEx", path);
  90. rk2.Close();
  91. rk.Close();
  92. }
  93. else //取消开机自启动
  94. {
  95. string path = Application.ExecutablePath;
  96. Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine;
  97. Microsoft.Win32.RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  98. rk2.DeleteValue("lyfzAttendanceEx", false);
  99. rk2.Close();
  100. rk.Close();
  101. }
  102. }
  103. catch { }
  104. }
  105. private void SetButtonToolTip()
  106. {
  107. #region btn_Connect按钮;
  108. ToolTip tip_Connect = new ToolTip();
  109. tip_Connect.AutoPopDelay = 5000;
  110. tip_Connect.InitialDelay = 100;
  111. tip_Connect.ShowAlways = true;
  112. tip_Connect.ReshowDelay = 100;
  113. tip_Connect.SetToolTip(this.btn_Connect, "连接指纹机设备.");
  114. #endregion
  115. #region btn_DisConnect 按钮;
  116. ToolTip tip_DisConnect = new ToolTip();
  117. tip_DisConnect.AutoPopDelay = 5000;
  118. tip_DisConnect.InitialDelay = 100;
  119. tip_DisConnect.ShowAlways = true;
  120. tip_DisConnect.ReshowDelay = 100;
  121. tip_DisConnect.SetToolTip(this.btn_DisConnect, "断开指纹机设备连接.");
  122. #endregion
  123. #region btn_clear_all_log 按钮;
  124. ToolTip tip_ClearAll = new ToolTip();
  125. tip_ClearAll.AutoPopDelay = 5000;
  126. tip_ClearAll.InitialDelay = 100;
  127. tip_ClearAll.ShowAlways = true;
  128. tip_ClearAll.ReshowDelay = 100;
  129. tip_ClearAll.SetToolTip(this.btn_clear_all_log, "清空指纹机所有的进出记录.");
  130. #endregion
  131. #region btn_InitMachine 按钮;
  132. ToolTip tip_InitMachine = new ToolTip();
  133. tip_InitMachine.AutoPopDelay = 5000;
  134. tip_InitMachine.InitialDelay = 100;
  135. tip_InitMachine.ShowAlways = true;
  136. tip_InitMachine.ReshowDelay = 100;
  137. tip_InitMachine.SetToolTip(this.btn_InitMachine, "指纹机初始化,恢复出厂设置.");
  138. #endregion
  139. #region btn_Import 按钮;
  140. ToolTip tip_Import = new ToolTip();
  141. tip_Import.AutoPopDelay = 5000;
  142. tip_Import.InitialDelay = 100;
  143. tip_Import.ShowAlways = true;
  144. tip_Import.ReshowDelay = 100;
  145. tip_Import.SetToolTip(this.btn_Import, "导入备份好的指纹机进出记录Json文件到列表中.");
  146. #endregion
  147. #region btn_Export 按钮;
  148. ToolTip tip_Export = new ToolTip();
  149. tip_Export.AutoPopDelay = 5000;
  150. tip_Export.InitialDelay = 100;
  151. tip_Export.ShowAlways = true;
  152. tip_Export.ReshowDelay = 100;
  153. tip_Export.SetToolTip(this.btn_Export, "将列表中的指纹机记录备份到Json文件中.");
  154. #endregion
  155. #region btn_BenumbAllManager 按钮;
  156. ToolTip tip_BenumbAllManager = new ToolTip();
  157. tip_BenumbAllManager.AutoPopDelay = 5000;
  158. tip_BenumbAllManager.InitialDelay = 100;
  159. tip_BenumbAllManager.ShowAlways = true;
  160. tip_BenumbAllManager.ReshowDelay = 100;
  161. tip_BenumbAllManager.SetToolTip(this.btn_BenumbAllManager, "清除指纹机所有管理员权限.");
  162. #endregion
  163. }
  164. /// <summary>
  165. /// 去除指纹机所有管理员权限;
  166. /// </summary>
  167. /// <param name="sender"></param>
  168. /// <param name="e"></param>
  169. private void Btn_BenumbAllManager_Click(object sender, EventArgs e)
  170. {
  171. frmAuthority dlg = new frmAuthority(AdminPassword);
  172. if (dlg.ShowDialog() != DialogResult.OK)
  173. return;
  174. if (ClientProcess.Attendance.IsConnected)
  175. {
  176. if (ClientProcess.Attendance.BenumbAllManager())
  177. {
  178. MessageBox.Show("去除指纹机管理员权限成功……");
  179. }
  180. else
  181. {
  182. MessageBox.Show("去除指纹机管理员权限失败……");
  183. }
  184. }
  185. }
  186. private void BtnAppFormExit_Click1(object sender, EventArgs e)
  187. {
  188. this.btnAppFormMinimize_Click(sender, e);
  189. }
  190. private void Btn_Export_Click(object sender, EventArgs e)
  191. {
  192. List<lyfzAttendanceLib.GeneralLogData> list = new List<lyfzAttendanceLib.GeneralLogData>();
  193. foreach (DataGridViewRow dgr in this.dataGridView1.Rows)
  194. {
  195. list.Add((lyfzAttendanceLib.GeneralLogData)dgr.Tag);
  196. }
  197. string savePath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(System.Windows.Forms.Application.StartupPath) + "指纹记录备份";
  198. if (!Directory.Exists(savePath))
  199. {
  200. Directory.CreateDirectory(savePath);
  201. }
  202. try
  203. {
  204. string strJson = JsonConvert.SerializeObject(list);
  205. File.WriteAllText(string.Format("{0}\\{1}.json", savePath, DateTime.Now.ToString("yyyyMMddHHmmss")), strJson);
  206. MessageBox.Show("保存成功");
  207. }
  208. catch (Exception ex)
  209. {
  210. MessageBox.Show(ex.Message);
  211. }
  212. }
  213. private void Btn_Import_Click(object sender, EventArgs e)
  214. {
  215. OpenFileDialog dialog = new OpenFileDialog();
  216. dialog.Multiselect = true;//该值确定是否可以选择多个文件
  217. dialog.Title = "请选择文件夹";
  218. dialog.Filter = "指纹机记录(*.json)|*.json";
  219. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  220. {
  221. string file = dialog.FileName;
  222. try
  223. {
  224. string strJson = File.ReadAllText(file);
  225. List<lyfzAttendanceLib.GeneralLogData> list = JsonConvert.DeserializeObject<List<lyfzAttendanceLib.GeneralLogData>>(strJson);
  226. AppendDataToList(list, true);
  227. }
  228. catch (Exception ex)
  229. {
  230. }
  231. }
  232. }
  233. private void TimerAutoUpload_Tick(object sender, EventArgs e)
  234. {
  235. lock (objlock)
  236. {
  237. log.LogDebug("定时任务");
  238. if (ClientProcess.Attendance.IsConnected)
  239. {
  240. List<lyfzAttendanceLib.GeneralLogData> list = ClientProcess.Attendance.GetEnrollLogData(lyfzAttendanceLib.ReadMark.ReadNew);
  241. if (list != null)
  242. {
  243. #region 插入到列表中,同时上传
  244. Model.UserInfo user = null;
  245. foreach (lyfzAttendanceLib.GeneralLogData glog in list)
  246. {
  247. bool bStatus = false;
  248. #region 上传到服务器
  249. if (GetUserInfo(glog.EnrollNumber.ToString()) != null)
  250. {
  251. AttendanceController controller = new AttendanceController();
  252. controller.CurrentModel.no = glog.EnrollNumber.ToString();
  253. controller.CurrentModel.datetime = glog.EnrollDateTime;
  254. bStatus = controller.Add("");
  255. }
  256. #endregion
  257. DataGridViewRow dgr = new DataGridViewRow();
  258. dgr.Tag = glog;
  259. dgr.CreateCells(this.dataGridView1);
  260. user = GetUserInfo(glog.EnrollNumber.ToString());
  261. if (user != null)
  262. {
  263. dgr.Cells[0].Value = user.User_EmployeeID;
  264. dgr.Cells[1].Value = user.User_Name;
  265. dgr.Cells[2].Value = user.User_Sex;
  266. log.LogInfo("实时提交:" + user.User_Name + "," + glog.EnrollNumber.ToString() + "," + glog.EnrollDateTime);
  267. }
  268. else
  269. {
  270. dgr.Cells[0].Value = "查无此人";
  271. dgr.Cells[1].Value = "查无此人";
  272. dgr.Cells[2].Value = "查无此人";
  273. log.LogInfo("实时提交:查无此人" + "," + glog.EnrollNumber.ToString() + "," + glog.EnrollDateTime);
  274. }
  275. dgr.Cells[3].Value = glog.EnrollNumber.ToString();
  276. dgr.Cells[4].Value = glog.EnrollDateTime;
  277. dgr.Cells[5].Value = bStatus ? "成功" : "失败";
  278. this.dataGridView1.Rows.Add(dgr);
  279. }
  280. this.lab_count.Text = string.Format("记录数:{0} 条", this.dataGridView1.Rows.Count);
  281. #endregion
  282. }
  283. }
  284. else
  285. {
  286. if (nTimes > 10)
  287. {
  288. nTimes = 0;
  289. ClientProcess.Attendance.Connect();
  290. }
  291. else
  292. nTimes++;
  293. }
  294. }
  295. }
  296. private void Cb_autoupload_CheckedChanged(object sender, EventArgs e)
  297. {
  298. this.dataGridView1.Rows.Clear();
  299. if (this.cb_autoupload.Checked == true)
  300. {
  301. timerAutoUpload.Enabled = true;
  302. this.btn_upload_log.Enabled = false;
  303. }
  304. else
  305. {
  306. timerAutoUpload.Enabled = false;
  307. this.btn_upload_log.Enabled = true;
  308. }
  309. }
  310. private void Btn_InitMachine_Click(object sender, EventArgs e)
  311. {
  312. frmAuthority dlg = new frmAuthority(AdminPassword);
  313. if (dlg.ShowDialog() != DialogResult.OK)
  314. return;
  315. if (ClientProcess.Attendance.IsConnected)
  316. {
  317. if (ClientProcess.Attendance.ClearKeeperData())
  318. {
  319. MessageBox.Show("初始化指纹机成功……");
  320. log.LogInfo("初始化指纹机成功");
  321. }
  322. else
  323. {
  324. MessageBox.Show("初始化指纹机失败……");
  325. log.LogInfo("初始化指纹机成功失败");
  326. }
  327. }
  328. }
  329. private void Btn_get_all_log_Click(object sender, EventArgs e)
  330. {
  331. if (this.cb_autoupload.Checked == true)
  332. {
  333. timerAutoUpload.Enabled = false;
  334. this.cb_autoupload.Checked = false;
  335. this.btn_upload_log.Enabled = true;
  336. }
  337. #region 获取员工信息
  338. listUser.Clear();
  339. UserInfoController controller = new UserInfoController();
  340. DataSet ds = controller.GetAllList();
  341. if (ds != null)
  342. {
  343. Model.UserInfo user = new Model.UserInfo();
  344. foreach (DataRow dr in ds.Tables[0].Rows)
  345. {
  346. user = controller.DataRowToModel(dr);
  347. listUser.Add(user);
  348. }
  349. }
  350. #endregion
  351. GetEnrollLogData(lyfzAttendanceLib.ReadMark.ReadAll);
  352. }
  353. private void Btn_upload_log_Click(object sender, EventArgs e)
  354. {
  355. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate (object obj, BackgroundWorker backgroundWorker)
  356. {
  357. backgroundWorker.ReportProgress(0, "正在上传指纹数据,请稍等...");
  358. foreach (DataGridViewRow dgr in this.dataGridView1.Rows)
  359. {
  360. bool bStatus = false;
  361. lyfzAttendanceLib.GeneralLogData glog = (lyfzAttendanceLib.GeneralLogData)dgr.Tag;
  362. if (GetUserInfo(glog.EnrollNumber.ToString()) != null)
  363. {
  364. AttendanceController controller = new AttendanceController();
  365. controller.CurrentModel.no = glog.EnrollNumber.ToString();
  366. controller.CurrentModel.datetime = glog.EnrollDateTime;
  367. bStatus = controller.Add("");
  368. }
  369. dgr.Cells[5].Value = bStatus ? "成功" : "失败";
  370. }
  371. });
  372. MessageBox.Show("上传完成");
  373. }
  374. private void Btn_clear_all_log_Click(object sender, EventArgs e)
  375. {
  376. frmAuthority dlg = new frmAuthority(AdminPassword);
  377. if (dlg.ShowDialog() != DialogResult.OK)
  378. return;
  379. if (ClientProcess.Attendance.IsConnected)
  380. {
  381. if (ClientProcess.Attendance.EmptyGeneralLogData())
  382. {
  383. MessageBox.Show("清空指纹机进出记录成功……");
  384. }
  385. else
  386. {
  387. MessageBox.Show("清空指纹机进出记录失败……");
  388. }
  389. }
  390. }
  391. private void Btn_DisConnect_Click(object sender, EventArgs e)
  392. {
  393. if (ClientProcess.Attendance.IsConnected)
  394. {
  395. ClientProcess.Attendance.DisConnect();
  396. this.btn_Connect.Enabled = true;
  397. this.btn_DisConnect.Enabled = false;
  398. log.LogInfo("断开设备连接");
  399. }
  400. }
  401. private void Btn_Connect_Click(object sender, EventArgs e)
  402. {
  403. try
  404. {
  405. if (ClientProcess.DeviceConfig.MachineType == (int)lyfzAttendanceLib.DeviceType.Old)
  406. {
  407. ClientProcess.Attendance = new lyfzAttendanceLib.Machine710C(ClientProcess.DeviceConfig);
  408. }
  409. else
  410. {
  411. ClientProcess.Attendance = new lyfzAttendanceLib.MachineAoXin(ClientProcess.DeviceConfig);
  412. }
  413. if (!ClientProcess.Attendance.IsConnected)
  414. {
  415. if (ClientProcess.Attendance.Connect())
  416. {
  417. this.btn_Connect.Enabled = false;
  418. this.btn_DisConnect.Enabled = true;
  419. MessageBox.Show("连接设备成功");
  420. }
  421. else
  422. {
  423. this.btn_Connect.Enabled = true;
  424. this.btn_DisConnect.Enabled = false;
  425. MessageBox.Show("连接设备失败");
  426. }
  427. }
  428. }
  429. catch (Exception ex)
  430. {
  431. if (ClientProcess.DeviceConfig.MachineType == lyfzAttendanceLib.DeviceType.New)
  432. {
  433. if (lyfzAttendanceLib.MachineAoXin.DllRegisterServer() >= 0)
  434. {
  435. }
  436. else
  437. {
  438. MessageBox.Show("注册ocx失败,请手动注册FP_CLOCK.ocx控件");
  439. }
  440. }
  441. }
  442. }
  443. private void BtnAppFormMenu_Click(object sender, EventArgs e)
  444. {
  445. frmMachineConfig dlg = new frmMachineConfig();
  446. dlg.ShowDialog();
  447. }
  448. private void BtnAppFormExit_Click(object sender, EventArgs e)
  449. {
  450. ClientProcess.DisConnect();
  451. }
  452. private Model.UserInfo GetUserInfo(string enrollNumber)
  453. {
  454. if (this.listUser != null)
  455. {
  456. foreach (Model.UserInfo user in this.listUser)
  457. {
  458. if (user.User_FirstFingerprintID == enrollNumber || user.User_SecondaryFingerprintID == enrollNumber)
  459. {
  460. return user;
  461. }
  462. }
  463. }
  464. return null;
  465. }
  466. private void GetEnrollLogData(lyfzAttendanceLib.ReadMark readMark)
  467. {
  468. if (ClientProcess.Attendance.IsConnected)
  469. {
  470. List<lyfzAttendanceLib.GeneralLogData> list = ClientProcess.Attendance.GetEnrollLogData(readMark);
  471. AppendDataToList(list, true);
  472. }
  473. else
  474. {
  475. MessageBox.Show("指纹机未连接,请连接设备");
  476. }
  477. }
  478. private void AppendDataToList(List<lyfzAttendanceLib.GeneralLogData> list, bool isClearDataGridView)
  479. {
  480. if (list != null)
  481. {
  482. if (isClearDataGridView)
  483. this.dataGridView1.Rows.Clear();
  484. Model.UserInfo user = null;
  485. foreach (lyfzAttendanceLib.GeneralLogData glog in list)
  486. {
  487. DataGridViewRow dgr = new DataGridViewRow();
  488. dgr.Tag = glog;
  489. dgr.CreateCells(this.dataGridView1);
  490. user = GetUserInfo(glog.EnrollNumber.ToString());
  491. if (user != null)
  492. {
  493. dgr.Cells[0].Value = user.User_EmployeeID;
  494. dgr.Cells[1].Value = user.User_Name;
  495. dgr.Cells[2].Value = user.User_Sex;
  496. }
  497. else
  498. {
  499. dgr.Cells[0].Value = "查无此人";
  500. dgr.Cells[1].Value = "查无此人";
  501. dgr.Cells[2].Value = "查无此人";
  502. }
  503. dgr.Cells[3].Value = glog.EnrollNumber.ToString();
  504. dgr.Cells[4].Value = glog.EnrollDateTime;
  505. this.dataGridView1.Rows.Add(dgr);
  506. }
  507. this.lab_count.Text = string.Format("记录数:{0} 条", this.dataGridView1.Rows.Count);
  508. }
  509. }
  510. private void frmMain_Load(object sender, EventArgs e)
  511. {
  512. #region 连接服务器
  513. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate (object obj, BackgroundWorker backgroundWorker)
  514. {
  515. int i = 10;
  516. bool ret = false;
  517. string result = "";
  518. backgroundWorker.ReportProgress(0, "正在连接服务器...");
  519. do
  520. {
  521. ret = ClientProcess.Connect(out result);
  522. if (!ret)
  523. System.Threading.Thread.Sleep(15000);
  524. if (i-- < 0 )
  525. break;
  526. }while (!ret);
  527. if ( !ret )
  528. LYFZ.MessageBoxCustom.Show(result);
  529. });
  530. if (ClientProcess.ConnectStatus == false)
  531. {
  532. this.Close();
  533. return;
  534. }
  535. #endregion
  536. #region 获取员工信息
  537. UserInfoController controller = new UserInfoController();
  538. DataSet ds = controller.GetAllList();
  539. if (ds != null)
  540. {
  541. Model.UserInfo user = new Model.UserInfo();
  542. foreach (DataRow dr in ds.Tables[0].Rows)
  543. {
  544. user = controller.DataRowToModel(dr);
  545. if (user.User_Account == "admin")
  546. AdminPassword = user.User_Password;
  547. listUser.Add(user);
  548. }
  549. }
  550. #endregion
  551. try
  552. {
  553. #region 尝试连接设备
  554. ClientProcess.LoadMachineConfig();
  555. if (ClientProcess.DeviceConfig.MachineType == (int)lyfzAttendanceLib.DeviceType.Old)
  556. {
  557. ClientProcess.Attendance = new lyfzAttendanceLib.Machine710C(ClientProcess.DeviceConfig);
  558. }
  559. else
  560. {
  561. ClientProcess.Attendance = new lyfzAttendanceLib.MachineAoXin(ClientProcess.DeviceConfig);
  562. }
  563. #if DEBUG
  564. int i = 1;
  565. #else
  566. int i = 5;
  567. #endif
  568. bool ret = false;
  569. do
  570. {
  571. ret = ClientProcess.Attendance.Connect();
  572. if (!ret) System.Threading.Thread.Sleep(5000);
  573. if (i-- < 0)
  574. break;
  575. } while (!ret);
  576. if ( ret )
  577. {
  578. this.btn_Connect.Enabled = false;
  579. log.LogInfo("连接设备成功");
  580. }
  581. else
  582. {
  583. this.btn_DisConnect.Enabled = false;
  584. log.LogInfo("连接设备失败");
  585. }
  586. #endregion
  587. }
  588. catch (Exception ex)
  589. {
  590. if (ClientProcess.DeviceConfig.MachineType == lyfzAttendanceLib.DeviceType.New)
  591. {
  592. if (lyfzAttendanceLib.MachineAoXin.DllRegisterServer() >= 0)
  593. {
  594. }
  595. else
  596. {
  597. MessageBox.Show("注册ocx失败,请手动注册FP_CLOCK.ocx控件");
  598. }
  599. }
  600. }
  601. this.cb_autoupload.Checked = true;
  602. timerAutoUpload.Enabled = true;
  603. this.btn_upload_log.Enabled = false;
  604. }
  605. #region 托盘效果
  606. private void mainNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
  607. {
  608. if (this.Visible)
  609. {
  610. this.WindowState = FormWindowState.Minimized;
  611. this.mainNotifyIcon.Visible = true;
  612. this.Hide();
  613. }
  614. else
  615. {
  616. this.Visible = true;
  617. this.WindowState = FormWindowState.Normal;
  618. this.Activate();
  619. }
  620. }
  621. private void toolStripMenuItemNormal_Click(object sender, EventArgs e)
  622. {
  623. this.Visible = true;
  624. this.WindowState = FormWindowState.Normal;
  625. this.Activate();
  626. }
  627. private void toolStripMenuItemQuit_Click(object sender, EventArgs e)
  628. {
  629. if (MessageBox.Show("你确定要退出?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
  630. {
  631. this.mainNotifyIcon.Visible = false;
  632. this.Close();
  633. this.Dispose();
  634. System.Environment.Exit(System.Environment.ExitCode);
  635. }
  636. }
  637. private void frmMain_SizeChanged(object sender, EventArgs e)
  638. {
  639. if (this.WindowState == FormWindowState.Minimized)
  640. {
  641. this.Hide();
  642. this.mainNotifyIcon.Visible = true;
  643. this.ShowInTaskbar = false;//取消窗体在任务栏的显示
  644. }
  645. }
  646. private void toolStripMenuItemAuto_Click(object sender, EventArgs e)
  647. {
  648. if (toolStripMenuItemAuto.Checked)
  649. SetAutoRun();
  650. else
  651. SetAutoRun(false);
  652. }
  653. #endregion
  654. }
  655. }