My fifteenth How do I is up.
Scenario
Several methods in the VS Interop world receive a IVsHierarchy item id parameter as an argument.
The problem is that there is no easy way to obtain the item id for a IVsHierarchy. Ideally we would like to do IVsHierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ItemId, out itemId) but that property doesn't exist.
The focus of this How do I tackles this scenario.
Interfaces and classes needed
Code snippet
public uint GetItemId(IVsHierarchy hierarchy)
{
object extObject;
uint itemId = 0;
IVsHierarchy tempHierarchy;
hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_BrowseObject, out extObject);
IVsBrowseObject browseObject = extObject as IVsBrowseObject;
if(browseObject != null)
{
browseObject.GetProjectItem(out tempHierarchy, out itemId);
}
return itemId;
}
Assemblies needed
- Microsoft.VisualStudio.Shell.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Stay tuned,
Pablo
My sixteenth How do I is up.
Scenario
It is always a best practice to inform the user what is going on when we need to perform a long running operation. For that purpose Visual Studio has the status bar:
There are several options when dealing with the Visual Studio status bar, we can write messages, display progress and animations, etc. The focus of this How do I tackles these scenarios.
Interfaces and classes needed
Code snippet
int frozen;
IVsStatusbar statusBar = serviceProvider.GetService(typeof(SVsStatusbar)) asIVsStatusbar;
statusBar.IsFrozen(out frozen);
//Write to the status bar
if(!Convert.ToBoolean(frozen))
{
statusBar.SetText("The Message");
//Do process
Thread.Sleep(5000);
statusBar.Clear();
}
//Write and display animatio in the status bar
if(!Convert.ToBoolean(frozen))
{
//The built icons that can be used are defined in Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_*
object icon = (short)Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_General;
statusBar.Animation(1, ref icon);
statusBar.SetText("The Message");
//Do process
Thread.Sleep(5000);
statusBar.Animation(0, ref icon);
statusBar.Clear();
}
//Display progress in the status bar
if(!Convert.ToBoolean(frozen))
{
uint userId = (uint)Microsoft.VisualStudio.Shell.Interop.Constants.VSCOOKIE_NIL;
for(int i = 1; i <= 10; i++)
{
statusBar.Progress(ref userId, 1, "The Message", (uint)i * 10, (uint)100);
//Do process
Thread.Sleep(1000);
}
statusBar.Progress(ref userId, 0, string.Empty, 0, 0);
}
Assemblies needed
- Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Stay tuned,
Pablo