CD_Laola_Welle/LaOlaWelle/MainWindow.xaml.cs

162 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
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.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
namespace LaOlaWelle
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public int ApplicationID;
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string Befehl, string buffer, int bufferSize, IntPtr hwndCallback);
ServerGUI server;
bool serverActive = false;
public MainWindow()
{
InitializeComponent();
System.Windows.Forms.NotifyIcon Icon = new System.Windows.Forms.NotifyIcon();
Icon.BalloonTipText = "Activated";
Bitmap bmp = new Bitmap(1, 1);
bmp.SetPixel(0, 0, System.Drawing.Color.AliceBlue);
Icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetEntryAssembly().ManifestModule.Name);
Icon.ShowBalloonTip(int.MaxValue);
Icon.Visible = true;
Icon.Click += Icon_Click;
Icon.DoubleClick += Icon_DoubleClick;
}
private void Icon_DoubleClick(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
private async void button_Click(object sender, RoutedEventArgs e)
{
if (Equals(clientNumber.Text, "0"))
{
server = new ServerGUI();
server.Owner = this;
server.Show();
serverActive = true;
this.Hide();
return;
}
try {
ApplicationID = int.Parse(clientNumber.Text);
teach.IsEnabled = false;
UdpClient client = new UdpClient();
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2222);
client.Client.Bind(localEp);
IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
client.JoinMulticastGroup(multicastaddress);
await Task.Run(() =>
{
while (true)
{
var data = client.Receive(ref localEp);
using (var stream = new MemoryStream(data))
{
stream.Position = 0;
using (var reader = new BinaryReader(stream))
{
var message = new Message();
if (!Equals(reader.ReadString(), message.Valid))
{
return;
}
if (reader.ReadInt32() == ApplicationID) {
OpenCloseCD((state)reader.ReadInt32());
}
}
}
}
});
} catch (Exception) {
MessageBox.Show("Eingabe ungültig");
return;
}
}
private void OpenCloseCD(state state)
{
if(state == state.open)
{
mciSendString("set CDAudio door open", null, 127, (IntPtr)0);
} else
{
mciSendString("set CDAudio door closed", null, 127, (IntPtr)0);
}
}
private void Icon_Click(object sender, EventArgs e)
{
if (serverActive)
{
if (!server.IsVisible)
{
server.Show();
}
if (server.WindowState == WindowState.Minimized)
{
server.WindowState = WindowState.Normal;
}
server.Activate();
server.Topmost = true;
server.Topmost = false;
server.Focus();
return;
}
if (!this.IsVisible)
{
this.Show();
}
if(this.WindowState == WindowState.Minimized)
{
this.WindowState = WindowState.Normal;
}
this.Activate();
this.Topmost = true;
this.Topmost = false;
this.Focus();
}
}
}