diff --git a/src/DesktopMagic/App.xaml.cs b/src/DesktopMagic/App.xaml.cs index 47f0e38..4a87422 100644 --- a/src/DesktopMagic/App.xaml.cs +++ b/src/DesktopMagic/App.xaml.cs @@ -1,4 +1,6 @@ -using System.Windows; +using System; +using System.Threading; +using System.Windows; namespace DesktopMagic { @@ -7,5 +9,30 @@ namespace DesktopMagic /// public partial class App : Application { + private Mutex _mutex; + + public App() + { + // Try to grab mutex + bool createdNew; + _mutex = new Mutex(true, $"Stone_Red{DesktopMagic.MainWindow.AppName}", out createdNew); + + //check if creating new was succesfull + if (!createdNew) + { + //Shutdown Aplication + Current.Shutdown(); + } + else + { + // Add Event handler to exit event. + Exit += CloseMutexHandler; + } + } + + protected virtual void CloseMutexHandler(object sender, EventArgs e) + { + _mutex?.Close(); + } } } \ No newline at end of file diff --git a/src/DesktopMagicPlugin.Test/PluginScript.cs b/src/DesktopMagicPlugin.Test/PluginScript.cs index a5deb03..090bdcc 100644 --- a/src/DesktopMagicPlugin.Test/PluginScript.cs +++ b/src/DesktopMagicPlugin.Test/PluginScript.cs @@ -1,60 +1,54 @@ using DesktopMagicPluginAPI; using DesktopMagicPluginAPI.Inputs; -using DesktopMagicPluginAPI.Drawing; using System.Drawing; -using System.Diagnostics; +using System.Drawing.Imaging; using System.Drawing.Drawing2D; +using System.IO; +using System.Collections.Generic; namespace DesktopMagicPlugin.Test { - public class InputExamplePlugin : Plugin + public class GifPlugin : Plugin { - public override int UpdateInterval { get; set; } = 0; + [Element("Gif path:")] + private TextBox input = new TextBox(""); - [Element("Text:")] //Mark the property as element with the specified description - private TextBox textBox = new TextBox("abc"); //Create a text box with the specified default value. + private List bitmaps = new List(); - private Color color = Color.Black; + public override int UpdateInterval { get; set; } = 100; + private int frameCount = -1; - public override void Start() + public GifPlugin() { - textBox.OnValueChanged += TextBox_OnValueChanged; //Add an event handler to the "OnValueChanged" event. + input.OnValueChanged += Input_OnValueChanged; + } + + private void Input_OnValueChanged() + { + if (File.Exists(input.Value)) + { + Image gif = Image.FromFile(input.Value); + bitmaps.Clear(); + for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++) + { + gif.SelectActiveFrame(FrameDimension.Time, i); + + bitmaps.Add(new Bitmap(gif)); + } + } } public override Bitmap Main() { - Bitmap bmp = new Bitmap(1000, 1000); + if (bitmaps.Count == 0) + return new Bitmap(1, 1); - using (Graphics g = Graphics.FromImage(bmp)) - { - g.InterpolationMode = InterpolationMode.HighQualityBilinear; - g.PixelOffsetMode = PixelOffsetMode.HighQuality; - g.Clear(color); - g.DrawStringFixedWidth(textBox.Value, new Font(Application.Font, 100), Brushes.Black, new PointF(0, 0), 120); //Draw the value of the text box to the image. - } + frameCount++; - return bmp; //Return the image. - } + if (frameCount >= bitmaps.Count) + frameCount = 0; - private void TextBox_OnValueChanged() - { - Application.UpdateWindow(); //Update the plugin window. (Calls the "Main" method.) - } - - public override void OnMouseMove(Point position) - { - Debug.WriteLine(position.X); - Debug.WriteLine(500); - if (500 < position.X && color == Color.Black) - { - color = Color.White; - Application.UpdateWindow(); - } - else if (500 > position.X && color == Color.White) - { - color = Color.Black; - Application.UpdateWindow(); - } + return bitmaps[frameCount]; } } } \ No newline at end of file