Add check so that only one instance can be open at a time

This commit is contained in:
Stone-Red-Code
2021-08-25 19:08:26 +02:00
parent ae490367ee
commit 8df53ef190
2 changed files with 60 additions and 39 deletions
+28 -1
View File
@@ -1,4 +1,6 @@
using System.Windows; using System;
using System.Threading;
using System.Windows;
namespace DesktopMagic namespace DesktopMagic
{ {
@@ -7,5 +9,30 @@ namespace DesktopMagic
/// </summary> /// </summary>
public partial class App : Application 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();
}
} }
} }
+32 -38
View File
@@ -1,60 +1,54 @@
using DesktopMagicPluginAPI; using DesktopMagicPluginAPI;
using DesktopMagicPluginAPI.Inputs; using DesktopMagicPluginAPI.Inputs;
using DesktopMagicPluginAPI.Drawing;
using System.Drawing; using System.Drawing;
using System.Diagnostics; using System.Drawing.Imaging;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.IO;
using System.Collections.Generic;
namespace DesktopMagicPlugin.Test 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 List<Bitmap> bitmaps = new List<Bitmap>();
private TextBox textBox = new TextBox("abc"); //Create a text box with the specified default value.
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() 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)) frameCount++;
{
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.
}
return bmp; //Return the image. if (frameCount >= bitmaps.Count)
} frameCount = 0;
private void TextBox_OnValueChanged() return bitmaps[frameCount];
{
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();
}
} }
} }
} }