PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSVSPackageWPFToolWindow/CSVSPackageWPFToolWindowPackage.cs

#
C# | 127 lines | 50 code | 8 blank | 69 comment | 6 complexity | 9905a45494467bbfe92d1b54b75c073a MD5 | raw file
  1/***************************************** Module Header *****************************\
  2* Module Name:  CSVSPackageWPFToolWindowPackage.cs
  3* Project:      CSVSPackageWPFToolWindow
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* This sample demostrate how to host a WPF control into Visual Studio 
  7* ToolWindow. 
  8*
  9* To use the sample
 10* 1. Run the sample
 11* 2. Open tool window by View / Other Windows / WPFToolWindow
 12* 3. A tool window will be docked at place of solution explorer
 13* 4. The tool window hosts a WPF control which represents MyDocuments folder
 14* structure
 15* 
 16* This source is subject to the Microsoft Public License.
 17* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 18* All other rights reserved.
 19* 
 20* History:
 21* * 03/16/2010 04:35 PM Hongye Sun Created
 22\*************************************************************************************/
 23
 24using System;
 25using System.Diagnostics;
 26using System.Globalization;
 27using System.Runtime.InteropServices;
 28using System.ComponentModel.Design;
 29using Microsoft.Win32;
 30using Microsoft.VisualStudio.Shell.Interop;
 31using Microsoft.VisualStudio.OLE.Interop;
 32using Microsoft.VisualStudio.Shell;
 33
 34namespace Company.VSPackageWPFToolWindow
 35{
 36    /// <summary>
 37    /// This is the class that implements the package exposed by this assembly.
 38    ///
 39    /// The minimum requirement for a class to be considered a valid package for Visual Studio
 40    /// is to implement the IVsPackage interface and register itself with the shell.
 41    /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
 42    /// to do it: it derives from the Package class that provides the implementation of the 
 43    /// IVsPackage interface and uses the registration attributes defined in the framework to 
 44    /// register itself and its components with the shell.
 45    /// </summary>
 46    // This attribute tells the registration utility (regpkg.exe) that this class needs
 47    // to be registered as package.
 48    [PackageRegistration(UseManagedResourcesOnly = true)]
 49    // A Visual Studio component can be registered under different regitry roots; for instance
 50    // when you debug your package you want to register it in the experimental hive. This
 51    // attribute specifies the registry root to use if no one is provided to regpkg.exe with
 52    // the /root switch.
 53    [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
 54    // This attribute is used to register the informations needed to show the this package
 55    // in the Help/About dialog of Visual Studio.
 56    [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
 57    // In order be loaded inside Visual Studio in a machine that has not the VS SDK installed, 
 58    // package needs to have a valid load key (it can be requested at 
 59    // http://msdn.microsoft.com/vstudio/extend/). This attributes tells the shell that this 
 60    // package has a load key embedded in its resources.
 61    [ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)]
 62    // This attribute is needed to let the shell know that this package exposes some menus.
 63    [ProvideMenuResource(1000, 1)]
 64    // This attribute registers a tool window exposed by this package.
 65    [ProvideToolWindow(typeof(MyToolWindow), Style = VsDockStyle.Tabbed, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")]
 66    [Guid(GuidList.guidVSPackageWPFToolWindowPkgString)]
 67    public sealed class CSVSPackageWPFToolWindowPackage : Package
 68    {
 69        /// <summary>
 70        /// Default constructor of the package.
 71        /// Inside this method you can place any initialization code that does not require 
 72        /// any Visual Studio service because at this point the package object is created but 
 73        /// not sited yet inside Visual Studio environment. The place to do all the other 
 74        /// initialization is the Initialize method.
 75        /// </summary>
 76        public CSVSPackageWPFToolWindowPackage()
 77        {
 78            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
 79        }
 80
 81        /// <summary>
 82        /// This function is called when the user clicks the menu item that shows the 
 83        /// tool window. See the Initialize method to see how the menu item is associated to 
 84        /// this function using the OleMenuCommandService service and the MenuCommand class.
 85        /// </summary>
 86        private void ShowToolWindow(object sender, EventArgs e)
 87        {
 88            // Get the instance number 0 of this tool window. This window is single instance so this instance
 89            // is actually the only one.
 90            // The last flag is set to true so that if the tool window does not exists it will be created.
 91            ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
 92            if ((null == window) || (null == window.Frame))
 93            {
 94                throw new NotSupportedException(Resources.CanNotCreateWindow);
 95            }
 96            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
 97            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
 98        }
 99
100
101        /////////////////////////////////////////////////////////////////////////////
102        // Overriden Package Implementation
103        #region Package Members
104
105        /// <summary>
106        /// Initialization of the package; this method is called right after the package is sited, so this is the place
107        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
108        /// </summary>
109        protected override void Initialize()
110        {
111            Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
112            base.Initialize();
113
114            // Add our command handlers for menu (commands must exist in the .vsct file)
115            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
116            if ( null != mcs )
117            {
118                // Create the command for the tool window
119                CommandID toolwndCommandID = new CommandID(GuidList.guidVSPackageWPFToolWindowCmdSet, (int)PkgCmdIDList.cmdidWPFToolWindow);
120                MenuCommand menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandID);
121                mcs.AddCommand( menuToolWin );
122            }
123        }
124        #endregion
125
126    }
127}