Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / Win32

.NET Shell Extensions - Shell Info Tip Handlers

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
5 Apr 2013CPOL7 min read 54.6K   2K   44  
Rapidly create Shell Info Tip Extensions using .NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using SharpShell.Diagnostics;
using SharpShell.Extensions;
using SharpShell.Interop;

namespace SharpShell.SharpPropertySheet
{
    /// <summary>
    /// The PropertyPageProxy is the object used to pass data between the 
    /// shell and the SharpPropertyPage.
    /// </summary>
    internal class PropertyPageProxy
    {
        /// <summary>
        /// Prevents a default instance of the <see cref="PropertyPageProxy"/> class from being created.
        /// </summary>
        private PropertyPageProxy()
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyPageProxy"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="propertyPage">The target property page.</param>
        internal PropertyPageProxy(SharpPropertySheet parent, ISharpPropertyPage propertyPage)
        {
            //  Set the target.
            Parent = parent;
            Target = propertyPage;

            //  Create the dialog proc delegate (as a class member so it won't be garbage collected).
            dialogProc = new DialogProc(WindowProc);
            callbackProc = new PropSheetCallback(CallbackProc);
        }

        /// <summary>
        /// The WindowProc. Called by the shell and must delegate messages via the proxy to the user control.
        /// </summary>
        /// <param name="hWnd">The h WND.</param>
        /// <param name="uMessage">The u message.</param>
        /// <param name="wParam">The w param.</param>
        /// <param name="lParam">The l param.</param>
        /// <returns></returns>
        private bool WindowProc(IntPtr hWnd, uint uMessage, IntPtr wParam, IntPtr lParam)
        {
            switch (uMessage)
            {
                case WindowsMessages.WM_INITDIALOG:

                    Logging.Log("WM_INITDIALOG Start");

                    try
                    {
                        //  Set the parent of the property page to the host.
                        User32.SetParent(Target.Handle, hWnd);

                        //  Update the page.
                        Target.OnPageInitialised(Parent);
                    }
                    catch (Exception exception)
                    {
                        Logging.Error("Failed to set the parent to the host.", exception);
                    }

                    Logging.Log("WM_INITDIALOG End");

                    break;
            }

            return false;
        }

        /// <summary>
        /// The CallbackProc. Called by the shell to inform of key property page events.
        /// </summary>
        /// <param name="hWnd">The h WND.</param>
        /// <param name="uMsg">The u MSG.</param>
        /// <param name="ppsp">The PPSP.</param>
        /// <returns></returns>
        private uint CallbackProc(IntPtr hWnd, uint uMsg, ref PROPSHEETPAGE ppsp)
        {
            switch ((PSPCB)uMsg)
            {
                case PSPCB.ADDREF:

                    break;

                case PSPCB.RELEASE:

                    break;

                case PSPCB.CREATE:

                    //  Allow the sheet to be created.
                    return 1;
            }
            return 0;
        }
        
        /// <summary>
        /// Creates the property page handle.
        /// </summary>
        public void CreatePropertyPageHandle(NativeBridge.NativeBridge nativeBridge)
        {
            Logging.Log("Creating property page handle via bridge.");

            //  Create a prop sheet page structure.
            var psp = new PROPSHEETPAGE();

            //  Set the key properties.
            psp.dwSize = Marshal.SizeOf(psp);
            //psp.dwFlags = PSP.USETITLE | PSP.USECALLBACK/* | PSP.DEFAULT |*/| PSP.DLGINDIRECT;
            //psp.dwFlags = PSP.DEFAULT | PSP.USETITLE | PSP.DLGINDIRECT;
            //psp.hInstance = nativeBridge.GetInstanceHandle();

            psp.hInstance = nativeBridge.GetInstanceHandle();
            psp.dwFlags = PSP.DEFAULT| PSP.USETITLE | PSP.USECALLBACK;
            Logging.Log("Getting proxy host...");
            psp.pTemplate = nativeBridge.GetProxyHostTemplate();
            psp.pfnDlgProc = dialogProc;
            psp.pcRefParent = 0;
            psp.pfnCallback = callbackProc;
            psp.lParam = IntPtr.Zero;

            //  If we have a title, set it.
            if (!string.IsNullOrEmpty(Target.Title))
            {
                psp.dwFlags |= PSP.USETITLE;
                psp.pszTitle = Target.Title;
            }

            //  If we have an icon, set it.
            if (Target.Icon != null && Target.Icon.Handle != IntPtr.Zero)
            {
                psp.dwFlags |= PSP.USEHICON;
                psp.hIcon = Target.Icon.Handle;
            }

            //  Create a the property sheet page.
            HostWindowHandle = Comctl32.CreatePropertySheetPage(ref psp);

            //  Log the host window handle.
            Logging.Log("Created Proxy Host: " + HostWindowHandle.ToString("X8"));
        }

        /// <summary>
        /// The dialog proc.
        /// </summary>
        private readonly DialogProc dialogProc;

        /// <summary>
        /// The callback proc.
        /// </summary>
        private readonly PropSheetCallback callbackProc;

        public SharpPropertySheet Parent { get; set; }

        /// <summary>
        /// Gets the property page.
        /// </summary>
        /// <value>
        /// The property page.
        /// </value>
        public ISharpPropertyPage Target { get; private set; }

        /// <summary>
        /// Gets the host window handle.
        /// </summary>
        /// <value>
        /// The host window handle.
        /// </value>
        public IntPtr HostWindowHandle { get; private set; }
    }
}

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
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions