using Client.Config; using Client.Utils; using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using Chromium; using Chromium.WebBrowser; using Chromium.Event; using Chromium.WebBrowser.Event; using System.Diagnostics; using CMS_Client.Browser_Tools; namespace CMS_Client.View { public partial class MainForm : Form { //Struct of PageError private struct PageError { public Boolean Error; public String ErrorInfo; } Random rnd = new Random(); //Internal Variables private PageError LoadingError; private NcForm NcFrm; private LoadingForm LdFrm; private static IntPtr MainHandle; private static IntPtr NcHandle; private int X = 0, Y = 0; private bool CntrlPressed; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region WINDOW_START_&_BEHAVIOUR_METHOD //Instance Method public MainForm() { LdFrm = new LoadingForm(); LdFrm.Owner = this; //Set the Loading window ShowLoading("Setup Main View..."); //Initialize the Windows InitializeComponent(); //Start Client Setting InitializeClientSettings(); //Initialize the Browser-Components InitializeBrowser(); //Set the client property this.Text = Application.ProductName + " " + Application.ProductVersion; this.Opacity = 0.0; //Initialize the Nc-Form InitializeNcForm(); //Setup Wait Cursor Cursor.Current = Cursors.WaitCursor; //Start following NC Window if (Config.VendorHmiConfig.Enabled) NcWindow.StartNcFollowing(MainHandle, NcHandle, NcFrm.Width, NcFrm.Height); else NcWindow.StartStepFollowing(MainHandle); //Load the page Browser.LoadUrl(Config.ConnectionConfig.StartingUrl); //Set the title of the window in "Loading" SetWindowTitle("Loading..."); ShowLoading("Loading Main View..."); } //On windows-Load Method private void MainForm_Load(object sender, EventArgs e) { this.DesktopLocation = new Point(X, Y); } //On Resize Form private void MainForm_Resize(object sender, EventArgs e) { if (Config.VendorHmiConfig.Enabled) { if (this.WindowState == FormWindowState.Minimized) { NcFrm.Hide(); NcWindow.ShowTaskBar(); } else { NcWindow.ShowNcWindow(); NcFrm.Show(); } } //Refresh Browser Paint Browser.Refresh(); } //Before closing the Window -> Remove the Browser (Increase the speed of closing window) private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { //Invoke method if is needed or call the method in STD mode if (!this.IsDisposed) { //Close the NC HMI && Stop Following Nc if (Config.VendorHmiConfig.Enabled) { NcWindow.CloseNcWindow(); } } } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region SETUP_METHODS //Initialize CMS-Client Settings private void InitializeClientSettings() { //Set the Transparency Key TransparencyKey = Config.ClientConfig.TranspColor; //Calculate Window Position CalcWindowPosition(); } //Initialize CMS-Client Settings private void InitializeNcForm() { //Create the Nc-Form if (Config.VendorHmiConfig.Enabled) { NcFrm = new NcForm(TransparencyKey, X, Y); NcHandle = NcFrm.Handle; NcFrm.Show(); } //Steup the Step variables MainHandle = this.Handle; } //Initialize the Browser-Component private void InitializeBrowser() { //Add Custom object to browser Browser.GlobalObject.Add(Constants.BROWSER_JS_OBJ_NAME, new BrowserJSObject(this)); //Add all Handlers Browser.DisplayHandler.OnTitleChange += BrowserTitleChanged; Browser.LoadHandler.OnLoadingStateChange += BrowserLoadsChanged; Browser.LoadHandler.OnLoadError += BrowserLoadsError; Browser.KeyboardHandler.OnKeyEvent += BrowserKeyPress; Browser.ContextMenuHandler.OnBeforeContextMenu += BrowserContextMenu; Browser.DisplayHandler.OnConsoleMessage += BrowserConsoleMessage; } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region BROWSER_EVENT_HANDLERS_METHODS //On browser Load-Error. Event Handler called by Browser private void BrowserLoadsError(object sender, CfxOnLoadErrorEventArgs e) { //Set the Load-Error global variable to TRUE LoadingError.Error = true; LoadingError.ErrorInfo = e.ErrorCode.GetTypeCode() + " - " + e.ErrorText; //write the error in the Title Bar SetWindowTitle(LoadingError.ErrorInfo); } //On browser Load-Changed. Event Handler called by Browser private void BrowserLoadsChanged(object sender, CfxOnLoadingStateChangeEventArgs e) { //if the State is NOT "loading" if (!e.Browser.IsLoading) { //Hide Loading Window HideLoadingWindow(); //Show the Main Window, when the page is loaded ShowWindow(); //If is an Error Show the Error page if (LoadingError.Error && !LoadingError.ErrorInfo.Contains("ERR_ABORTED")) MessageBox.Show("Error While Loading the Page: " + LoadingError.ErrorInfo + "\nPress:\n - F5: reload the Page\n - F1: Back to last Page"); //Set focus on the page BrowserFocus(); } //if the State is "loading" else { //Hide NC Window NcWindow.SetState(0); HideNCWindow(); //Set the title of the window in "Loading" SetWindowTitle("Loading..."); //Set the Load-Error global variable to FALSE LoadingError.Error = false; } } //On browser Title-Changed. Event Handler called by Browser private void BrowserTitleChanged(object sender, CfxOnTitleChangeEventArgs e) { if (!LoadingError.Error) SetWindowTitle(e.Title); } // on Key-Press Browser private void BrowserKeyPress(object sender, CfxOnKeyEventEventArgs e) { //Save the Control-Key pressed if (e.Event.Type == CfxKeyEventType.RawKeydown && e.Event.WindowsKeyCode == (int)Keys.ControlKey) CntrlPressed = true; else if (e.Event.Type == CfxKeyEventType.Keyup && e.Event.WindowsKeyCode == (int)Keys.ControlKey) CntrlPressed = false; //If is a Key Up do an action if (e.Event.Type == CfxKeyEventType.Keyup) { //Work only if is a Developer mode if (Config.ClientConfig.DeveloperMode) { if (e.Event.WindowsKeyCode == (int)Keys.F1) Browser.Browser.GoBack(); if (e.Event.WindowsKeyCode == (int)Keys.F2) Browser.Browser.GoForward(); if (e.Event.WindowsKeyCode == (int)Keys.F5) Browser.Browser.ReloadIgnoreCache(); if (e.Event.WindowsKeyCode == (int)Keys.F10) ShowChromeVersion(); if (e.Event.WindowsKeyCode == (int)Keys.F11) ShowChromeGPU(); if (e.Event.WindowsKeyCode == (int)Keys.F12) ShowDevTools(); } //Work everytime if (CntrlPressed && e.Event.WindowsKeyCode == (int)Keys.R) Browser.Browser.ReloadIgnoreCache(); } } //Show Dev Tools Window private void ShowDevTools(){ //Setup Dev Window CfxWindowInfo windowInfo = new CfxWindowInfo(); windowInfo.Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_POPUP | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE); windowInfo.ParentWindow = MainHandle; windowInfo.WindowName = Application.ProductName + " | CEF Dev Tools"; windowInfo.X = 1120; windowInfo.Y = 0; windowInfo.Width = 800; windowInfo.Height = 1080; //Show Dev Window Browser.BrowserHost.ShowDevTools(windowInfo, new CfxClient(), new CfxBrowserSettings(), null); } //Show Chrome GPU Window private void ShowChromeGPU() { int w=1600, h=800; //Setup Window CfxWindowInfo windowInfo = new CfxWindowInfo(); windowInfo.Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_POPUP | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE); windowInfo.ParentWindow = MainHandle; windowInfo.WindowName = Application.ProductName + " | CEF Graphic Tools"; windowInfo.X = (Screen.PrimaryScreen.Bounds.Width / 2) - (w / 2); windowInfo.Y = (Screen.PrimaryScreen.Bounds.Height / 2) - (h / 2); windowInfo.Width = w; windowInfo.Height = h; //Show Window CfxBrowserHost.CreateBrowser(windowInfo, new CfxClient(), "chrome://gpu/", ChromiumWebBrowser.DefaultBrowserSettings, null); } //Show Chrome Version Window private void ShowChromeVersion() { int w = 1600, h = 800; //Setup Window CfxWindowInfo windowInfo = new CfxWindowInfo(); windowInfo.Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_POPUP | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE); windowInfo.ParentWindow = MainHandle; windowInfo.WindowName = Application.ProductName + " | CEF Chromium-Version Tools"; windowInfo.X = (Screen.PrimaryScreen.Bounds.Width / 2) - (w / 2); windowInfo.Y = (Screen.PrimaryScreen.Bounds.Height / 2) - (h / 2); windowInfo.Width = w; windowInfo.Height = h; //Show Window CfxBrowserHost.CreateBrowser(windowInfo, new CfxClient(), "chrome://version/", ChromiumWebBrowser.DefaultBrowserSettings, null); } //Override default Browser Context Menu private void BrowserContextMenu(object sender, CfxOnBeforeContextMenuEventArgs e) { e.Model.Clear(); } //Override Javascript Console Message private void BrowserConsoleMessage(object sender, CfxOnConsoleMessageEventArgs e) { e.SetReturnValue(true); } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region INVOKES_&_OTHER_METHODS //Show MainWindow Method private void ShowWindow() { //Invoke method if is needed or call the method in STD mode if (!this.IsDisposed) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.WindowState = FormWindowState.Maximized; this.Opacity = 1.0; }); else { this.WindowState = FormWindowState.Maximized; this.Opacity = 1.0; } } } //Show Hide window private void HideLoadingWindow() { //Invoke method if is needed or call the method in STD mode if (!this.IsDisposed) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { LdFrm.Hide(); }); else { LdFrm.Hide(); } } } //Show Message window private void ShowLoading(String Message) { //Invoke method if is needed or call the method in STD mode if (!this.IsDisposed) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { //Set Message Status LdFrm.Show(Message); LdFrm.Focus(); }); else { //Set Message Status LdFrm.Show(Message); } } } //Show MainWindow Method private void BrowserFocus() { //Invoke method if is needed or call the method in STD mode if (Browser.InvokeRequired) Browser.Invoke((MethodInvoker)delegate () { Browser.Focus(); }); else Browser.Focus(); } //Set Window Title private void SetWindowTitle(String Title) { //Setup product label if (Environment.Is64BitProcess) Title = Application.ProductName + " x64 | " + Title; else Title = Application.ProductName + " x86 | " + Title; //Invoke method if is needed or call the method in STD mode if (!this.IsDisposed) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.Text = Title; }); else this.Text = Title; } } //Show NC Method public void ShowNCWindow() { if (!Config.VendorHmiConfig.Enabled) return; //Invoke method if is needed or call the method in STD mode if(!this.IsDisposed) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.Owner = null; this.NcFrm.Owner = this; this.NcFrm.TopMost = true; this.NcFrm.TopMost = false; }); else { this.Owner = null; this.NcFrm.Owner = this; this.NcFrm.TopMost = true; this.NcFrm.TopMost = false; } } } //Hide NC Method public void HideNCWindow() { if (!Config.VendorHmiConfig.Enabled) return; //Invoke method if is needed or call the method in STD mode if (!this.IsDisposed) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.NcFrm.Owner = null; this.TopMost = true; this.TopMost = false; }); else { this.NcFrm.Owner = null; this.TopMost = true; this.TopMost = false; } } } //CAlculate Window Position private void CalcWindowPosition() { //Position of the Window if (Screen.AllScreens.Length > 1 && Config.ClientConfig.RunningOnSecondaryScreen) { Screen ps = Screen.AllScreens.FirstOrDefault(S => S.Primary == true); Screen ss = Screen.AllScreens.FirstOrDefault(S => S.Primary == false); X = ps.Bounds.Width; if(ss.Bounds.Top != 0) Y = ss.Bounds.Top; } } #endregion } }