Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Win32

.NET Shell Extensions - Shell Preview Handlers

Rate me:
Please Sign up or sign in to vote.
4.93/5 (23 votes)
20 May 2014MIT8 min read 140.1K   6.1K   71  
Quickly create Shell Preview Handlers for Windows or Outlook using .NET!
using System;
using System.Collections.Generic;
using System.Linq;
using SharpShell.Attributes;
using SharpShell.Diagnostics;
using SharpShell.Interop;

namespace SharpShell.SharpPropertySheet
{
    /// <summary>
    /// SharpPropertySheet is the base class for Shell Property Sheet Extensions supported
    /// by SharpShell.
    /// </summary>
    [ServerType(ServerType.ShellPropertySheet)]
    public abstract class SharpPropertySheet : ShellExtInitServer, IShellPropSheetExt
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SharpPropertySheet"/> class.
        /// </summary>
        protected SharpPropertySheet()
        {
            //  Log the event.
            Log("Constructing property sheet.");

            //  The lazy property sheet pages will be created by the abstract 
            //  create pages function.
            propertySheetPages = new Lazy<List<SharpPropertyPage>>( () => CreatePages().ToList());
        }

       #region Implementation of IShellPropSheetExt

        /// <summary>
        /// Adds the pages.
        /// </summary>
        /// <param name="pfnAddPage">The PFN add page.</param>
        /// <param name="lParam">The l param.</param>
        /// <returns></returns>
        int IShellPropSheetExt.AddPages(IntPtr pfnAddPage, IntPtr lParam)
        {
            //  Log the event.
            Log("Adding Pages...");

            //  Create the bridge.
            var bridge = new NativeBridge.NativeBridge();
            
            //  Initialise it.
            if(bridge.Initialise() == false)
            {
                Logging.Error("Failed to initialise the NativeBridge.", null);
                return 0;
            }

            //  Go through each page - that has a property page.
            foreach (var page in propertySheetPages.Value.Where(p => p.Handle != IntPtr.Zero))
            {
                //  Create a property page proxy for this page.
                var proxy = new PropertyPageProxy(this, page);

                //  Using the proxy, create the property page handle.
                proxy.CreatePropertyPageHandle(bridge);
                var propertyPageHandle = proxy.HostWindowHandle;

                //  Name the page, to aid with debugging.
                User32.SetWindowText(propertyPageHandle, "SharpShell Host for " + page.GetType().Name);
                
                //  Log the event.
                Log("Created Page Proxy, handle is " + propertyPageHandle.ToString("x8"));

                //  Now that we have the page handle, add the page via the callback.
                bridge.CallAddPropSheetPage(pfnAddPage, propertyPageHandle, lParam);
            }

            //  Release the bridge.
            //bridge.Deinitialise();

            //  Log the event.
            Log("Adding Pages (Done)");

            //  We've succeeded.
            return WinError.S_OK;
        }

        /// <summary>
        /// Replaces the page.
        /// </summary>
        /// <param name="uPageID">The u page ID.</param>
        /// <param name="lpfnReplacePage">The LPFN replace page.</param>
        /// <param name="lParam">The l param.</param>
        /// <returns></returns>
        int IShellPropSheetExt.ReplacePage(uint uPageID, AddPropertySheetPageDelegate lpfnReplacePage, IntPtr lParam)
        {
            return 0;
        }

        #endregion

        /// <summary>
        /// Determines whether this instance can show a shell property sheet, given the specified selected file list.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance should show a shell property sheet for the specified file list; otherwise, <c>false</c>.
        /// </returns>
        protected abstract bool CanShowSheet();

        /// <summary>
        /// Creates the pages.
        /// </summary>
        /// <returns>The property sheet pages.</returns>
        protected abstract IEnumerable<SharpPropertyPage> CreatePages();

        /// <summary>
        /// The lazy property sheet pages, only created when we actually need them.
        /// </summary>
        private readonly Lazy<List<SharpPropertyPage>> propertySheetPages;
        
        /// <summary>
        /// Gets the pages.
        /// </summary>
        public IEnumerable<SharpPropertyPage> Pages
        {
            get { return propertySheetPages.Value; }
        }
    }
}

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 MIT License


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions