Clone
13
Guide
Stone_Red edited this page 2023-01-25 12:25:47 +01:00

How to create a basic plugin

  1. Create a .NET (Core) class library project

  2. Add the DesktopMagicPluginAPI nuget package

  3. Create a class that inherits from DesktopMagicPluginAPI.Plugin

  4. Implement the required members from DesktopMagicPluginAPI.Plugin

  5. Compile it to a .dll file.

  6. In the plugin directory, create a directory with the same name as the .dll file without the file extension and put the .dll file with all required dependencies into it.

  7. Add the plugin to the DesktopMagic-Plugins repository.

General information

Tip: Use Bitmap.SetResolution to avoid scaling problems on different screens.

The Main method

The Main method is where the main processing of the plugin should take place.
The method is executed in a different thread, which means you can run things with heavy load there without affecting the main application or other plugins.
You must return a bitmap from this method to display the output in the plugin window.

Event handlers

All events are usually raised in the main thread of the DesktopMagic application, so they should not contain code that blocks the main thread.

Examples

Minimum example

This is a very basic plugin that will display a constant string.

using DesktopMagicPluginAPI;
using System.Diagnostics;
using System.Drawing;

namespace DesktopMagicPlugin
{
    public class MyFirstPlugin : Plugin
    {
        public override Bitmap Main()
        {
            Bitmap bmp = new Bitmap(1000, 1000);

            using(Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Application.Color); //Set the background color to the color specified in the Desktop Magic application.
                
                g.DrawString("Hello", new Font(Application.Font, 100), Brushes.Black, new PointF(0, 0)); //Draw the "Hello" to the image.
            }

            bmp.SetResolution(300, 300); //Set DPI to avoid scaling issues.

            return bmp; //Return the image.
        }
    }
}

Input example

In this example, the user can enter a string that will be displayed on the image.

using DesktopMagicPluginAPI;
using DesktopMagicPluginAPI.Inputs;
using System.Drawing;

namespace DesktopMagicPlugin
{
    public class InputExamplePlugin : Plugin
    {
        public override int UpdateInterval { get; set; } = 0; //Set the update interval to 0 so that the window will not be refreshed automatically.

        [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.

        public override void Start() //The "Start" metod is called once when the plugin is loaded.
        {
            textBox.OnValueChanged += TextBox_OnValueChanged; //Add an event handler to the "OnValueChanged" event.
        }

        private void TextBox_OnValueChanged()
        {
            Application.UpdateWindow(); //Update the plugin window. (Calls the "Main" method.)
        }

        public override Bitmap Main()
        {
            Bitmap bmp = new Bitmap(1000, 1000);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Application.Color); //Set the background color to the color specified in the Desktop Magic application.

                g.DrawString(textBox.Value, new Font(Application.Font, 100), Brushes.Black, new PointF(0, 0)); //Draw the value of the text box to the image.
            }

            bmp.SetResolution(300, 300); //Set DPI to avoid scaling issues.

            return bmp; //Return the image.
        }
    }
}