Suppose that we have a tool window and we want to handle the events when it is moved, resized, re docked, etc.
For doing that we only need to implement the Microsoft.VisualStudio.Shell.Interop.IVsWindowFrameNotify3 interface on the tool window.
The following code snippet does the trick:
[Guid("315b41f1-903f-4fcc-af56-a7ad9e84c910")]
public
class
MyToolWindow : ToolWindowPane, IVsWindowFrameNotify3
{
privateMyControl control;
public MyToolWindow()
:
base(null)
{
this.Caption = Resources.ToolWindowTitle;
this.BitmapResourceID = 301;
this.BitmapIndex = 1;
control = newMyControl(this);
}
overridepublicIWin32Window Window
{
get
{
return (IWin32Window)control;
}
}
#region IVsWindowFrameNotify3 Members
publicint OnClose(refuint pgrfSaveOptions)
{
returnVSConstants.S_OK;
}
publicint OnDockableChange(int fDockable, int x, int y, int w, int h)
{
returnVSConstants.S_OK;
}
publicint OnMove(int x, int y, int w, int h)
{
returnVSConstants.S_OK;
}
publicint OnSize(int x, int y, int w, int h)
{
returnVSConstants.S_OK;
}
publicint OnShow(int fShow)
{
returnVSConstants.S_OK;
}
#endregion
}
One important thing to mention is that when the user click on the "X" of a tool window, VS doesn't close it, it only hides it. When the IDE shuts down that is the only time when the tool window is closed.
Pablo
This post focus on the VS selection architecture and how to use the MPF to integrate a particular selection with the property browser.
VS constant keep track of the current selected objects and it also tracks what the property browser should be showing by tracking by using the Microsoft.VisualStudio.Shell.SelectionContainer object .
The SelectionContainer is an object that can hold single or multiple selection that property browser should be showing on.
The following codesnippet shows how to use the SelectionContainer in conjunction with the IVsTrackSelectionEx interface:
[Guid("315b41f1-903f-4fcc-af56-a7ad9e84c910")]
public
class
MyToolWindow : ToolWindowPane
{
private MyControl control;
private SelectionContainer selectionContainer;
public MyToolWindow()
:
base(null)
{
this.Caption = Resources.ToolWindowTitle;
this.BitmapResourceID = 301;
this.BitmapIndex = 1;
control = new MyControl(this);
}
override public IWin32Window Window
{
get
{
return (IWin32Window)control;
}
}
public override void OnToolWindowCreated()
{
selectionContainer = new Microsoft.VisualStudio.Shell.SelectionContainer();
}
public virtual void OnSelectionChange(ICollection selectedObjects)
{
IVsTrackSelectionEx trackSelection = this.GetService(typeof(SVsTrackSelectionEx)) as IVsTrackSelectionEx;
if(trackSelection != null)
{
selectionContainer.SelectedObjects = selectedObjects;
selectionContainer.SelectableObjects = selectedObjects;
trackSelection.OnSelectChange((ISelectionContainer)selectionContainer);
}
}
}
Then a basic user control sample:
public
partial
class
MyControl :
UserControl{
ArrayList list;
private MyToolWindow tool;
public MyControl(MyToolWindow tool)
{
this.tool = tool;
InitializeComponent();
InitializeList();
}
private void InitializeList()
{
list = new ArrayList();
Customer cust = new Customer();
cust.Name = "Foo";
cust.Age = 99;
list.Add(cust);
}
private void button1_Click(object sender, System.EventArgs e)
{
tool.OnSelectionChange(list);
}
}
internal class Customer
{
private string name;
[Category("Customer")]
[Description("The Name")]
[Browsable(true)]
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
[Category("Customer")]
[Description("The age")]
[Browsable(true)]
public int Age
{
get { return age; }
set { age = value; }
}
}
There is also another interface that can be used that is the Microsoft.VisualStudio.Shell.Interop.IVsMonitorSelection interface. IVsMonitorSelection is a global VS Service that manages what is currently selected at the IDE at a particular time and it can also be used to notify a subscriber every time a selection changes.
Pablo