Click here to Skip to main content
15,886,199 members
Articles / General Programming / Threads

Plugged.NET - An event based plug-in library for enterprise application integration and extensibility

Rate me:
Please Sign up or sign in to vote.
4.89/5 (19 votes)
25 Jul 2013CPOL6 min read 34K   1.4K   92  
An event based plug-in library for enterprise application integration, extensibility and cross business application management.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml.Serialization;

namespace Common.Plugin.Platform.Menu
{
    public class PluginMenuItem : IDisposable
    {
        private bool disposed;

        public string Application { get; set; }
        public string MenuText { get; set; }
        public string MenuIcon { get; set; }
        public string Verb { get; set; }
        public string VerbCanonicalName { get; set; }
        public string VerbHelpText { get; set; }
        public string VerbCommand { get; set; }

        public uint Ordinal { get; set; }

        private IntPtr? menuIconPtr = null;
        public IntPtr MenuIconPtr
        {
            get
            {
                if (menuIconPtr == null)
                {
                    menuIconPtr = IntPtr.Zero;
                    Bitmap bmp = GetBitmap(16, 16);
                    menuIconPtr = bmp.GetHbitmap();
                }

                return menuIconPtr.Value;
            }
        }

        Bitmap menuIconImage = null;
        private Bitmap GetBitmap(int width, int height)
        {
            if (!string.IsNullOrEmpty(MenuIcon) && File.Exists(MenuIcon))
            {
                if (menuIconImage != null)
                    menuIconImage.Dispose();

                if (MenuIcon.TrimEnd().EndsWith(".dll"))
                {
                    var icon = Icon.ExtractAssociatedIcon(MenuIcon.TrimEnd());
                    menuIconImage = icon.ToBitmap();
                }
                else
                {
                    // Load the bitmap for the menu item.
                    menuIconImage = Bitmap.FromFile(MenuIcon) as Bitmap;
                }

                if (menuIconImage.Width != width || menuIconImage.Height != height)
                {
                    Bitmap newImage = new Bitmap(width, height);
                    using (Graphics gr = Graphics.FromImage(newImage))
                    {
                        gr.SmoothingMode = SmoothingMode.HighQuality;
                        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        gr.DrawImage(menuIconImage, new Rectangle(0, 0, width, height));
                    }

                    menuIconImage.Dispose();
                    menuIconImage = newImage;
                }

                menuIconImage.MakeTransparent(Color.White);
            }

            return menuIconImage;
        }

        public static PluginMenuItem Parse(string xmlString)
        {
            try
            {
                var serializer = new XmlSerializer(typeof(PluginMenuItem));
                return (PluginMenuItem)serializer.Deserialize(new StringReader(xmlString));
            }
            catch
            {
                return null;
            }
        }

        public void Dispose()
        {
            if (!disposed)
            {
                if (menuIconPtr != null && menuIconPtr.Value != IntPtr.Zero)
                {
                    NativeMethods.DeleteObject(menuIconPtr.Value);
                    menuIconPtr = null;
                }
                if (menuIconImage != null)
                {
                    menuIconImage.Dispose();
                    menuIconImage = null;
                }
                disposed = true;
            }
        }
    }

    internal class NativeMethods
    {
        /// <summary>
        /// The DeleteObject function deletes a logical pen, brush, font, bitmap, 
        /// region, or palette, freeing all system resources associated with the 
        /// object. After the object is deleted, the specified handle is no longer 
        /// valid.
        /// </summary>
        /// <param name="hObject">
        /// A handle to a logical pen, brush, font, bitmap, region, or palette.
        /// </param>
        /// <returns>
        /// If the function succeeds, the return value is true.
        /// </returns>
        [DllImport("gdi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern bool DeleteObject(IntPtr hObject);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
is a poor software developer and thinker. Presently working on a theory of "complementary perception". It's a work in progress.

Comments and Discussions