SixPairs

August 28, 2013

Hide a ToolWindow at VSShell Startup

Filed under: VSShell — Tags: — Ceyhun Ciper @ 21:29
using System;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using EnvDTE80;

namespace Company.VSPackage1
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [Guid(GuidList.guidVSPackage1PkgString)]
    [ProvideAutoLoad(UIContextGuids80.NoSolution)]
    public sealed class VSPackage1Package : Package, IVsShellPropertyEvents
    {
        private uint m_EventSinkCookie;

        protected override void Initialize()
        {
            base.Initialize();
        
            // Set an event listener for shell property changes
            var shellService = GetService(typeof(SVsShell)) as IVsShell;
            ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this, out m_EventSinkCookie));
        }

        public int OnShellPropertyChange(int propid, object val)
        {
            // We handle the event if zombie state changes from true to false
            if (propid == (int)__VSSPROPID.VSSPROPID_Zombie)
            {
                if ((bool)val == false)
                {
                    DTE2 dte = GetService(typeof(SDTE)) as DTE2;
                    dte.ExecuteCommand("View.SolutionExplorer");
                    dte.ExecuteCommand("Window.Hide");

                    // Unsubscribe from events
                    var shellService = GetService(typeof(SVsShell)) as IVsShell;
                    ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(m_EventSinkCookie));
                    m_EventSinkCookie = 0;

                }
            } 
            
            return VSConstants.S_OK;
        }
    }
}

August 23, 2013

Load a ToolWindow at VSShell Startup

Filed under: VSShell — Tags: — Ceyhun Ciper @ 11:45

Original ToolBar-based article..

using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace Company.VSPackage1
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [ProvideToolWindow(typeof(MyToolWindow))]
    [Guid(GuidList.guidVSPackage1PkgString)]
    [ProvideAutoLoad(UIContextGuids80.NoSolution)]
    public sealed class VSPackage1Package : Package, IVsShellPropertyEvents
    {
        private uint m_EventSinkCookie;

        private void ShowToolWindow() { ShowToolWindow(null, null); }
        private void ShowToolWindow(object sender, EventArgs e)
        {
            ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }

        protected override void Initialize()
        {
            base.Initialize();

            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            CommandID toolwndCommandID = new CommandID(GuidList.guidVSPackage1CmdSet, (int)PkgCmdIDList.cmdidMyTool);
            mcs.AddCommand(new MenuCommand(ShowToolWindow, toolwndCommandID));

            // Set an event listener for shell property changes
            var shellService = GetService(typeof(SVsShell)) as IVsShell;
            ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this, out m_EventSinkCookie));
        }

        public int OnShellPropertyChange(int propid, object val)
        {
            // We handle the event if zombie state changes from true to false
            if (propid == (int)__VSSPROPID.VSSPROPID_Zombie)
            {
                if ((bool)val == false)
                {
                    ShowToolWindow();

                    // Unsubscribe from events
                    var shellService = GetService(typeof(SVsShell)) as IVsShell;
                    ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(m_EventSinkCookie));
                    m_EventSinkCookie = 0;
                }
            }
            return VSConstants.S_OK;
        }
    }
}

Create a free website or blog at WordPress.com.