Squashed 'FSI.Lib/' content from commit dceb500

git-subtree-dir: FSI.Lib
git-subtree-split: dceb5008a2176c2b8ab5e55a73b1c25d31a7f841
This commit is contained in:
maier_S
2022-03-14 11:02:41 +01:00
commit 1a74bce2ad
51 changed files with 6124 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<Window x:Class="FSI.Lib.Guis.SieTiaWinCCMsgMgt.FrmMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FSI.Lib.Guis.SieTiaWinCCMsgMgt"
mc:Ignorable="d"
Title="FSI WinCC Mgt"
SizeToContent="WidthAndHeight"
Height="Auto"
Width="Auto"
MinWidth="200"
Icon="../../Icons/FondiumU.ico"
WindowStyle="ToolWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="30"
Height="*" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnStart"
Content="Start"
Margin="5 5 5 5 "
Click="btnStart_Click" />
<Button x:Name="btnStop"
Content="Stop"
Margin="5 5 5 5 "
Click="btnStop_Click" />
</StackPanel>
<StatusBar Grid.Row="2">
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem>
<TextBlock x:Name="tbStatus" />
</StatusBarItem>
<StatusBarItem Grid.Column="1">
<ProgressBar x:Name="pgProgress"
Width="80"
Height="18" />
</StatusBarItem>
</StatusBar>
</Grid>
</Window>

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace FSI.Lib.Guis.SieTiaWinCCMsgMgt
{
/// <summary>
/// Interaktionslogik für FrmMain.xaml
/// </summary>
public partial class FrmMain : Window
{
public WinCC WinCC { get; set; }
public FrmMain()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
WinCC.Start();
CtrlMgt();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
WinCC.Stop();
CtrlMgt();
}
private void CtrlMgt()
{
if (WinCC.Status)
{
btnStart.IsEnabled = false;
btnStop.IsEnabled = true;
pgProgress.IsIndeterminate = true;
}
else
{
btnStart.IsEnabled = true;
btnStop.IsEnabled = false;
pgProgress.IsIndeterminate = false;
}
}
}
}

View File

@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FSI.Lib.Guis.SieTiaWinCCMsgMgt
{
public class WinCC
{
[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, UInt32 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;
public bool AutoStart { get; set; }
public int UpdateIntervall { get; set; }
public string WindowsName { get; set; }
public string WindowsClassName { get; set; }
public string ButtonName { get; set; }
public WinCC()
{
backgroundWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
backgroundWorker.DoWork += BackgroundWorker_DoWork;
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
if (AutoStart)
{
Start();
}
}
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.
System.Threading.Thread.Sleep(UpdateIntervall);
// Find windos by Name
var windowHandle = FindWindow(WindowsClassName, WindowsName);
if (windowHandle != IntPtr.Zero && IsWindowVisible(windowHandle))
{
SetForegroundWindow(windowHandle);
var btnHandle = FindWindowEx(windowHandle, IntPtr.Zero, null, ButtonName);
SendMessage(btnHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
public void Start()
{
if (backgroundWorker.IsBusy != true)
{
backgroundWorker.RunWorkerAsync();
}
Status = true;
}
public void Stop()
{
if (backgroundWorker.WorkerSupportsCancellation == true)
{
backgroundWorker.CancelAsync();
}
Status = false;
}
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;
}
}
}
}