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