AddIn: Hello World! sample (Windows)

Hier diskutieren die Betatester von PhotoLine untereinander und mit den Entwicklern
Antworten
Benutzeravatar
photoken
Mitglied
Beiträge: 2162
Registriert: Sa 28 Sep 2013 01:25

AddIn: Hello World! sample (Windows)

Beitrag von photoken »

Uses PL 20.40b11, at a minimum.

I've been having a great time working with the new scripting capabilities in PL, and here's the first result -- a "Hello World!" sample AddIn. What could be more appropriate for a first trial sample than "Hello World"? :wink:

Here's the link to the compiled executable and its supporting files:
https://www.dropbox.com/s/pbubo4tvzjj0j ... d.zip?dl=0
Simply extract the content to a convenient location, and (for the time being) create an External Program entry for the executable, using a File Type of "None".

The AddIn is quite silly and does only one thing: it creates a text layer with "Hello World! from PhotoLine" as the content. It uses 72pt Arial Boldface, and white as the text colour.

The AddIn does do some interesting things --
  • It is localized for the five "EFIGS" languages (English, French, Italian, German, and Spanish), which means that the default "Hello World" text will appear in your language if your system uses one of those five.
  • It allows you to change the text that will be created, and automatically persists your text to be used the next time.
Those are definitely overkill for a little "Hello World" AddIn, but those features will come in handy for more complex AddIns.

Only one warning: Always close the AddIn before closing PhotoLine. If you don't, PL will not start again. You'll need to open Task Manager, scroll down the list of Background Processes, and kill the PhotoLine task.

I'll be more than happy to provide the complete Visual Studio solution, but I'm waiting for one more scripting feature to be implemented (the paragraph vertical alignment) before doing that. In the meantime, if anyone out there is interested, here's the Form1.cs code that does all the work:

Code: Alles auswählen

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HelloWorld {
	public partial class Form1 : Form {
		public Form1() {

			InitializeComponent();

			// Initialize a ResourceManager to retrieve localized strings.
			ResourceManager locRM = new ResourceManager("HelloWorld.Resource1", typeof(Form1).Assembly);

			// Populate the text box with either the default text or the saved user's text.
			if (Properties.Settings.Default.UserText == null || Properties.Settings.Default.UserText == "") {
				textBox1.Text = locRM.GetString("DefaultText");
			} else {
				textBox1.Text = Properties.Settings.Default.UserText;
			}
		}

		/// <summary>
		/// Create a text layer with the specified content and formatting.
		/// </summary>
		/// <param name="doc">The active document/image.</param>
		/// <param name="selectedlayer">The selected layer.</param>
		public void CreateTextLayer(PhotoLine.document doc, PhotoLine.layer selectedlayer) {

			// Initialize the arrays and variables that will be needed.
			int[] charrange = new int[] { 0, 1 };
			int[] RGBvalue = new int[] { 255, 255, 255 };
			string testtext;

			// Initialize the new text layer.
			PhotoLine.Text targetlayer = new PhotoLine.Text();

			// Get the text in the textbox.
			testtext = textBox1.Text;

			// Create the font attributes object.
			PhotoLine.Dictionary fontAttribute = new PhotoLine.Dictionary();
			fontAttribute.Add("FamilyName", "Arial");
			fontAttribute.Add("Size", 72);
			fontAttribute.Add("Weight", 700);

			// Create the paragraph text horizontal alignment object.
			PhotoLine.Dictionary paragraphHorizAttribute = new PhotoLine.Dictionary();
			paragraphHorizAttribute.Add("Alignment", PhotoLine.ParagraphAlignment.PACenter);

			// Set text of the new layer and the range of text characters that will be affected.
			targetlayer.Text = testtext;
			charrange[1] = targetlayer.TextLength;

			// Apply the paragraph and font attributes to the affected text characters.
			targetlayer.SetAttribute(charrange, "Paragraph", paragraphHorizAttribute);
			targetlayer.SetAttribute(charrange, "Color", RGBvalue);
			targetlayer.SetAttribute(charrange, "Font", fontAttribute);

			// Name the new text layer, size it to the document size and create the text layer.
			targetlayer.Name = "C# Add-In";
			targetlayer.Size = doc.Size;
			selectedlayer.Parent.Insert(targetlayer, selectedlayer);
		}

		/// <summary>
		/// Close the AddIn.
		/// </summary>
		public static void CloseAddIn() {
			Form1.ActiveForm.Dispose();
		}

		private void OKbutton_Click(object sender, EventArgs e) {

			PhotoLine.Application app = new PhotoLine.Application();

			if (app != null) {
				PhotoLine.document doc = app.ActiveDocument;

				if (doc != null) {
					PhotoLine.layer selectedlayer = (PhotoLine.layer)doc.ActivePage.ActiveLayer;

					if (selectedlayer != null) {
						try {
							CreateTextLayer(doc, selectedlayer);
						} catch (Exception appe) {
							MessageBox.Show(appe.InnerException.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
							throw;
						} finally {
							// Save the user's text and close the AddIn.
							Properties.Settings.Default.UserText = textBox1.Text;
							Properties.Settings.Default.Save();
							CloseAddIn();
						}
					}
				}
			}
		}
	}
}
Ken
Yes, I think it can be eeeeeasily done....
Just take everything out on Highway 61.
Antworten