mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 00:46:12 +02:00
Add check so that only one instance can be open at a time
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System.Windows;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
|
||||
namespace DesktopMagic
|
||||
{
|
||||
@@ -7,5 +9,30 @@ namespace DesktopMagic
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Bitmap> bitmaps = new List<Bitmap>();
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user