123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Threading;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.ComModel;
- using System.Security.Principal;
- using Uestc.Auto6.Dso.Core;
- using System.Diagnostics;
- namespace Uestc.Auto6.Dso.U2
- {
- internal static class Program
- {
- public static DsoPrsnt Oscilloscope;
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- private static void Main()
- {
- // Try to create a kernel object with the specified name
- using (new Semaphore(0, 1, "U2OscilloscopeApp", out var creatednew))
- {
- if (creatednew)
- {
- Application.SetHighDpiMode(HighDpiMode.SystemAware);
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- //获得当前登录的Windows用户标示
- WindowsPrincipal principal = new(WindowsIdentity.GetCurrent());
- if (Debugger.IsAttached || principal.IsInRole(WindowsBuiltInRole.Administrator))
- {
- Run(); //如果是管理员,则直接运行
- }
- else
- {
- //创建启动对象
- ProcessStartInfo startInfo = new();
- startInfo.UseShellExecute = true;
- startInfo.WorkingDirectory = Environment.CurrentDirectory;
- startInfo.FileName = Application.ExecutablePath;
- //设置启动动作,确保以管理员身份运行
- startInfo.Verb = "runas";
- try
- {
- Process.Start(startInfo);
- }
- catch
- {
- return;
- }
- Application.Exit();
- }
- }
- }
- }
- private static void Run()
- {
- //配置风格管理器的风格配置项
- Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.StyleConfig = AppStyleConfig.Singleton;
- Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.EnableStlize = Constants.ENABLE_STYLE;
- var dsoform = new DsoForm();
- Oscilloscope = new DsoPrsnt(dsoform);
- var dsoapp = new DsoAppContent(dsoform, new SplashForm());
- Application.Run(dsoapp);
- Oscilloscope.Close();
- }
- public static void InitApp(Object arg)
- {
- var startup = (SplashForm)arg;
- startup.Invoke(new Action<String>((o) => startup.ShowMessge(o)), "Initializing...");
- var succ = Oscilloscope.Open("", Constants.PRODUCT, Constants.BOARD_ATTACHED ? DataSourceOpt.PCIe : DataSourceOpt.Simulator);
- if (succ)
- {
- Oscilloscope.Run();
- }
- if (succ)
- {
- startup.Invoke(new Action(() =>
- {
- if (Oscilloscope.View is DsoForm dsoform)
- {
- //Show causes Load
- dsoform.Show();
- dsoform.Visible = false;
- dsoform.InitOnLoad();
- }
- startup.Invoke(new Action<String>((o) => startup.ShowMessge(o)),"Succeed!");
- startup.Close();
- }));
- }
- else
- {
- startup.Invoke(new Action<String>((o) => startup.ShowMessge(o)), "Fail!");
- Application.Exit();
- }
- }
- #region 多屏显示
- public static Int32 GetScreenIdx(Form form)
- {
- Int32 i;
- for (i = 0; i < Screen.AllScreens.Length; i++)
- {
- if (Screen.AllScreens[i].Bounds.Contains(form.Location))
- {
- break;
- }
- }
- return i;
- }
- public static Int32 GetScreenCount()
- {
- return Screen.AllScreens.Length;
- }
- public static void MoveToScreen(Form form, Int32 index)
- {
- Rectangle rect;
- if (index >= 0)
- {
- rect = Screen.AllScreens[index].Bounds;
- }
- else
- {
- rect = Screen.AllScreens.First((scr) => scr.Primary).Bounds;
- }
- var oldrect = form.Bounds;
- form.Top = (rect.Height - form.Height) / 2 + rect.Y;
- form.Left = (rect.Width - form.Width) / 2 + rect.X;
- foreach (var owned in form.OwnedForms)
- {
- var hr = (owned.Top - oldrect.Top) / (Double)oldrect.Height;
- var top = (Int32)(rect.Top + hr * rect.Height);
- if (top + owned.Height > rect.Bottom)
- {
- top = rect.Bottom - owned.Height;
- if (top < rect.Top)
- {
- top = rect.Top;
- }
- }
- owned.Top = top;
- var wr = (owned.Left - oldrect.Left) / (Double)oldrect.Width;
- var left = (Int32)(rect.Left + wr * rect.Width);
- if (left + owned.Width > rect.Right)
- {
- left = rect.Right - owned.Width;
- if (left < rect.Left)
- {
- left = rect.Left;
- }
- }
- owned.Left = left;
- }
- }
- #endregion
- }
- public class DsoAppContent : ApplicationContext
- {
- private readonly Form _MainForm;
- public DsoAppContent(Form mainForm, Form flashForm)
- : base(mainForm)
- {
- _MainForm = mainForm;
- //First set the SplashForm to use as context
- MainForm = flashForm;
- }
- protected override void OnMainFormClosed(object sender, EventArgs e)
- {
- if (sender is SplashForm)
- {
- //Secondly set the DsoForm to use as context
- MainForm = _MainForm;
- MainForm.Visible = true;
- MainForm.Activate();
- }
- else
- {
- base.OnMainFormClosed(sender, e);
- }
- }
- }
- }
|