1703 lines
62 KiB
C#
1703 lines
62 KiB
C#
using Active_Client.Properties;
|
|
using Chromium.Remote;
|
|
using Client.Config;
|
|
using Client.Utils;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static Client.Utils.Constants;
|
|
|
|
namespace Active_Client.View
|
|
{
|
|
static class NcWindow
|
|
{
|
|
//Const value used for open the Process/Application
|
|
const String FanucPath = @"\CNCScreenE\CNCScrnE.exe";
|
|
const String OsaiPath = @"\OSAI\WinNBI\ProVideo.exe";
|
|
const String SiemensPath = @"\Siemens\MotionControl\siemens\sinumerik\hmi\autostart\run_hmi.exe";
|
|
const String DemoPath = @"\Nc_Demo_Application.exe";
|
|
|
|
const String FanucName = "CNCScrnE";
|
|
const String OsaiName = "provideo";
|
|
const String SiemensName = "slsmhmihost";
|
|
const String DemoName = "Nc_Demo_Application";
|
|
|
|
const String Win7TaskBar = "Shell_TrayWnd";
|
|
const String Win7StartBtnCL = "Button";
|
|
const String Win7StartBtnTxt = "Start";
|
|
const String SiemensTool1Name = "RUN_HMI";
|
|
const String SiemensTool2Name = "SLSMSYSTEMMANAGER";
|
|
const int WaitingMs = 500;
|
|
const int TimesToTryKill = 10;
|
|
const int TimesToTryOpen = 240; // 240 * 500ms = 120000ms => 2min
|
|
|
|
//Public variables
|
|
public static bool isFollowing { get { return FollowingFocus; } }
|
|
private static bool FollowingFocus;
|
|
public static bool WindowStarted { get { return windowstarted; } }
|
|
private static bool windowstarted = false;
|
|
public static bool ProdWindowStarted { get { return prodwindowstarted; } }
|
|
private static bool prodwindowstarted = false;
|
|
public static String ProcessName { get { return processname; } }
|
|
private static String processname = "";
|
|
public static String ProcessPath { get { return processpath; } }
|
|
private static String processpath = "";
|
|
public static Process NcProcess { get { return ncprocess; } }
|
|
private static Process ncprocess;
|
|
public static Process prodProcess { get { return prodprocess; } }
|
|
private static Process prodprocess;
|
|
public static String NcCapture { get { return ncCapture; } }
|
|
private static String ncCapture;
|
|
|
|
private static int LastX, LastY;
|
|
private static int LastWidth = 1024, LastHeight = 768;
|
|
private static IntPtr hhook;
|
|
private static IntPtr hhookWindow;
|
|
private static IntPtr keybhook;
|
|
private static IntPtr MainViewHandle;
|
|
private static IntPtr TaskBarHandle;
|
|
private static IntPtr StartBtnHandle;
|
|
private static uint MainProcessPID;
|
|
private static uint NcProcessPID;
|
|
private static uint ProdProcessPID;
|
|
private static uint ActualPID;
|
|
private static WinEventDelegate procDelegate;
|
|
private static WinEventDelegate procDelegateWindow;
|
|
private static keyboardHookProc keybDelegate;
|
|
private static uint LastdwmsEventTime;
|
|
private static uint LastHookedPID;
|
|
private static uint KeyboardPID;
|
|
private static int TriedTimes;
|
|
private static bool ProcKilled;
|
|
private static bool KeyboardVisible;
|
|
private static IntPtr KeyboardHandle;
|
|
private static CfrRect LastKeybPoint;
|
|
private static Point LastKeybPosition;
|
|
private static Point LastKeybPositionReopen;
|
|
|
|
private static IntPtr ActualHND;
|
|
private static IntPtr LastHookedHND;
|
|
private static IntPtr NcHND;
|
|
private static Thread CaptureThread;
|
|
private static Thread FollowNCThread;
|
|
private static int ncWindowWidth;
|
|
private static int ncWindowHeight;
|
|
private static int ncWindowX;
|
|
private static int ncWindowY;
|
|
private static bool altPressed;
|
|
private static bool ctrlPressed;
|
|
private static bool shiftPressed;
|
|
private static uint KeybPID;
|
|
private static uint ActualWindowPID;
|
|
private static int ActiveNCWindowStyle;
|
|
private static int ActiveNCWindowBorder;
|
|
private static int ActiveWindowNCWindowStyle;
|
|
private static int ActiveWindowNCWindowBorder;
|
|
private static uint ActiveWindowNCPopUp;
|
|
private static uint ActiveNCWindowNCPopUp;
|
|
|
|
|
|
public static MainForm mainFrm;
|
|
private static string prodEXEname;
|
|
|
|
|
|
public static NcState State { get { return state; } }
|
|
private static NcState state;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region PUBLIC_CUSTOM_METHODS
|
|
|
|
// Start/Open the NC Window
|
|
public static int StartNcWindow()
|
|
{
|
|
Process[] processes;
|
|
|
|
//Setup the variables
|
|
ProcKilled = false;
|
|
|
|
//Set Window Started = false
|
|
windowstarted = false;
|
|
|
|
//Setup the Path/Name of the process
|
|
SetupNcProcess();
|
|
|
|
//Read if exists a Process with correct name
|
|
processes = Process.GetProcessesByName(processname);
|
|
|
|
//Save the Taskbar Windows Handle
|
|
TaskBarHandle = FindWindow(Win7TaskBar, "");
|
|
StartBtnHandle = FindWindow(Win7StartBtnCL, Win7StartBtnTxt);
|
|
|
|
//Kill Window Keyboard
|
|
closeVirtualKeyboard();
|
|
|
|
//Kill tool processes if they had been started whitout HMI (Only Siemens)
|
|
if (Config.VendorHmiConfig.Type == 2 && processes.Length <= 0)
|
|
{
|
|
//Kill All Windows
|
|
KillAllProcessWithName(SiemensTool2Name);
|
|
}
|
|
|
|
//If exists the Process
|
|
if (processes.Length > 0)
|
|
{
|
|
//Set the first founded process
|
|
ncprocess = processes[0];
|
|
ncprocess.WaitForInputIdle();
|
|
|
|
//Wait until the process is started
|
|
if (Config.VendorHmiConfig.Type != 2)
|
|
{
|
|
//If is not Siemens try to wait the HMI for a period of tyme. After that re-open the window
|
|
TriedTimes = 1;
|
|
while ((processes[0].MainWindowHandle == IntPtr.Zero || !isWindowReady(processes[0].MainWindowTitle)) && TriedTimes < TimesToTryKill)
|
|
{
|
|
processes = Process.GetProcessesByName(processname);
|
|
if (processes[0].MainWindowHandle == IntPtr.Zero)
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//If is Siemens wait the HMI for an infinite time
|
|
while (processes[0].MainWindowHandle == IntPtr.Zero || !isWindowReady(processes[0].MainWindowTitle))
|
|
{
|
|
processes = Process.GetProcessesByName(processname);
|
|
if (processes[0].MainWindowHandle == IntPtr.Zero)
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Kill the process if needed
|
|
if ((TriedTimes == TimesToTryKill) && (Config.VendorHmiConfig.Type != 2))
|
|
{
|
|
//Kill the Process
|
|
CloseNcWindow(true);
|
|
|
|
//Setup the Variable
|
|
ProcKilled = true;
|
|
}
|
|
}
|
|
|
|
if ((processes.Length <= 0 || ProcKilled) && (Config.VendorHmiConfig.Type != 2))
|
|
{
|
|
ncprocess = new Process();
|
|
|
|
// Check if exists the Nc Path
|
|
if (!File.Exists(processpath))
|
|
return 1;
|
|
|
|
//Setup the Process path
|
|
ncprocess.StartInfo.FileName = processpath;
|
|
|
|
//Add arguments if is Fanuc NC
|
|
if (Config.VendorHmiConfig.Type == 1)
|
|
ncprocess.StartInfo.Arguments = "/H=" + Config.VendorHmiConfig.IpAddress + ":" + Config.VendorHmiConfig.Port + "/T=30";
|
|
|
|
//Start the Process
|
|
ncprocess.Start();
|
|
ncprocess.WaitForInputIdle(1000);
|
|
|
|
//Wait until the process is started
|
|
TriedTimes = 1;
|
|
while ((processes.Length <= 0 || (processes[0].MainWindowHandle == IntPtr.Zero) || !isWindowReady(processes[0].MainWindowTitle)) && TriedTimes < TimesToTryOpen)
|
|
{
|
|
processes = Process.GetProcessesByName(processname);
|
|
if (processes.Length <= 0 || (processes[0].MainWindowHandle == IntPtr.Zero) || !isWindowReady(processes[0].MainWindowTitle))
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
if (TriedTimes == TimesToTryOpen)
|
|
return 2;
|
|
|
|
//Set the first founded process
|
|
ncprocess = processes[0];
|
|
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
//Set the kill method
|
|
ncprocess.Exited += OnprocessExit;
|
|
|
|
//Set Window Started => true
|
|
windowstarted = true;
|
|
|
|
//Show the Main Window & Hide the Others (Only Siemens)
|
|
ShowWindow(ncprocess.MainWindowHandle, WS_SHOW);
|
|
if (Config.VendorHmiConfig.Type == 2)
|
|
{
|
|
//Wait 500 ms of Siemens software
|
|
Thread.Sleep(500);
|
|
|
|
//Hide Second Window
|
|
hideAllProcWindows(SiemensTool1Name);
|
|
|
|
//Hide first Window
|
|
hideAllProcWindows(SiemensTool2Name);
|
|
|
|
SetNcIcon(Resources.SinumerikHmi);
|
|
}
|
|
|
|
//Wait until window title become correct
|
|
|
|
//Remove the Frame Border
|
|
RemoveFrameBorder(ncprocess.MainWindowHandle);
|
|
|
|
|
|
//Maximize Window if is Osai
|
|
if (Config.VendorHmiConfig.Type == 3)
|
|
ShowWindow(ncprocess.MainWindowHandle, SW_SHOWMAXIMIZED);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
// Start/Open the PROD Window
|
|
public static int StartProdWindow(string EXEpath, string EXEname)
|
|
{
|
|
Process[] processes;
|
|
|
|
ProcKilled = false;
|
|
prodEXEname = EXEname;
|
|
|
|
//Set Window Started = false
|
|
prodwindowstarted = false;
|
|
|
|
//Read if exists a Process with correct name
|
|
processes = Process.GetProcessesByName(EXEname);
|
|
|
|
//If exists the Process
|
|
if (processes.Length > 0)
|
|
{
|
|
//Set the first founded process
|
|
prodprocess = processes[0];
|
|
prodprocess.WaitForInputIdle(1000);
|
|
|
|
//Wait until the process is started
|
|
TriedTimes = 1;
|
|
while (processes[0].MainWindowHandle == IntPtr.Zero && TriedTimes < TimesToTryKill)
|
|
{
|
|
processes = Process.GetProcessesByName(EXEname);
|
|
if (processes[0].MainWindowHandle == IntPtr.Zero)
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
|
|
//Kill the process if needed
|
|
if ((TriedTimes == TimesToTryKill))
|
|
{
|
|
//Kill the Process
|
|
CloseProdWindow(true);
|
|
|
|
//Setup the Variable
|
|
ProcKilled = true;
|
|
}
|
|
}
|
|
|
|
if (processes.Length <= 0 || ProcKilled)
|
|
{
|
|
prodprocess = new Process();
|
|
|
|
// Check if exists the Nc Path
|
|
if (!File.Exists(EXEpath))
|
|
return 1;
|
|
|
|
//Setup the Process path
|
|
prodprocess.StartInfo.FileName = EXEpath;
|
|
|
|
//Start the Process
|
|
prodprocess.Start();
|
|
prodprocess.WaitForInputIdle();
|
|
|
|
//Wait until the process is started
|
|
TriedTimes = 1;
|
|
while ((processes.Length <= 0 || (processes[0].MainWindowHandle == IntPtr.Zero)) && TriedTimes < TimesToTryOpen)
|
|
{
|
|
processes = Process.GetProcessesByName(EXEname);
|
|
if (processes.Length <= 0 || (processes[0].MainWindowHandle == IntPtr.Zero))
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
if (TriedTimes == TimesToTryOpen)
|
|
return 2;
|
|
|
|
//Set the first founded process
|
|
prodprocess = processes[0];
|
|
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
|
|
//Set Window Started => true
|
|
prodwindowstarted = true;
|
|
|
|
//Show the Main Window & Hide the Others (Only Siemens)
|
|
ShowWindow(prodprocess.MainWindowHandle, WS_SHOW);
|
|
|
|
//Remove the Frame Border
|
|
RemoveFrameBorder(prodprocess.MainWindowHandle);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
//Kill/Close the NC Window
|
|
public static void CloseNcWindow(bool forced)
|
|
{
|
|
Process[] processes;
|
|
int style;
|
|
|
|
//Stop Following Nc
|
|
StopNcFollowing();
|
|
|
|
//If the NC is Siemens Exit from Function and lets the HMI to continue his life
|
|
if (!forced && Config.VendorHmiConfig.Type == 2)
|
|
{
|
|
MinimizeNcWindow();
|
|
return;
|
|
}
|
|
//If FollowNcWindow is OFF, set the Border and lets the HMI to continue his life
|
|
else if (!forced && !Config.VendorHmiConfig.FollowNcWindow)
|
|
{
|
|
ShowNcWindow();
|
|
style = GetWindowLong(ncprocess.MainWindowHandle, GWL_STYLE);
|
|
SetWindowLong(ncprocess.MainWindowHandle, GWL_STYLE, (style | WS_CAPTION | WS_THICKFRAME));
|
|
return;
|
|
}
|
|
|
|
//Kill the Process
|
|
if (ncprocess != null && !ncprocess.HasExited)
|
|
ncprocess.Kill();
|
|
|
|
//Read if exists a Process with correct name
|
|
processes = Process.GetProcessesByName(processname);
|
|
|
|
//Wait until the process is killed
|
|
TriedTimes = 1;
|
|
while (processes.Length > 0 && TriedTimes < TimesToTryKill)
|
|
{
|
|
processes = Process.GetProcessesByName(processname);
|
|
if (processes.Length > 0)
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
|
|
//Kill Tool Process
|
|
if (Config.VendorHmiConfig.Type == 2)
|
|
KillAllProcessWithName(SiemensTool2Name);
|
|
|
|
}
|
|
|
|
|
|
//Kill/Close the PROD Window
|
|
public static void CloseProdWindow(bool forced)
|
|
{
|
|
Process[] processes;
|
|
|
|
if (prodEXEname == null)
|
|
return;
|
|
|
|
//Kill the Process
|
|
if (prodprocess != null && !prodprocess.HasExited)
|
|
prodprocess.Kill();
|
|
|
|
//Read if exists a Process with correct name
|
|
processes = Process.GetProcessesByName(prodEXEname);
|
|
|
|
//Wait until the process is killed
|
|
TriedTimes = 1;
|
|
while (processes.Length > 0 && TriedTimes < TimesToTryKill)
|
|
{
|
|
processes = Process.GetProcessesByName(prodEXEname);
|
|
if (processes.Length > 0)
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//Show Nc Window
|
|
public static void ShowNcWindow()
|
|
{
|
|
if (windowstarted)
|
|
{
|
|
ShowWindow(ncprocess.MainWindowHandle, SW_SHOWNOACTIVATE);
|
|
ResizeAndMoveNcWindow(ncWindowX, ncWindowY, ncWindowWidth, ncWindowHeight);
|
|
}
|
|
}
|
|
|
|
//Show Prod Window
|
|
public static void ShowProdWindow()
|
|
{
|
|
if (prodwindowstarted)
|
|
{
|
|
ShowWindow(prodprocess.MainWindowHandle, SW_SHOWNOACTIVATE);
|
|
ResizeAndMoveProdWindow(HMI_WINDOW_POS_X_DEMO, HMI_WINDOW_POS_Y_DEMO, HMI_WINDOW_WIDTH_DEMO, HMI_WINDOW_HEIGHT_DEMO);
|
|
}
|
|
}
|
|
|
|
|
|
//Minimize Nc Window
|
|
public static void MinimizeNcWindow()
|
|
{
|
|
if (windowstarted)
|
|
ShowWindow(ncprocess.MainWindowHandle, WS_MINIMIZE);
|
|
}
|
|
|
|
//Show Prod Window
|
|
public static void MinimizeProdWindow()
|
|
{
|
|
if (prodwindowstarted)
|
|
ShowWindow(prodprocess.MainWindowHandle, WS_MINIMIZE);
|
|
}
|
|
|
|
|
|
//Hide Nc Window
|
|
public static void HideNcWindow()
|
|
{
|
|
if (windowstarted)
|
|
ShowWindow(ncprocess.MainWindowHandle, WS_HIDE);
|
|
}
|
|
|
|
//Hide Prod Window
|
|
public static void HideProdWindow()
|
|
{
|
|
if (prodwindowstarted)
|
|
ShowWindow(prodprocess.MainWindowHandle, WS_HIDE);
|
|
}
|
|
|
|
|
|
//Set State
|
|
public static void SetState(NcState St)
|
|
{
|
|
state = St;
|
|
}
|
|
|
|
|
|
|
|
//Show Windows Taskbar
|
|
public static void ShowTaskBar()
|
|
{
|
|
if (!Config.ClientConfig.RunningOnSecondaryScreen)
|
|
{
|
|
ShowWindow(TaskBarHandle, SW_SHOWNOACTIVATE);
|
|
ShowWindow(StartBtnHandle, SW_SHOWNOACTIVATE);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Hide Windows Taskbar
|
|
public static void HideTaskBar()
|
|
{
|
|
if (windowstarted && !Config.ClientConfig.RunningOnSecondaryScreen)
|
|
{
|
|
ShowWindow(TaskBarHandle, WS_HIDE);
|
|
ShowWindow(StartBtnHandle, WS_HIDE);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Set NC Icon
|
|
public static void SetNcIcon(Icon icon)
|
|
{
|
|
if (windowstarted)
|
|
SendMessage(ncprocess.MainWindowHandle, WM_SETICON, ICON_BIG, icon.Handle);
|
|
}
|
|
|
|
|
|
|
|
//Force NC Focus
|
|
public static void ForceNcFocus()
|
|
{
|
|
if (windowstarted)
|
|
ForceFocus(ncprocess.MainWindowHandle);
|
|
}
|
|
|
|
//Force Prod Focus
|
|
public static void ForceProdFocus()
|
|
{
|
|
if (windowstarted)
|
|
ForceFocus(prodprocess.MainWindowHandle);
|
|
}
|
|
|
|
|
|
//Force NC Redraw
|
|
public static void ForceNcRedraw()
|
|
{
|
|
if (windowstarted)
|
|
{
|
|
HideNcWindow();
|
|
ShowNcWindow();
|
|
}
|
|
}
|
|
|
|
|
|
//Force STEP Focus
|
|
public static void ForceStepFocus()
|
|
{
|
|
if (MainViewHandle != IntPtr.Zero)
|
|
ForceFocus(MainViewHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Force Focus External app
|
|
public static void ForceExtFocus(IntPtr Handle, int X, int Y, int width, int height)
|
|
{
|
|
if (Handle != IntPtr.Zero)
|
|
ForceFocus(Handle);
|
|
|
|
if (width > 0 && height > 0)
|
|
MoveWindow(Handle, X, Y, width, height, true);
|
|
}
|
|
|
|
|
|
//Start Step Managing
|
|
public static void StartStepFollowing(IntPtr Handle)
|
|
{
|
|
//Setup the Handle-variable
|
|
MainViewHandle = Handle;
|
|
|
|
|
|
//Start Keyboard Hook
|
|
GetWindowThreadProcessId(MainViewHandle, out MainProcessPID);
|
|
|
|
if (keybhook != IntPtr.Zero)
|
|
UnhookWindowsHookEx(keybhook);
|
|
keybDelegate = new keyboardHookProc(KeybEventProc);
|
|
keybhook = SetWindowsHookEx(WH_KEYBOARD_LL, keybDelegate, IntPtr.Zero, 0);
|
|
|
|
}
|
|
|
|
|
|
//Start following Nc Window (bring-to-font the main window)
|
|
public static void StartNcFollowing(IntPtr Handle, IntPtr NcHandle, IntPtr ProdHandle, int Width, int Height)
|
|
{
|
|
//Setup the Handle-variable
|
|
MainViewHandle = Handle;
|
|
NcHND = NcHandle;
|
|
|
|
//Set the parent of the window
|
|
if (windowstarted)
|
|
SetParent(ncprocess.MainWindowHandle, NcHandle);
|
|
|
|
//Set the parent of the window
|
|
if (prodwindowstarted)
|
|
SetParent(prodprocess.MainWindowHandle, ProdHandle);
|
|
|
|
//Get the Process-Id
|
|
GetWindowThreadProcessId(MainViewHandle, out MainProcessPID);
|
|
if (windowstarted)
|
|
GetWindowThreadProcessId(ncprocess.MainWindowHandle, out NcProcessPID);
|
|
if (prodwindowstarted)
|
|
GetWindowThreadProcessId(prodProcess.MainWindowHandle, out ProdProcessPID);
|
|
|
|
//Resize the Window
|
|
if (windowstarted)
|
|
ResizeAndMoveNcWindow(ncWindowX, ncWindowY, ncWindowWidth, ncWindowHeight);
|
|
if (prodwindowstarted)
|
|
ResizeAndMoveProdWindow(HMI_WINDOW_POS_X_DEMO, HMI_WINDOW_POS_Y_DEMO, HMI_WINDOW_WIDTH_DEMO, HMI_WINDOW_HEIGHT_DEMO);
|
|
|
|
//Start Keyboard Hook
|
|
if (keybhook != IntPtr.Zero)
|
|
UnhookWindowsHookEx(keybhook);
|
|
keybDelegate = new keyboardHookProc(KeybEventProc);
|
|
keybhook = SetWindowsHookEx(WH_KEYBOARD_LL, keybDelegate, IntPtr.Zero, 0);
|
|
|
|
//Start the Hook
|
|
if (MainViewHandle != IntPtr.Zero && MainProcessPID != 0 && ((windowstarted && NcProcessPID != 0) || (prodwindowstarted && ProdProcessPID != 0)))
|
|
{
|
|
//Detach the hook
|
|
if (hhook != IntPtr.Zero)
|
|
UnhookWinEvent(hhook);
|
|
|
|
//Attach the new hook
|
|
procDelegate = new WinEventDelegate(WinEventProc);
|
|
hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
|
|
|
|
//Detach the hook
|
|
if (hhookWindow != IntPtr.Zero)
|
|
UnhookWinEvent(hhookWindow);
|
|
|
|
//Attach the OSAI hook
|
|
if (windowstarted && (Config.VendorHmiConfig.Type == 2))// || Config.VendorHmiConfig.Type == 3))
|
|
{
|
|
procDelegateWindow = new WinEventDelegate(WinEventProcWindow);
|
|
hhookWindow = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_DESTROY, IntPtr.Zero, procDelegateWindow, 0, 0, WINEVENT_OUTOFCONTEXT);
|
|
}
|
|
|
|
FollowingFocus = true;
|
|
|
|
//Start the new Thread of Image Captutre - in Standby (NICOLA CARMINATI 25/01/2018)
|
|
//if (CaptureThread != null)
|
|
// CaptureThread.Abort();
|
|
//CaptureThread = new Thread(new ThreadStart(CaptureCycle));
|
|
//CaptureThread.Start();
|
|
|
|
//Start the new Thread of Follow-NC-Window
|
|
if (windowstarted && Config.VendorHmiConfig.FollowNcWindow)
|
|
{
|
|
|
|
if (FollowNCThread != null)
|
|
FollowNCThread.Abort();
|
|
FollowNCThread = new Thread(new ThreadStart(FollowNcBehaviour));
|
|
FollowNCThread.Start();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Stop following Nc Window (bring-to-font the main window) -> ONLY FOR OSAI System!
|
|
public static void StopNcFollowing()
|
|
{
|
|
//Detach the hook
|
|
if (hhook != IntPtr.Zero)
|
|
UnhookWinEvent(hhook);
|
|
|
|
if (hhookWindow != IntPtr.Zero)
|
|
UnhookWinEvent(hhookWindow);
|
|
|
|
if (keybhook != IntPtr.Zero)
|
|
UnhookWindowsHookEx(keybhook);
|
|
|
|
if (FollowNCThread != null)
|
|
FollowNCThread.Abort();
|
|
|
|
//Un-parent the window
|
|
if ((Config.VendorHmiConfig.Type == 2) && ncprocess.MainWindowHandle != IntPtr.Zero)
|
|
SetParent(ncprocess.MainWindowHandle, IntPtr.Zero);
|
|
|
|
FollowingFocus = false;
|
|
|
|
//Stop the Image-Capture Thread
|
|
if (CaptureThread != null)
|
|
CaptureThread.Abort();
|
|
|
|
|
|
//Stop the Follow-Nc Thread
|
|
if (Config.VendorHmiConfig.FollowNcWindow && FollowNCThread != null)
|
|
FollowNCThread.Abort();
|
|
|
|
}
|
|
|
|
|
|
//Take screenshot of the Nc Window
|
|
public static void CaptureHMI()
|
|
{
|
|
RECT rc = new RECT();
|
|
Graphics gr;
|
|
bool success = false;
|
|
Bitmap bmp;
|
|
MemoryStream m = new MemoryStream();
|
|
|
|
Thread.BeginThreadAffinity();
|
|
m = new MemoryStream();
|
|
String Base64Capture;
|
|
if (ncprocess != null && ncprocess.MainWindowHandle != IntPtr.Zero)
|
|
{
|
|
GetWindowRect(ncprocess.MainWindowHandle, out rc);
|
|
bmp = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top);
|
|
gr = Graphics.FromImage(bmp);
|
|
success = PrintWindow(ncprocess.MainWindowHandle, gr.GetHdc(), 0);
|
|
gr.ReleaseHdc();
|
|
if (success && bmp != null)
|
|
{
|
|
bmp.Save(m, ImageFormat.Png);
|
|
Base64Capture = Convert.ToBase64String(m.ToArray());
|
|
ncCapture = "data:image/png;base64," + Base64Capture;
|
|
}
|
|
m.Dispose();
|
|
bmp.Dispose();
|
|
gr.Dispose();
|
|
}
|
|
else
|
|
ncCapture = "data:image/png;base64,";
|
|
|
|
}
|
|
|
|
public static void ActivateNcWindow()
|
|
{
|
|
SetForegroundWindow(ncprocess.MainWindowHandle);
|
|
}
|
|
|
|
#endregion
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region PUBLIC_KEYBOARD_METHODS
|
|
|
|
//Show virtual Keyboard in last position
|
|
public static void reOpenVirtualKeyboard()
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
Thread.Sleep(100);
|
|
if (KeyboardVisible && LastKeybPoint != null && !IsWindowVisible(KeyboardHandle))
|
|
openVirtualKeyboard(LastKeybPoint, false, true);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
//Open/Show virtual Keyboard
|
|
public static void openVirtualKeyboard(CfrRect LastPoint, Boolean forcedCenterPosition, Boolean forcedLastPosition)
|
|
{
|
|
//Save last Point
|
|
LastKeybPoint = LastPoint;
|
|
|
|
//Elaborate the Keyboard position
|
|
Point Position = ElaborateKeyboardPosition(LastPoint, forcedCenterPosition, forcedLastPosition);
|
|
|
|
//Run in a Task (Don't stop Browser UI)
|
|
Task.Run(() =>
|
|
{
|
|
|
|
//Open & Find Keyboard
|
|
Process[] processes = Process.GetProcessesByName(KEYB_PROC_NAME);
|
|
if (processes.Length == 0)
|
|
{
|
|
//If i don't find the Keyboard -> Start it!
|
|
launchProcessKeyboard();
|
|
|
|
//Delete Menu buttons
|
|
deleteMenuKeyboard(KeyboardHandle);
|
|
|
|
//Move Window
|
|
NcWindow.MoveWindow(KeyboardHandle, Position.X, Position.Y, Constants.KEYB_WIDTH, Constants.KEYB_HEIGHT, true);
|
|
}
|
|
else
|
|
{
|
|
|
|
//Try to show window
|
|
if (KeyboardHandle != IntPtr.Zero)
|
|
{
|
|
//Move Window
|
|
NcWindow.MoveWindow(KeyboardHandle, Position.X, Position.Y, Constants.KEYB_WIDTH, Constants.KEYB_HEIGHT, true);
|
|
|
|
//Delete Menu buttons
|
|
deleteMenuKeyboard(KeyboardHandle);
|
|
|
|
//Try to show window
|
|
ShowWindow(KeyboardHandle, SW_SHOWNOACTIVATE);
|
|
}
|
|
|
|
//Check if window is visible
|
|
if (KeyboardHandle == IntPtr.Zero || !IsWindowVisible(KeyboardHandle))
|
|
{
|
|
//Kill actual process
|
|
closeVirtualKeyboard();
|
|
|
|
//Start a new one!
|
|
launchProcessKeyboard();
|
|
|
|
//Delete Menu buttons
|
|
deleteMenuKeyboard(KeyboardHandle);
|
|
|
|
//Move Window
|
|
NcWindow.MoveWindow(KeyboardHandle, Position.X, Position.Y, Constants.KEYB_WIDTH, Constants.KEYB_HEIGHT, true);
|
|
}
|
|
}
|
|
|
|
// Save PID
|
|
GetWindowThreadProcessId(KeyboardHandle, out KeyboardPID);
|
|
|
|
//Setup variable "Keyboard visible"
|
|
KeyboardVisible = true;
|
|
|
|
});
|
|
}
|
|
|
|
|
|
|
|
//Close virtual Keyboard
|
|
public static void closeVirtualKeyboard()
|
|
{
|
|
Process[] processes = Process.GetProcessesByName(KEYB_PROC_NAME);
|
|
RECT KeybRct = new RECT();
|
|
|
|
//Save last position
|
|
if (KeyboardHandle != IntPtr.Zero)
|
|
{
|
|
GetWindowRect(KeyboardHandle, out KeybRct);
|
|
LastKeybPositionReopen = new Point() { X = KeybRct.Left, Y = KeybRct.Top };
|
|
}
|
|
|
|
for (int i = 0; i < processes.Length; i++)
|
|
processes[i].Kill();
|
|
|
|
KeyboardPID = 0;
|
|
KeyboardHandle = IntPtr.Zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Hide virtual keyboard
|
|
public static void hideVirtualKeyboard()
|
|
{
|
|
if (KeyboardHandle != IntPtr.Zero)
|
|
ShowWindow(KeyboardHandle, WS_HIDE);
|
|
|
|
//Setup variable "Keyboard visible"
|
|
KeyboardVisible = false;
|
|
LastKeybPosition.X = -1;
|
|
LastKeybPosition.Y = -1;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//Private Methods
|
|
|
|
//Elaborates Keyboard position
|
|
private static Point ElaborateKeyboardPosition(CfrRect ObjectPoint, Boolean forcedCenterPosition, Boolean forcedLastPosition)
|
|
{
|
|
int keybX = 0, keybY = 0;
|
|
RECT KeybRct = new RECT();
|
|
|
|
//Find the window position
|
|
if (KeyboardHandle != IntPtr.Zero)
|
|
GetWindowRect(KeyboardHandle, out KeybRct);
|
|
|
|
//If i moved manually the Keyboard -> let where it is, until becomes hide
|
|
if (!forcedLastPosition && (KeyboardHandle != IntPtr.Zero && (LastKeybPosition.X != -1 && LastKeybPosition.Y != -1) && (KeybRct.Left != LastKeybPosition.X || KeybRct.Top != LastKeybPosition.Y)))
|
|
return new Point() { X = KeybRct.Left, Y = KeybRct.Top };
|
|
|
|
//If have to re-open, move at the point where was before close
|
|
else if (forcedLastPosition && LastKeybPositionReopen.X != -1 && LastKeybPositionReopen.Y != -1)
|
|
return new Point() { X = LastKeybPositionReopen.X, Y = LastKeybPositionReopen.Y };
|
|
|
|
//If flag forcedCenterPosition is true -> put in center-bottom position
|
|
else if (forcedCenterPosition)
|
|
{
|
|
LastKeybPosition = new Point()
|
|
{
|
|
X = (Screen.PrimaryScreen.Bounds.Width / 2) - (Constants.KEYB_WIDTH / 2),
|
|
Y = Screen.PrimaryScreen.Bounds.Height - Constants.KEYB_HEIGHT
|
|
};
|
|
return LastKeybPosition;
|
|
}
|
|
//In all others calc the position from HTML Node changed
|
|
else
|
|
{
|
|
//
|
|
|
|
//Setup position Y
|
|
/*
|
|
* 1) try to put the keyboard below the html object. If there's not enough space (2)
|
|
* 2) try to put above the html object. If there's not enough space yet (3)
|
|
* 3) put close to the bottom edge of the screen
|
|
*/
|
|
keybY = ObjectPoint.Y + ObjectPoint.Height + Constants.KEYB_Y_OFFSET;
|
|
if ((keybY + Constants.KEYB_HEIGHT) > Screen.PrimaryScreen.Bounds.Height)
|
|
keybY = ObjectPoint.Y - Constants.KEYB_Y_OFFSET - Constants.KEYB_HEIGHT;
|
|
if (keybY < 0)
|
|
keybY = Screen.PrimaryScreen.Bounds.Height - Constants.KEYB_HEIGHT;
|
|
|
|
|
|
//Setup position X
|
|
/*
|
|
* 1) try to put the keyboard in the middle the html object. If is out of the screen at Left Border (2), at Right Border (3)
|
|
* 2) put close to the left edge of the screen
|
|
* 3) put close to the right edge of the screen
|
|
*/
|
|
keybX = ObjectPoint.X + (ObjectPoint.Width / 2) - (Constants.KEYB_WIDTH / 2);
|
|
if (keybX < 0)
|
|
keybX = 0;
|
|
if (keybX + Constants.KEYB_WIDTH > Screen.PrimaryScreen.Bounds.Width)
|
|
keybX = Screen.PrimaryScreen.Bounds.Width - Constants.KEYB_WIDTH;
|
|
|
|
//Setup the Final Position
|
|
LastKeybPosition = new Point() { X = keybX, Y = keybY };
|
|
return LastKeybPosition;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete Menu Action of a window
|
|
private static void deleteMenuKeyboard(IntPtr Handle)
|
|
{
|
|
// NO Control-Box + NO resize window
|
|
SetWindowLong(Handle, GWL_STYLE, (GetWindowLong(Handle, GWL_STYLE) & ~0x80000 & ~0x40000));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Launch new Process OSK
|
|
private static void launchProcessKeyboard()
|
|
{
|
|
Process[] processes = Process.GetProcessesByName(KEYB_PROC_NAME);
|
|
Process process = new Process();
|
|
process.StartInfo.FileName = KEYB_EXE_NAME;
|
|
process.Start();
|
|
int times = 0;
|
|
|
|
//Find the New Window Handle
|
|
do { processes = Process.GetProcessesByName(KEYB_PROC_NAME); Thread.Sleep(10); times++; }
|
|
while ((processes.Length == 0 || processes[0].MainWindowHandle == IntPtr.Zero) && times < 20);
|
|
|
|
//Filter if i can't find the window
|
|
if (times == 20)
|
|
{
|
|
KeyboardHandle = IntPtr.Zero;
|
|
return;
|
|
}
|
|
|
|
//Set the window Handle Parameter
|
|
KeyboardHandle = processes[0].MainWindowHandle;
|
|
}
|
|
#endregion
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region WINDOWS_EVENT_OVERRIDE
|
|
|
|
private static void WinEventProcWindow(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
|
{
|
|
|
|
//Read the actual ID of the Process
|
|
GetWindowThreadProcessId(hwnd, out ActualWindowPID);
|
|
if (ActualWindowPID == NcProcessPID && NcProcessPID != 0 && hWinEventHook != ncprocess.MainWindowHandle)
|
|
{
|
|
//Read the style of the window
|
|
ActiveWindowNCWindowStyle = GetWindowLong(hwnd, GWL_STYLE);
|
|
|
|
//Find if it has the border
|
|
ActiveWindowNCWindowBorder = (ActiveWindowNCWindowStyle & WS_BORDER);
|
|
ActiveWindowNCPopUp = ((uint)ActiveWindowNCWindowStyle & WS_POPUP);
|
|
|
|
//if it has the border and a title execute focus
|
|
if (ActiveWindowNCWindowStyle != 0 && (((ActiveWindowNCWindowBorder == WS_BORDER) && ReadWindowTitle(hwnd) != "") || (ActiveWindowNCPopUp == WS_POPUP)))
|
|
{
|
|
if (eventType == EVENT_OBJECT_CREATE)
|
|
{
|
|
SetForegroundWindow(ncprocess.MainWindowHandle);
|
|
}
|
|
else if (eventType == EVENT_OBJECT_DESTROY)
|
|
SetWindowPos(MainViewHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Following Nc Window Method
|
|
private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
|
{
|
|
ActualHND = hwnd;
|
|
|
|
//Read the actual ID of the Process
|
|
GetWindowThreadProcessId(hwnd, out ActualPID);
|
|
|
|
//Delete the TOPMOST Style of the Window
|
|
if (NcProcessPID != 0 && ActualPID == NcProcessPID)
|
|
{
|
|
|
|
//Read the style of the window
|
|
ActiveNCWindowStyle = GetWindowLong(hwnd, GWL_STYLE);
|
|
//Find if it has the border
|
|
ActiveNCWindowBorder = (ActiveNCWindowStyle & WS_BORDER);
|
|
ActiveNCWindowNCPopUp = ((uint)ActiveWindowNCWindowStyle & WS_POPUP);
|
|
|
|
|
|
//if it has the border and a title execute focus
|
|
if (ActiveNCWindowStyle != 0 && ((ActiveNCWindowBorder == WS_BORDER && ReadWindowTitle(hwnd) != "") || (ActiveNCWindowNCPopUp == WS_POPUP)) && hwnd != ncprocess.MainWindowHandle)
|
|
{
|
|
Console.WriteLine("Handle: " + hwnd + " MainWindowHandle" + ncprocess.MainWindowHandle);
|
|
SetWindowPos(ncprocess.MainWindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
|
|
}
|
|
}
|
|
|
|
|
|
//If the PID is the STEP-HMI Process
|
|
if (ActualPID == MainProcessPID)
|
|
{
|
|
if (!IsIconic(MainViewHandle))
|
|
{
|
|
//Show Virtual keyboard
|
|
if (Config.ClientConfig.ShowVirtualKeyboard)
|
|
reOpenVirtualKeyboard();
|
|
}
|
|
}
|
|
//If the PID is the NC-HMI Process
|
|
else if ((ActualPID == NcProcessPID && NcProcessPID != 0) || (ActualPID == ProdProcessPID && ProdProcessPID != 0))
|
|
{
|
|
//Hide the TaskBar
|
|
if (Environment.OSVersion.Version.Major < 10)
|
|
HideTaskBar();
|
|
|
|
}
|
|
//If the PID is OTHER Process
|
|
else
|
|
{
|
|
//Show the TaskBar
|
|
if (Environment.OSVersion.Version.Major < 10)
|
|
ShowTaskBar();
|
|
|
|
//SetForegroundWindow(hwnd);
|
|
|
|
//Hide Virtual keyboard
|
|
if (Config.ClientConfig.ShowVirtualKeyboard && KeyboardPID != 0 && ActualPID != KeyboardPID)
|
|
closeVirtualKeyboard();
|
|
}
|
|
|
|
//Save the Last Hooked PID
|
|
LastHookedHND = ActualHND;
|
|
LastHookedPID = ActualPID;
|
|
|
|
//Save the Last TimeStamp
|
|
LastdwmsEventTime = dwmsEventTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
//Hook Keyboard Handle
|
|
private static void KeybEventProc(int nCode, IntPtr wParam, IntPtr lParam)
|
|
{
|
|
int Key = Marshal.ReadInt32(lParam);
|
|
|
|
//Elaborate Hotkey
|
|
if (wParam == HWND_KEYDOWN || wParam == HWND_SYSKEYDOWN)
|
|
{
|
|
if (Key == 0xA4 || Key == 0xA5)
|
|
altPressed = true;
|
|
if (Key == 0xA2 || Key == 0xA3)
|
|
ctrlPressed = true;
|
|
if (Key == 0xA0 || Key == 0xA1)
|
|
shiftPressed = true;
|
|
}
|
|
if (wParam == HWND_KEYUP || wParam == HWND_SYSKEYUP)
|
|
{
|
|
if (Key == 0xA4 || Key == 0xA5)
|
|
altPressed = false;
|
|
if (Key == 0xA2 || Key == 0xA3)
|
|
ctrlPressed = false;
|
|
if (Key == 0xA0 || Key == 0xA1)
|
|
shiftPressed = false;
|
|
}
|
|
|
|
//Read the actual ID of the Process
|
|
GetWindowThreadProcessId(GetForegroundWindow(), out KeybPID);
|
|
|
|
//Filter the PID
|
|
if (KeybPID == MainProcessPID || KeybPID == NcProcessPID || KeybPID == ProdProcessPID)
|
|
{
|
|
//Filter if is keydown
|
|
if (wParam == HWND_SYSKEYDOWN || wParam == HWND_KEYDOWN)
|
|
mainFrm.keyPressedHandler(altPressed, ctrlPressed, shiftPressed, Key);
|
|
}
|
|
if ((wParam == HWND_SYSKEYDOWN || wParam == HWND_KEYDOWN) && !altPressed && (KeybPID == MainProcessPID || KeybPID == ProdProcessPID) && state == NcState.SHOW)
|
|
{
|
|
//IF Siemens
|
|
if (Config.VendorHmiConfig.Type == 2)
|
|
{
|
|
if (Key >= 0x70 && Key <= 0x87)
|
|
SetForegroundWindow(ncprocess.MainWindowHandle);
|
|
}
|
|
//IF Fanuc
|
|
else if (Config.VendorHmiConfig.Type == 1)
|
|
{
|
|
if ((Key >= 0x70 && Key <= 0x87) || Key == 0xA2 || Key == 0xA3)
|
|
SetForegroundWindow(ncprocess.MainWindowHandle);
|
|
}
|
|
}
|
|
|
|
//Call Newx System Hook
|
|
CallNextHookEx(keybhook, nCode, wParam, lParam);
|
|
}
|
|
#endregion
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region PRIVATE_CUSTOM_METHODS
|
|
|
|
|
|
//Resize out of Bound window
|
|
private static void ResizeOutofBoundWindow(IntPtr ParentHandle, IntPtr WindowHandle)
|
|
{
|
|
RECT MainRct;
|
|
RECT ParentRct;
|
|
int width;
|
|
int height;
|
|
int width2;
|
|
int height2;
|
|
int newX;
|
|
int newY;
|
|
bool move = false;
|
|
bool resize = false;
|
|
|
|
if (GetWindowRect(WindowHandle, out MainRct) && GetWindowRect(ParentHandle, out ParentRct))
|
|
{
|
|
//Setup resize and move variables
|
|
width = (MainRct.Right - MainRct.Left + 1);
|
|
height = (MainRct.Bottom - MainRct.Top + 1);
|
|
width2 = (ParentRct.Right - ParentRct.Left + 1);
|
|
height2 = (ParentRct.Bottom - ParentRct.Top + 1);
|
|
newX = width2 / 2 - width / 2 + ParentRct.Left;
|
|
newY = height2 / 2 - height / 2 + ParentRct.Top;
|
|
|
|
//Check if i need to resize
|
|
if (width > width2 || height > height2)
|
|
resize = true;
|
|
|
|
//Check if i need to move
|
|
if (MainRct.Left < ParentRct.Left || MainRct.Top < ParentRct.Top || MainRct.Right > ParentRct.Right || MainRct.Bottom > ParentRct.Bottom)
|
|
move = true;
|
|
|
|
//Move or Resize the Window
|
|
if (move & !resize)
|
|
MoveWindow(WindowHandle, newX, newY, width, height, true);
|
|
else if (resize)
|
|
MoveWindow(WindowHandle, ParentRct.Left, ParentRct.Top, LastWidth, LastHeight, true);
|
|
}
|
|
}
|
|
|
|
|
|
//Read Window Title
|
|
private static string ReadWindowTitle(IntPtr hwnd)
|
|
{
|
|
int length = GetWindowTextLength(hwnd);
|
|
StringBuilder sb = new StringBuilder(length + 1);
|
|
GetWindowText(hwnd, sb, sb.Capacity);
|
|
return sb.ToString().Trim();
|
|
}
|
|
|
|
|
|
//Resize & Move NC Window
|
|
private static void ResizeAndMoveNcWindow(int X, int Y, int width, int height)
|
|
{
|
|
LastX = X;
|
|
LastY = Y;
|
|
LastWidth = width;
|
|
LastHeight = height;
|
|
|
|
//Win32 Method
|
|
if (ncprocess != null && windowstarted)
|
|
{
|
|
MoveWindow(ncprocess.MainWindowHandle, LastX, LastY, LastWidth, LastHeight, true);
|
|
}
|
|
}
|
|
|
|
|
|
//Resize & Move NC Window
|
|
private static void ResizeAndMoveProdWindow(int X, int Y, int width, int height)
|
|
{
|
|
//Win32 Method
|
|
if (prodProcess != null && prodwindowstarted)
|
|
{
|
|
MoveWindow(prodProcess.MainWindowHandle, X, Y, width, height, true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static bool isWindowReady(string Title)
|
|
{
|
|
switch (Config.VendorHmiConfig.Type)
|
|
{
|
|
case 1: return (Title.Contains(HMI_WINDOW_TITLE_FANUC) && Title.Contains(Config.VendorHmiConfig.IpAddress));
|
|
case 2: return (Title.Contains(HMI_WINDOW_TITLE_SIEMENS) || Title.Contains(HMI_WINDOW_TITLE_SIEMENS_CMS_CONTROL));
|
|
case 3: return true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
//Setup the Path/Name of the process Method
|
|
private static void SetupNcProcess()
|
|
{
|
|
switch (Config.VendorHmiConfig.Type)
|
|
{
|
|
// 0: Demo
|
|
case 0:
|
|
{
|
|
processname = DemoName;
|
|
processpath = @"C:\CMS\ACTIVE\DEMO" + DemoPath;
|
|
ncWindowWidth = HMI_WINDOW_WIDTH_DEMO;
|
|
ncWindowHeight = HMI_WINDOW_HEIGHT_DEMO;
|
|
ncWindowX = HMI_WINDOW_POS_X_DEMO;
|
|
ncWindowY = HMI_WINDOW_POS_Y_DEMO;
|
|
}; break;
|
|
|
|
// 1: Fanuc
|
|
case 1:
|
|
{
|
|
processname = FanucName;
|
|
processpath = getProgramFilesx86Path() + FanucPath;
|
|
ncWindowWidth = HMI_WINDOW_WIDTH_FANUC;
|
|
ncWindowHeight = HMI_WINDOW_HEIGHT_FANUC;
|
|
ncWindowX = HMI_WINDOW_POS_X_FANUC;
|
|
ncWindowY = HMI_WINDOW_POS_Y_FANUC;
|
|
}; break;
|
|
|
|
// 2: Siemens
|
|
case 2:
|
|
{
|
|
processname = SiemensName;
|
|
processpath = getProgramFilesx86Path() + SiemensPath;
|
|
ncWindowWidth = HMI_WINDOW_WIDTH_SIEMENS;
|
|
ncWindowHeight = HMI_WINDOW_HEIGHT_SIEMENS;
|
|
ncWindowX = HMI_WINDOW_POS_X_SIEMENS;
|
|
ncWindowY = HMI_WINDOW_POS_Y_SIEMENS;
|
|
}; break;
|
|
|
|
// 3: Osai
|
|
case 3:
|
|
{
|
|
processname = OsaiName;
|
|
processpath = getProgramFilesx86Path() + OsaiPath;
|
|
ncWindowWidth = HMI_WINDOW_WIDTH_OSAI;
|
|
ncWindowHeight = HMI_WINDOW_HEIGHT_OSAI;
|
|
ncWindowX = HMI_WINDOW_POS_X_OSAI;
|
|
ncWindowY = HMI_WINDOW_POS_Y_OSAI;
|
|
}; break;
|
|
|
|
// 4: Siemens S7
|
|
case 4:
|
|
{
|
|
processname = DemoName;
|
|
processpath = @"C:\CMS\ACTIVE\DEMO" + DemoPath;
|
|
ncWindowWidth = 0;
|
|
ncWindowHeight = 0;
|
|
ncWindowX = 0;
|
|
ncWindowY = 0; ;
|
|
//ncWindowWidth = HMI_WINDOW_WIDTH_SIEMENS;
|
|
//ncWindowHeight = HMI_WINDOW_HEIGHT_SIEMENS;
|
|
//ncWindowX = HMI_WINDOW_POS_X_SIEMENS;
|
|
//ncWindowY = HMI_WINDOW_POS_Y_SIEMENS;
|
|
}; break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Get X86 Program folder
|
|
private static string getProgramFilesx86Path()
|
|
{
|
|
if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
|
|
{
|
|
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
|
|
}
|
|
return Environment.GetEnvironmentVariable("ProgramFiles");
|
|
}
|
|
|
|
|
|
|
|
// On Process Kill
|
|
private static void OnprocessExit(object sender, EventArgs e)
|
|
{
|
|
Application.Exit();
|
|
}
|
|
|
|
|
|
|
|
//Hide All windows Process
|
|
private static void hideAllProcWindows(String ProcessName)
|
|
{
|
|
Process[] processes = Process.GetProcessesByName(ProcessName);
|
|
foreach (Process proc in processes)
|
|
{
|
|
if (proc.MainWindowHandle != IntPtr.Zero)
|
|
ShowWindow(proc.MainWindowHandle, WS_HIDE);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Kill All Process Name
|
|
private static void KillAllProcessWithName(String ProcessName)
|
|
{
|
|
Process[] procTool = Process.GetProcessesByName(ProcessName);
|
|
foreach (Process proc in procTool)
|
|
{
|
|
proc.Kill();
|
|
|
|
//Read if exists a Process with correct name
|
|
procTool = Process.GetProcessesByName(proc.ProcessName);
|
|
|
|
//Wait until the process is killed
|
|
TriedTimes = 1;
|
|
while (procTool.Length > 0 && TriedTimes < TimesToTryKill)
|
|
{
|
|
procTool = Process.GetProcessesByName(processname);
|
|
if (procTool.Length > 0)
|
|
{
|
|
Thread.Sleep(500);
|
|
TriedTimes++;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private static void ForceFocus(IntPtr Handle)
|
|
{
|
|
//If is not already handled
|
|
if (GetForegroundWindow() != Handle)
|
|
{
|
|
//If is minimized -> Show it
|
|
if (IsIconic(Handle))
|
|
ShowWindow(Handle, SW_SHOWDEFAULT);
|
|
|
|
//Set as foreground
|
|
SetForegroundWindow(Handle);
|
|
}
|
|
}
|
|
|
|
|
|
private static void RemoveFrameBorder(IntPtr Handle)
|
|
{
|
|
int style = GetWindowLong(Handle, GWL_STYLE);
|
|
SetWindowLong(Handle, GWL_STYLE, (style & ~WS_CAPTION & ~WS_THICKFRAME));
|
|
}
|
|
|
|
#endregion
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region THREAD_CYCLE_METHODS
|
|
|
|
|
|
//Image Capture Method
|
|
public static void CaptureCycle()
|
|
{
|
|
RECT rc = new RECT();
|
|
Graphics gr;
|
|
bool success = false;
|
|
Bitmap bmp;
|
|
MemoryStream m = new MemoryStream();
|
|
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
if (state == NcState.SHOWPROD)
|
|
{
|
|
Thread.BeginThreadAffinity();
|
|
m = new MemoryStream();
|
|
String Base64Capture;
|
|
if (ncprocess != null && ncprocess.MainWindowHandle != IntPtr.Zero)
|
|
{
|
|
GetWindowRect(ncprocess.MainWindowHandle, out rc);
|
|
bmp = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top);
|
|
gr = Graphics.FromImage(bmp);
|
|
success = PrintWindow(ncprocess.MainWindowHandle, gr.GetHdc(), 0);
|
|
gr.ReleaseHdc();
|
|
if (success && bmp != null)
|
|
{
|
|
bmp.Save(m, ImageFormat.Png);
|
|
Base64Capture = Convert.ToBase64String(m.ToArray());
|
|
ncCapture = "data:image/png;base64," + Base64Capture;
|
|
}
|
|
m.Dispose();
|
|
bmp.Dispose();
|
|
gr.Dispose();
|
|
}
|
|
else
|
|
ncCapture = "data:image/png;base64,";
|
|
|
|
Thread.EndThreadAffinity();
|
|
}
|
|
Thread.Sleep(500);
|
|
}
|
|
}
|
|
catch (ThreadAbortException) { }
|
|
}
|
|
|
|
|
|
//NC Following Method
|
|
public static void FollowNcBehaviour()
|
|
{
|
|
uint process = 0;
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
|
|
GetWindowThreadProcessId(GetForegroundWindow(), out process);
|
|
if (state != NcState.SHOW && state != NcState.SHOWPROD && (process == NcProcessPID || process == ProdProcessPID))
|
|
mainFrm.HideAUXWindow();
|
|
else if (state == NcState.SHOWPROD && process == NcProcessPID && prodwindowstarted)
|
|
mainFrm.ShowProdWindow();
|
|
else if (state == NcState.SHOW && process == ProdProcessPID && windowstarted)
|
|
mainFrm.ShowNCWindow();
|
|
|
|
if (Process.GetProcessesByName(processname).Length == 0)
|
|
{
|
|
//Un-parent the window
|
|
if (ncprocess.MainWindowHandle != IntPtr.Zero)
|
|
SetParent(ncprocess.MainWindowHandle, IntPtr.Zero);
|
|
|
|
//Detach the hook
|
|
if (hhook != IntPtr.Zero)
|
|
UnhookWinEvent(hhook);
|
|
|
|
//Stop the Image-Capture Thread
|
|
if (CaptureThread != null)
|
|
CaptureThread.Abort();
|
|
|
|
//Show Task Bar
|
|
ShowTaskBar();
|
|
|
|
//If the mainform is disposed do nothing
|
|
if (mainFrm.IsDisposed)
|
|
return;
|
|
|
|
//Exit from Application
|
|
if (mainFrm.InvokeRequired)
|
|
mainFrm.Invoke((MethodInvoker)delegate ()
|
|
{
|
|
mainFrm.sendClose();
|
|
});
|
|
else
|
|
{
|
|
mainFrm.sendClose();
|
|
}
|
|
}
|
|
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
catch (ThreadAbortException) { }
|
|
}
|
|
|
|
#endregion
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#region WIN32_METHODS
|
|
|
|
private const int GWL_STYLE = -16;
|
|
private const int GWL_EXSTYLE = -20;
|
|
private const int LWA_ALPHA = 0x00000002;
|
|
private const int LWA_COLORKEY = 0x00000001;
|
|
private const int WS_EX_LAYERED = 0x00080000;
|
|
private const int WS_EX_TOOLWINDOW = 0x00000080;
|
|
private const int WS_CHILD = 0x40000000;
|
|
private const int WS_BORDER = 0x00800000;
|
|
private const int WS_DLGFRAME = 0x00400000;
|
|
private const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
|
|
private const int WS_THICKFRAME = 262144;
|
|
private const int WS_HIDE = 0;
|
|
private const int WS_SHOW = 5;
|
|
private const int WS_MINIMIZE = 6;
|
|
private const int WS_SHOWNORMAL = 1;
|
|
private const int SW_SHOWMAXIMIZED = 3;
|
|
private const int SW_SHOWDEFAULT = 10;
|
|
private const int SW_SHOWNOACTIVATE = 4;
|
|
private const int WM_SETICON = 0x80;
|
|
private const int ICON_SMALL = 0;
|
|
private const int ICON_BIG = 1;
|
|
private const uint EVENT_OBJECT_FOCUS = 0x8005;
|
|
private const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
|
|
private const uint EVENT_OBJECT_CREATE = 0x8000;
|
|
private const uint EVENT_OBJECT_DESTROY = 0x8001;
|
|
private const uint WINEVENT_OUTOFCONTEXT = 0;
|
|
private const UInt32 SWP_NOSIZE = 0x0001;
|
|
private const UInt32 SWP_NOMOVE = 0x0002;
|
|
private const UInt32 SWP_NOACTIVATE = 0x0010;
|
|
private const UInt32 SWP_SHOWWINDOW = 0x0040;
|
|
private const UInt32 SWP_NOZORDER = 0x0004;
|
|
private const UInt32 SWP_NOREDRAW = 0x0008;
|
|
private const UInt32 SWP_HIDEWINDOW = 0x0080;
|
|
private const int MF_BYCOMMAND = 0x00000000;
|
|
private const int SC_CLOSE = 0xF060;
|
|
private const int SC_MINIMIZE = 0xF020;
|
|
private const int SC_MAXIMIZE = 0xF030;
|
|
private const int HIDE_CLOSE = 0x200;
|
|
private const int SC_SIZE = 0xF000;
|
|
private const int WS_SYSMENU = 0x00080000;
|
|
private const uint WS_POPUP = 0x80000000;
|
|
private const int WS_MAXIMIZE = 0x01000000;
|
|
private const int WH_KEYBOARD_LL = 13;
|
|
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
|
|
static readonly IntPtr HWND_TOP = new IntPtr(0);
|
|
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
|
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
|
|
static readonly IntPtr HWND_KEYDOWN = new IntPtr(0x100);
|
|
static readonly IntPtr HWND_SYSKEYDOWN = new IntPtr(0x104);
|
|
static readonly IntPtr HWND_KEYUP = new IntPtr(0x101);
|
|
static readonly IntPtr HWND_SYSKEYUP = new IntPtr(0x105);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
|
|
|
|
[DllImport("USER32.DLL")]
|
|
[return: MarshalAs(UnmanagedType.I4)]
|
|
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
|
|
|
[DllImport("USER32.DLL")]
|
|
[return: MarshalAs(UnmanagedType.I4)]
|
|
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool IsWindowVisible(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool IsIconic(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.I8)]
|
|
public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.I4)]
|
|
private static extern int IsZoomed(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.I4)]
|
|
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
|
static extern int GetWindowTextLength(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
|
|
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool UnhookWinEvent(IntPtr hWinEventHook);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);
|
|
delegate void keyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
|
|
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool UnhookWindowsHookEx(IntPtr idHook);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern IntPtr FindWindow(string className, string windowText);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool SetWindowText(IntPtr hWnd, string text);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
static extern IntPtr SetFocus(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr GetAncestor(IntPtr hWnd, uint uCmd);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool IsWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
|
[return: MarshalAs(UnmanagedType.I4)]
|
|
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr GetLastActivePopup(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
|
|
|
|
[DllImport("User32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool ShowOwnedPopups(IntPtr hwnd, bool show);
|
|
|
|
[DllImport("dwmapi.dll")]
|
|
static extern int DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb);
|
|
|
|
|
|
[DllImport("user32", EntryPoint = "GetMenuItemCount", SetLastError = true,
|
|
CharSet = CharSet.Unicode, ExactSpelling = true,
|
|
CallingConvention = CallingConvention.Winapi)]
|
|
private static extern int GetMenuItemCount(IntPtr hMenu);
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct RECT
|
|
{
|
|
public int Left;
|
|
public int Top;
|
|
public int Right;
|
|
public int Bottom;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|