235 lines
6.6 KiB
C#
235 lines
6.6 KiB
C#
using FSI.Lib.Guis.SieTiaWinCCMsgMgt.Model;
|
|
using FSI.Lib.MVVM;
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
|
|
namespace FSI.Lib.Guis.SieTiaWinCCMsgMgt.ViewModel
|
|
{
|
|
public class ViewModelWinCC : MVVM.ViewModelBase
|
|
{
|
|
[DllImport("user32.dll")]
|
|
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
|
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr PostMessage(IntPtr hwndParent, int msg, int wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
static extern bool IsWindowVisible(IntPtr hWnd);
|
|
|
|
const int BM_CLICK = 0x00F5; //message for Click which is a constant
|
|
const int WM_LBUTTONDOWN = 0x0201; //message for Mouse down
|
|
const int WM_LBUTTONUP = 0x0202; // message for Mouse up
|
|
|
|
private CancellationTokenSource tokenSource = null;
|
|
private CancellationToken token;
|
|
private Task task = null;
|
|
|
|
|
|
private BackgroundWorker backgroundWorker;
|
|
private ICommand _cmdStart;
|
|
private ICommand _cmdStop;
|
|
private WinCC _winCC;
|
|
|
|
public ViewModelWinCC()
|
|
{
|
|
WinCC = new Model.WinCC();
|
|
Init();
|
|
}
|
|
|
|
public ViewModelWinCC(bool autoStart, int updateIntervall, string windowsName, string windowsClassName, string buttonName)
|
|
{
|
|
_winCC = new Model.WinCC
|
|
{
|
|
AutoStart = autoStart,
|
|
UpdateIntervall = updateIntervall,
|
|
WindowsName = windowsName,
|
|
WindowsClassName = windowsClassName,
|
|
ButtonName = buttonName
|
|
};
|
|
|
|
//_seletctedWinCC = WinCC;
|
|
|
|
Init();
|
|
|
|
if (WinCC.AutoStart)
|
|
{
|
|
ExecuteStart(null);
|
|
}
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
_cmdStart = new RelayCommand<object>(ExecuteStart, CanExecuteStart);
|
|
_cmdStop = new RelayCommand<object>(ExecuteStop, CanExecuteStop);
|
|
|
|
backgroundWorker = new BackgroundWorker
|
|
{
|
|
WorkerReportsProgress = true,
|
|
WorkerSupportsCancellation = true
|
|
};
|
|
backgroundWorker.DoWork += BackgroundWorker_DoWork;
|
|
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
|
|
}
|
|
|
|
public Model.WinCC WinCC
|
|
{
|
|
get
|
|
{
|
|
return _winCC;
|
|
}
|
|
set
|
|
{
|
|
if (_winCC != value & _winCC != null)
|
|
{
|
|
_winCC = value;
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public ICommand CmdStart
|
|
{
|
|
get { return _cmdStart; }
|
|
set => _cmdStart = value;
|
|
}
|
|
|
|
private bool CanExecuteStop(object obj)
|
|
{
|
|
return Status;
|
|
}
|
|
|
|
private void ExecuteStop(object obj)
|
|
{
|
|
if (backgroundWorker.WorkerSupportsCancellation == true)
|
|
{
|
|
backgroundWorker.CancelAsync();
|
|
}
|
|
Status = false;
|
|
}
|
|
|
|
public ICommand CmdStop
|
|
{
|
|
get { return _cmdStop; }
|
|
set => _cmdStop = value;
|
|
}
|
|
|
|
|
|
private bool CanExecuteStart(object obj)
|
|
{
|
|
return !Status;
|
|
}
|
|
|
|
private void ExecuteStart(object obj)
|
|
{
|
|
if (backgroundWorker.IsBusy != true)
|
|
{
|
|
backgroundWorker.RunWorkerAsync();
|
|
}
|
|
Status = true;
|
|
}
|
|
|
|
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
BackgroundWorker worker = sender as BackgroundWorker;
|
|
|
|
while (true)
|
|
{
|
|
if (worker.CancellationPending == true)
|
|
{
|
|
e.Cancel = true;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
// Perform a time consuming operation and report progress.
|
|
Thread.Sleep(WinCC.UpdateIntervall);
|
|
|
|
// Find windos by Name
|
|
var windowHandle = FindWindow(WinCC.WindowsClassName, WinCC.WindowsName);
|
|
|
|
if (windowHandle != IntPtr.Zero && IsWindowVisible(windowHandle))
|
|
{
|
|
SetForegroundWindow(windowHandle);
|
|
var btnHandle = FindWindowEx(windowHandle, IntPtr.Zero, null, WinCC.ButtonName);
|
|
SendMessage(btnHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public bool Status { get; set; }
|
|
|
|
private void WinCCMsgMgt()
|
|
{
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
if (token.IsCancellationRequested)
|
|
{
|
|
token.ThrowIfCancellationRequested();
|
|
}
|
|
//MessageBox.Show("13456");
|
|
Thread.Sleep(5000);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
bool disposedValue = false;
|
|
|
|
try
|
|
{
|
|
if (!disposedValue)
|
|
{
|
|
if (disposing)
|
|
{
|
|
tokenSource.Cancel();
|
|
task.Wait();
|
|
tokenSource.Dispose();
|
|
task.Dispose();
|
|
}
|
|
|
|
disposedValue = true;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
finally
|
|
{
|
|
tokenSource = null;
|
|
task = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|