Add Solution name to VS by developing a VS Addin
Sometimes I find myself wishing that the name of the solution that you are currently in, is displayed along with the name of the project and file in the window title of Visual Studio .NET. This becomes more important when I am working with multiple solutions and I want to quickly switch between them use alt-tab.
After failing to find a solution after googling for a VS option or VS Addin that provides this functionality, I decided to roll my own. This is how I did it:
1. I created a Visual Studio .NET Addin Project in Visual Studio. I chose C# as the language to develop the addin, but VB and C is also available. The Visual Studio .NET Addin Project generates C# source for a Connect class. The Connect class is a COM Interop enabled class which will generate a COM object with a Prog ID of [addinName].Connect. The Connect class implements the IDTExtensibility2 COM interface, which VS uses to load and interact with the AddIn.
2. The OnConnection method of the IDTExtensibility2 interface is called by VS whenever an Addin is loaded into the environment. The default OnConnection method implementation saves references to the application object and addinInstance objects in fields. The application object is a COM class which implements the _DTE interface. The application object can be used by the AddIn too interact with Visual Studio.
I modified the OnConnection method to subscribe to two events:
windowEvents = applicationObject.Events.get_WindowEvents(null); windowEvents.WindowActivated = new _dispWindowEvents_WindowActivatedEventHandler(WindowEvents_WindowActivated); applicationObject.Events.SolutionEvents.Opened =new _dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);
The WindowActivated event needs to be handled because, I needed to change the window title anytime a document was switched to. And the window activated event is the closest event I could find in the object model that came close to doing this.
The Opened event occurs whenever a solution is opened.
3. The SolutionEvents_Opened event handler extracts the solution filename from the path of the solution and saves it in a field. The path of the Solution may be retrieved using the Solution.FileName property. You could be led to believe that the FileName property will return just the filename of the solution as the name suggests, but alas it returns the entire path:
private void SolutionEvents_Opened()
{
_solutionName =
Path.GetFileName(applicationObject.Solution.FileName);
}
4. Now in the WindowEvents_WindowActivate event handler we need to set the Window Title.
Since I just wanted to prepend the file name of the solution to the window title, I first checked to see if the solution file name is already there:
if (_solutionName != string.Empty)
{
if (!applicationObject.MainWindow.Caption.StartsWith(_solutionName))
{
...
Now we can just set the window title like below right?
applicationObject.MainWindow.Caption = _solutionName " - " applicationObject.MainWindow.Caption;
Nope. The Caption property cannot be set for the MainWindow. So I had to resort to the Win32 API and use a function called SetWindowText:
[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string lpString);
The above is a PInvoke signature for the SetWindowText Win32 function. It accepts an hWnd which is a handle to a window, and the string to set as the text of the window. For “normal” windows setting the text of the window changes the window title.
So now because MainWindow conveniently provides an HWnd property which will receive the window handle of the main window. I could set the window title of the VS window title like this:
IntPtr hwnd = (IntPtr) applicationObject.MainWindow.HWnd; SetWindowText(hwnd, _solutionName " - " applicationObject.MainWindow.Caption);
5. Compile and then voila!! I could load my addin into VS and have the solution file name appear in the VS window title. Now getting this AddIn installed onto other machines is a different story, and is better left off for now.
interracial free movies sexvideo free sex movies downloadfree trailers xxx moviewild gone movie girlsregal theater moviesapphic pink moviessamples sex moviemovie strip Map



February 12th, 2006 at 1:09 pm
Excellent! That this isn’t a native feature of the IDE is ridiculous, and I can’t imagine I’m the only one who would love to get my hands on this add-in.
February 14th, 2006 at 9:50 am
Well found two bugs so far.
Window Title reverts when editing a file, and when coming out of a debugger.