Program.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using CefSharp;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Runtime.Serialization;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Timers;
  13. using System.Windows.Forms;
  14. namespace felixyin
  15. {
  16. static class Program
  17. {
  18. private static ApplicationContext context;
  19. // Will attempt to load missing assembly from either x86 or x64 subdir
  20. private static Assembly Resolver(object sender, ResolveEventArgs args)
  21. {
  22. if (args.Name.StartsWith("CefSharp"))
  23. {
  24. string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
  25. string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
  26. Environment.Is64BitProcess ? "x64" : "x86",
  27. assemblyName);
  28. return File.Exists(archSpecificPath)
  29. ? Assembly.LoadFile(archSpecificPath)
  30. : null;
  31. }
  32. return null;
  33. }
  34. /// <summary>
  35. /// 应用程序的主入口点。
  36. /// </summary>
  37. [STAThread]
  38. static void Main()
  39. {
  40. //Application.SetCompatibleTextRenderingDefault(false);
  41. Application.EnableVisualStyles();
  42. Application.DoEvents();
  43. AppDomain.CurrentDomain.AssemblyResolve += Resolver;
  44. MainForm mainForm = new MainForm();
  45. mainForm.Show();
  46. context = new ApplicationContext();
  47. context.Tag = mainForm;
  48. Application.Idle += new EventHandler(Application_Idle);
  49. Application.Run(context);
  50. // AutoStart(true);
  51. }
  52. private static void Application_Idle(object sender, EventArgs e)
  53. {
  54. Application.Idle -= new EventHandler(Application_Idle);
  55. if (context.MainForm == null)
  56. {
  57. SplashForm splashForm = new SplashForm();
  58. splashForm.Opacity = 1D;
  59. splashForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  60. splashForm.Show();
  61. splashForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  62. splashForm.ShowIcon = false;
  63. splashForm.ShowInTaskbar = false;
  64. splashForm.ControlBox = false;
  65. Thread.Sleep(3000);
  66. splashForm.Close();
  67. }
  68. }
  69. public static void showMainForm()
  70. {
  71. MainForm mainForm = (MainForm)context.Tag;
  72. context.MainForm = mainForm;
  73. /*
  74. mainForm.Opacity = 0.20D;
  75. Thread.Sleep(100);
  76. mainForm.Opacity = 0.40D;
  77. Thread.Sleep(100);
  78. mainForm.Opacity = 0.60D;
  79. Thread.Sleep(100);
  80. mainForm.Opacity = 0.80D;
  81. Thread.Sleep(100);
  82. */
  83. // Thread.Sleep(5000);
  84. mainForm.Opacity = 1D;
  85. }
  86. private static bool isShow = false;
  87. public static void toggleDevTools()
  88. {
  89. MainForm mainForm = (MainForm)context.Tag;
  90. context.MainForm = mainForm;
  91. if (isShow)
  92. {
  93. isShow = false;
  94. mainForm.webBrowser1.CloseDevTools();
  95. }
  96. else
  97. {
  98. isShow = true;
  99. mainForm.webBrowser1.ShowDevTools();
  100. }
  101. }
  102. /// <summary>
  103. /// 修改程序在注册表中的键值
  104. /// </summary>
  105. /// <param name="isAuto">true:开机启动,false:不开机自启</param>
  106. public static void AutoStart(bool isAuto)
  107. {
  108. try
  109. {
  110. if (isAuto == true)
  111. {
  112. RegistryKey R_local = Registry.LocalMachine;//RegistryKey R_local = Registry.CurrentUser;
  113. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  114. R_run.SetValue("佰安客工厂管理系统", Application.ExecutablePath);
  115. R_run.Close();
  116. R_local.Close();
  117. }
  118. else
  119. {
  120. RegistryKey R_local = Registry.LocalMachine;//RegistryKey R_local = Registry.CurrentUser;
  121. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  122. R_run.DeleteValue("佰安客工厂管理系统", false);
  123. R_run.Close();
  124. R_local.Close();
  125. }
  126. //GlobalVariant.Instance.UserConfig.AutoStart = isAuto;
  127. }
  128. catch (Exception)
  129. {
  130. //MessageBoxDlg dlg = new MessageBoxDlg();
  131. //dlg.InitialData("您需要管理员权限修改", "提示", MessageBoxButtons.OK, MessageBoxDlgIcon.Error);
  132. //dlg.ShowDialog();
  133. MessageBox.Show("程序未能设置随系统自动启动。\n可能被360、腾讯管家等软件阻止,\n或者需要管理员权限,请尝试右键以管理员运行", "佰安客科技提示");
  134. }
  135. }
  136. }
  137. }