using CefSharp; using CefSharp.WinForms; using Client.Config; using Client.Utils; using CMS_Client.Browser_Tools; using Microsoft.WindowsAPICodePack.Taskbar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace CMS_Client.View { public partial class MainForm : Form { //Struct of PageError private struct PageError { public Boolean Error; public String ErrorInfo; } //Internal Variables private ChromiumWebBrowser CefBrowser; private CefSettings CefBrowserSettings; private PageError LoadingError; private NcForm NcFrm; private static IntPtr MainHandle; private static IntPtr NcHandle; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region WINDOW_START_&_BEHAVIOUR_METHOD //Instance Method public MainForm() { //Start Chromium Settings InitializeCefSettings(); //Initialize the Windows InitializeComponent(); //Start Client Setting InitializeClientSettings(); //Initialize the Browser-Components InitializeBrowser(); //Set the client property this.Text = AssemblyInfo.AssemblyProduct + " " + AssemblyInfo.AssemblyVersion; this.Opacity = 0.0; //Initialize the Nc-Form InitializeNcForm(); //Setup Wait Cursor Cursor.Current = Cursors.WaitCursor; //Load the page CefBrowser.Load(Config.ConnectionConfig.Url); } //On windows-Load Method private void MainForm_Load(object sender, EventArgs e) { //Force to use on Screen 1 this.DesktopLocation = new Point(0, 0); } //On Resize Form private void MainForm_Resize(object sender, EventArgs e) { if (Config.VendorHmiConfig.Enabled) { if (this.WindowState == FormWindowState.Minimized) { NcFrm.Hide(); NcWindow.HideNcWindow(); NcWindow.ShowTaskBar(); } else { NcWindow.ShowNcWindow(); NcFrm.Show(); } } } //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) { if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { if (!CefBrowser.IsDisposed) this.Controls.Remove(CefBrowser); }); else { if (!CefBrowser.IsDisposed) this.Controls.Remove(CefBrowser); } //Close the NC HMI && Stop Following Nc if (Config.VendorHmiConfig.Enabled) { NcWindow.ShowTaskBar(); NcWindow.StopNcFollowing(); NcWindow.CloseNcWindow(); } } } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region SETUP_METHODS //Initialize CMS-Client Settings private void InitializeClientSettings() { //Set the Transparency Key TransparencyKey = Config.ClientConfig.TranspColor; } //Initialize CMS-Client Settings private void InitializeNcForm() { //Create the Nc-Form if (Config.VendorHmiConfig.Enabled) { NcFrm = new NcForm(); NcFrm.Show(); this.Owner = NcFrm; this.TopLevel = true; NcHandle = NcFrm.Handle; } //Steup the Step variables MainHandle = this.Handle; } //Start Chromium Settings private void InitializeCefSettings() { //Create the Instance CefBrowserSettings = new CefSettings(); //Setup the Browser Cache CefBrowserSettings.CachePath = Constants.BROWSER_CACHE_FOLDER; //Setup best rendering performances | Must DISABLE GPU for transparent method! -> inside SetOffScreenRenderingBestPerformanceArgs method CefBrowserSettings.SetOffScreenRenderingBestPerformanceArgs(); CefBrowserSettings.CefCommandLineArgs.Add("disable-pinch", "1"); CefBrowserSettings.CefCommandLineArgs.Add("enable-transparent-visuals", "1"); //allow windowless rendering CefBrowserSettings.WindowlessRenderingEnabled = true; //Initialize CEF Cef.Initialize(CefBrowserSettings); } //Initialize the Browser-Component private void InitializeBrowser() { //Create the Instance CefBrowser = new ChromiumWebBrowser(""); //Set the Dock Property CefBrowser.Dock = DockStyle.Fill; //Add the Event-Handlers CefBrowser.LoadingStateChanged += new EventHandler(BrowserLoadsChanged); CefBrowser.TitleChanged += new EventHandler(BrowserTitleChanged); CefBrowser.LoadError += new EventHandler(BrowserLoadsError); CefBrowser.KeyboardHandler = new CefBrowserKeyHandler(); CefBrowser.MenuHandler = new CefBrowserMenuHandler(); //Register JS Object CefBrowser.RegisterJsObject(Constants.BROWSER_JS_OBJ_NAME, new BrowserJSObject(this)); //Insert into Form this.Controls.Add(CefBrowser); } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region BROWSER_EVENT_HANDLERS_METHODS //On browser Load-Error. Event Handler called by Browser private void BrowserLoadsError(object sender, LoadErrorEventArgs 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, LoadingStateChangedEventArgs e) { //if the State is NOT "loading" if (!e.IsLoading) { //Set cursor Application.UseWaitCursor = true; if(NcFrm != null) NcFrm.UseWaitCursor = true; //Show the Main Window, when the page is loaded ShowWindow(); //Start following NC Window if (Config.VendorHmiConfig.Enabled) NcWindow.StartNcFollowing(MainHandle, NcHandle, NcFrm.Width, NcFrm.Height); else NcWindow.StartStepFollowing(MainHandle); //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 NOT "loading" else { //Set cursor Application.UseWaitCursor = false; if (NcFrm != null) NcFrm.UseWaitCursor = false; //Set the Load-Error global variable to FALSE LoadingError.Error = false; //Set the title of the window in "Loading" SetWindowTitle("Loading..."); //Show the loading state on the app ICON //TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Indeterminate); } } //On browser Title-Changed. Event Handler called by Browser private void BrowserTitleChanged(object sender, TitleChangedEventArgs e) { if (!LoadingError.Error) SetWindowTitle(e.Title); } #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.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.WindowState = FormWindowState.Maximized; this.Opacity = 1.0; }); else { this.WindowState = FormWindowState.Maximized; this.Opacity = 1.0; } } //Show MainWindow Method private void BrowserFocus() { //Invoke method if is needed or call the method in STD mode if (CefBrowser.InvokeRequired) CefBrowser.Invoke((MethodInvoker)delegate () { CefBrowser.Focus(); }); else CefBrowser.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.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.Text = Title; }); else this.Text = Title; } #endregion } }