Files
cms_thermo_active/Client/View/MainForm.cs
T
2017-12-21 08:30:49 +01:00

420 lines
13 KiB
C#

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.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
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;
}
Random rnd = new Random();
//Internal Variables
private ChromiumWebBrowser CefBrowser;
private CefSettings CefBrowserSettings;
private PageError LoadingError;
private NcForm NcFrm;
private static IntPtr MainHandle;
private static IntPtr NcHandle;
private int X = 0, Y = 0;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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)
{
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.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)
{
try
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate () {
if (!CefBrowser.IsDisposed)
this.Controls.Remove(CefBrowser);
});
else
{
if (!CefBrowser.IsDisposed)
this.Controls.Remove(CefBrowser);
}
}
catch (Exception ex)
{
}
//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;
//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;
}
//Start Chromium Settings
private void InitializeCefSettings()
{
//Create the Instance
CefBrowserSettings = new CefSettings();
//Setup the Browser Cache
CefBrowserSettings.CachePath = Constants.BROWSER_CACHE_FOLDER;
//Setup CEF
if (Config.ClientConfig.RenderingMethod == Constants.Rendering.CPU)
CefBrowserSettings.SetOffScreenRenderingBestPerformanceArgs();
CefBrowserSettings.CefCommandLineArgs.Add("disable-pinch", "1");
Debug.WriteLine("[{0}]", string.Join(", ", CefBrowserSettings.CefCommandLineArgs));
//allow windowless rendering
CefBrowserSettings.WindowlessRenderingEnabled = true;
//Initialize CEF
Cef.EnableHighDPISupport();
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<LoadingStateChangedEventArgs>(BrowserLoadsChanged);
CefBrowser.TitleChanged += new EventHandler<TitleChangedEventArgs>(BrowserTitleChanged);
CefBrowser.LoadError += new EventHandler<LoadErrorEventArgs>(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)
{
//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 the Load-Error global variable to FALSE
LoadingError.Error = false;
//Set the title of the window in "Loading"
SetWindowTitle("Loading...");
}
}
//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;
}
//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.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.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
}
}