PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSVSPackageWPFToolWindow/MyToolWindow.cs

#
C# | 145 lines | 93 code | 19 blank | 33 comment | 6 complexity | eccbf34775e4c3fd706d6f74cc9a2c5b MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using System.Runtime.InteropServices;
  8. using Microsoft.VisualStudio.Shell.Interop;
  9. using Microsoft.VisualStudio.Shell;
  10. using System.IO;
  11. using System.Windows.Controls;
  12. using Microsoft.VisualStudio;
  13. using EnvDTE;
  14. namespace Company.VSPackageWPFToolWindow
  15. {
  16. /// <summary>
  17. /// This class implements the tool window exposed by this package and hosts a user control.
  18. ///
  19. /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane,
  20. /// usually implemented by the package implementer.
  21. ///
  22. /// This class derives from the ToolWindowPane class provided from the MPF in order to use its
  23. /// implementation of the IVsWindowPane interface.
  24. /// </summary>
  25. [Guid("1c0ed058-d9bf-44f0-97fa-18ae474a30f5")]
  26. public class MyToolWindow : ToolWindowPane, Microsoft.VisualStudio.OLE.Interop.IServiceProvider
  27. {
  28. // This is the user control hosted by the tool window; it is exposed to the base class
  29. // using the Window property. Note that, even if this class implements IDispose, we are
  30. // not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
  31. // the object returned by the Window property.
  32. private MyControl control;
  33. private WPFControl wpfControl;
  34. /// <summary>
  35. /// Standard constructor for the tool window.
  36. /// </summary>
  37. public MyToolWindow() :
  38. base(null)
  39. {
  40. // Set the window title reading it from the resources.
  41. this.Caption = Resources.ToolWindowTitle;
  42. // Set the image that will appear on the tab of the window frame
  43. // when docked with an other window
  44. // The resource ID correspond to the one defined in the resx file
  45. // while the Index is the offset in the bitmap strip. Each image in
  46. // the strip being 16x16.
  47. this.BitmapResourceID = 301;
  48. this.BitmapIndex = 1;
  49. control = new MyControl();
  50. wpfControl = control.WPFControl;
  51. }
  52. public override void OnToolWindowCreated()
  53. {
  54. base.OnToolWindowCreated();
  55. InitializeTreeViewContent();
  56. }
  57. private void InitializeTreeViewContent()
  58. {
  59. DirectoryInfo myDocInfo =
  60. new DirectoryInfo(
  61. Environment.GetFolderPath(
  62. Environment.SpecialFolder.MyDocuments
  63. )
  64. );
  65. IntializeTreeViewContentRecursively(myDocInfo, wpfControl.WPFTreeView.Items);
  66. }
  67. private void IntializeTreeViewContentRecursively(
  68. DirectoryInfo myDocInfo, ItemCollection itemCollection)
  69. {
  70. if (myDocInfo == null)
  71. return;
  72. try
  73. {
  74. TreeViewItem item = new TreeViewItem();
  75. item.Header = myDocInfo.Name;
  76. itemCollection.Add(item);
  77. DirectoryInfo[] subDirs = myDocInfo.GetDirectories();
  78. if (subDirs != null)
  79. {
  80. foreach (DirectoryInfo dir in subDirs)
  81. IntializeTreeViewContentRecursively(dir, item.Items);
  82. }
  83. FileInfo[] files = myDocInfo.GetFiles();
  84. if (files != null)
  85. {
  86. foreach (FileInfo file in files)
  87. {
  88. TreeViewItem fileItem = new TreeViewItem();
  89. fileItem.Header = file.Name;
  90. fileItem.Tag = file.FullName;
  91. // fileItem.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(fileItem_MouseDoubleClick);
  92. item.Items.Add(fileItem);
  93. }
  94. }
  95. }
  96. catch
  97. {
  98. return;
  99. }
  100. }
  101. //void fileItem_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
  102. //{
  103. // TreeViewItem item = sender as TreeViewItem;
  104. // DTE dte = GetService(typeof(DTE)) as DTE;
  105. // dte.ExecuteCommand("File.OpenFile", item.Tag as string);
  106. //}
  107. /// <summary>
  108. /// This property returns the handle to the user control that should
  109. /// be hosted in the Tool Window.
  110. /// </summary>
  111. override public IWin32Window Window
  112. {
  113. get
  114. {
  115. return (IWin32Window)control;
  116. }
  117. }
  118. #region IServiceProvider Members
  119. public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
  120. {
  121. ppvObject = IntPtr.Zero;
  122. return VSConstants.S_OK;
  123. }
  124. #endregion
  125. }
  126. }