Program.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using Uestc.Auto6.Dso.ComModel;
  7. using System.Security.Principal;
  8. using Uestc.Auto6.Dso.Core;
  9. using System.Diagnostics;
  10. namespace Uestc.Auto6.Dso.U2
  11. {
  12. internal static class Program
  13. {
  14. public static DsoPrsnt Oscilloscope;
  15. /// <summary>
  16. /// The main entry point for the application.
  17. /// </summary>
  18. [STAThread]
  19. private static void Main()
  20. {
  21. // Try to create a kernel object with the specified name
  22. using (new Semaphore(0, 1, "U2OscilloscopeApp", out var creatednew))
  23. {
  24. if (creatednew)
  25. {
  26. Application.SetHighDpiMode(HighDpiMode.SystemAware);
  27. Application.EnableVisualStyles();
  28. Application.SetCompatibleTextRenderingDefault(false);
  29. //获得当前登录的Windows用户标示
  30. WindowsPrincipal principal = new(WindowsIdentity.GetCurrent());
  31. if (Debugger.IsAttached || principal.IsInRole(WindowsBuiltInRole.Administrator))
  32. {
  33. Run(); //如果是管理员,则直接运行
  34. }
  35. else
  36. {
  37. //创建启动对象
  38. ProcessStartInfo startInfo = new();
  39. startInfo.UseShellExecute = true;
  40. startInfo.WorkingDirectory = Environment.CurrentDirectory;
  41. startInfo.FileName = Application.ExecutablePath;
  42. //设置启动动作,确保以管理员身份运行
  43. startInfo.Verb = "runas";
  44. try
  45. {
  46. Process.Start(startInfo);
  47. }
  48. catch
  49. {
  50. return;
  51. }
  52. Application.Exit();
  53. }
  54. }
  55. }
  56. }
  57. private static void Run()
  58. {
  59. //配置风格管理器的风格配置项
  60. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.StyleConfig = AppStyleConfig.Singleton;
  61. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.EnableStlize = Constants.ENABLE_STYLE;
  62. var dsoform = new DsoForm();
  63. Oscilloscope = new DsoPrsnt(dsoform);
  64. var dsoapp = new DsoAppContent(dsoform, new SplashForm());
  65. Application.Run(dsoapp);
  66. Oscilloscope.Close();
  67. }
  68. public static void InitApp(Object arg)
  69. {
  70. var startup = (SplashForm)arg;
  71. startup.Invoke(new Action<String>((o) => startup.ShowMessge(o)), "Initializing...");
  72. var succ = Oscilloscope.Open("", Constants.PRODUCT, Constants.BOARD_ATTACHED ? DataSourceOpt.PCIe : DataSourceOpt.Simulator);
  73. if (succ)
  74. {
  75. Oscilloscope.Run();
  76. }
  77. if (succ)
  78. {
  79. startup.Invoke(new Action(() =>
  80. {
  81. if (Oscilloscope.View is DsoForm dsoform)
  82. {
  83. //Show causes Load
  84. dsoform.Show();
  85. dsoform.Visible = false;
  86. dsoform.InitOnLoad();
  87. }
  88. startup.Invoke(new Action<String>((o) => startup.ShowMessge(o)),"Succeed!");
  89. startup.Close();
  90. }));
  91. }
  92. else
  93. {
  94. startup.Invoke(new Action<String>((o) => startup.ShowMessge(o)), "Fail!");
  95. Application.Exit();
  96. }
  97. }
  98. #region 多屏显示
  99. public static Int32 GetScreenIdx(Form form)
  100. {
  101. Int32 i;
  102. for (i = 0; i < Screen.AllScreens.Length; i++)
  103. {
  104. if (Screen.AllScreens[i].Bounds.Contains(form.Location))
  105. {
  106. break;
  107. }
  108. }
  109. return i;
  110. }
  111. public static Int32 GetScreenCount()
  112. {
  113. return Screen.AllScreens.Length;
  114. }
  115. public static void MoveToScreen(Form form, Int32 index)
  116. {
  117. Rectangle rect;
  118. if (index >= 0)
  119. {
  120. rect = Screen.AllScreens[index].Bounds;
  121. }
  122. else
  123. {
  124. rect = Screen.AllScreens.First((scr) => scr.Primary).Bounds;
  125. }
  126. var oldrect = form.Bounds;
  127. form.Top = (rect.Height - form.Height) / 2 + rect.Y;
  128. form.Left = (rect.Width - form.Width) / 2 + rect.X;
  129. foreach (var owned in form.OwnedForms)
  130. {
  131. var hr = (owned.Top - oldrect.Top) / (Double)oldrect.Height;
  132. var top = (Int32)(rect.Top + hr * rect.Height);
  133. if (top + owned.Height > rect.Bottom)
  134. {
  135. top = rect.Bottom - owned.Height;
  136. if (top < rect.Top)
  137. {
  138. top = rect.Top;
  139. }
  140. }
  141. owned.Top = top;
  142. var wr = (owned.Left - oldrect.Left) / (Double)oldrect.Width;
  143. var left = (Int32)(rect.Left + wr * rect.Width);
  144. if (left + owned.Width > rect.Right)
  145. {
  146. left = rect.Right - owned.Width;
  147. if (left < rect.Left)
  148. {
  149. left = rect.Left;
  150. }
  151. }
  152. owned.Left = left;
  153. }
  154. }
  155. #endregion
  156. }
  157. public class DsoAppContent : ApplicationContext
  158. {
  159. private readonly Form _MainForm;
  160. public DsoAppContent(Form mainForm, Form flashForm)
  161. : base(mainForm)
  162. {
  163. _MainForm = mainForm;
  164. //First set the SplashForm to use as context
  165. MainForm = flashForm;
  166. }
  167. protected override void OnMainFormClosed(object sender, EventArgs e)
  168. {
  169. if (sender is SplashForm)
  170. {
  171. //Secondly set the DsoForm to use as context
  172. MainForm = _MainForm;
  173. MainForm.Visible = true;
  174. MainForm.Activate();
  175. }
  176. else
  177. {
  178. base.OnMainFormClosed(sender, e);
  179. }
  180. }
  181. }
  182. }