Click here to Skip to main content
6,936,634 members and growing! (17,303 online)
Email Password   helpLost your password?
 
General Programming » Macros and Add-ins » Visual Studio Add-ins     Beginner License: The Code Project Open License (CPOL)

ZoomSliderVSX - Visual Studio Extension (VSX) 2010

By Abhijit Jana

Scrollable Zooming Extension for VS 2010
C# (C#3.0, C#4.0), Windows, Visual-Studio (VS2010), WPF, Dev
Revision:5 (See All)
Posted:8 Feb 2010
Updated:8 Feb 2010
Views:6,424
Bookmarked:27 times
Prize winner in Competition "Visual Studio 2010 Extension Contest"
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
35 votes for this article.
Popularity: 7.37 Rating: 4.77 out of 5

1

2
3 votes, 8.6%
3
1 vote, 2.9%
4
31 votes, 88.6%
5
ZoomSliderVSX_Source

Table of Contents

Introduction

Developing Visual Studio 2010 Extension is very much easy and full of fun. I have developed one small extension for VS 2010 called "ZoomSliderVSX". ZoomSliderVSX will provide you a sliding Zooming facility to zoom your source code, where minimum zoom level is 20% and maximum is 400%. Though Visual Studio 2010 has an inbuilt dropdown to set the zoom level for VS Source editor still I found that the scrolling features is missing which is one of the most common features in most of the Microsoft Products like Microsoft-Word, Excel. So I have implemented this small add-in for Visual Studio 2010. Hope you will enjoy it. Please provide your valuable feedback and suggestions to improve this control.

Figure: ZoomSliderVSX UI

Prerequisite for Extension Development

For developing Visual Studio 2010 Extension, we need to have the following installed in our system:

  1. Visual Studio 2010 Beta 2
  2. Visual Studio 2010 Beta 2 SDK

Developing ZoomSliderVSX

Before we start with implementation, please make sure that all the required things are installed that are mentioned in the prerequisite section.

Extension Template Selection

After installing VS 2010 SDK, we will have different types of Extension Templates and based on our requirement, we have to select proper extension template. For ZoomSlider, I have used "Editor Margin" Template. If you want to learn more about what are the uses of other extension templates, please have a look into this.

To start up, Open Visual Studio 2010 IDE. Goto Start > New > Project. Select "Extensibility" from Left side template section. Then select "Editor Margin" Extension.

Figure: Select Extension Template

Default Solution Structure

After selecting template, give the project name as "ZoomSliderVSX" and click on "Ok". This will create one Sample VS Addin control of Editor margin type. Below is the default file structure for the "Editor Margin" type solution.

Figure: Default Solution Structure

As my control name is "ZoomSlider", I have renamed both the CS files to "ZoomSlider.cs" and "ZoomSliderFactory.cs". I have explained the use of both the files in the "Code" section. Now we are already set for starting our development, but one thing is remaining, adding the WPF user control. This is the heart of this extension. So, let’s add one WPF user control named "SliderControl.xaml".

Adding WPF User Control

To add user control in the application, right click on the project > Add > New Item

Figure: Adding New Item

Now Select "WPF Control" from the list of items.

Figure: Adding New WPF Control

Name the control as "SlideControl.xaml" and click on OK. Now, below is the final solution structure for "ZoomSliderVSX".

Final Solution Structure

So currently, we are having two CS files and one WPF control for the whole extension.

Figure: Final solution structure for development

So, let's have a look into the code.

Using Code

In this application, we have three main code files: ZoomSlider.cs, ZoomSliderFactory.cs and SliderControl.XML.

The ZoomControl class has been created to handle appropriate methods when the actual execution takes place within the extension. This class actually creates an object of the WPF User control, and places the control in Editor Margin. ZoomSlider constructor is used to place the control into margin and handle the LayoutChanged event when required.

// The IWpfTextView that our margin will be attached to.
private IWpfTextView _textView;

// The name of the margin
public const string MarginName = "ZoomSlideBar";

// A flag stating whether this margin has been disposed
private bool _isDisposed = false;

//Slider WPF User Control 
private SliderControl _slider = null;

public ZoomSlider(IWpfTextView textView)
{
    // Set the IWpfTextView
    _textView = textView;

    // Establish the background of the margin.
    this.Height = 30;
    this.ClipToBounds = true;
    this.Background = new SolidColorBrush(Color.FromArgb(60, 41,57, 85));
            
    //Set Direction of Control
    this.FlowDirection = System.Windows.FlowDirection.RightToLeft;
            
    //Add Event handler for for LayoutChanges Event
    _textView.LayoutChanged += this.OnLayoutChanged;
          
    //Create New instance for WPF Slider Control
    _slider = new SliderControl(_textView);

    //Add Control
    this.Children.Add(_slider);
}

ZoomSliderFactory class creates the object of ZoomSlider and returns it. In this class, we can also set property like order, margin, etc.

namespace ZoomSliderVSX
{
    #region ZoomSliderVSX Factory
    [Export(typeof(IWpfTextViewMarginProvider))]
    [Name("ZoomSlideBar")]
    //Ensure that the margin occurs below the horizontal scrollbar
    [Order(After = PredefinedMarginNames.HorizontalScrollBar)]
    //Set the container to the bottom of the editor window
    [MarginContainer(PredefinedMarginNames.Bottom)]
    //Do this for all content types
    [ContentType("text")]
    [TextViewRole(PredefinedTextViewRoles.Zoomable)]
    internal sealed class ZoomSliderFactory : IWpfTextViewMarginProvider
    {
        public IWpfTextViewMargin CreateMargin
	(IWpfTextViewHost textViewHost, IWpfTextViewMargin containerMargin)
        {
            return new ZoomSlider(textViewHost.TextView);
        }
    }
    #endregion
}

Now, check the code for the actual WPF control. This is very much simple where I have used two buttons and one Slider control.

     <StackPanel Orientation="Horizontal" Height="25">
        <Label Name="ZoomValue"
               Content=""
               Width="40"
               Foreground="#FF2C3B59">
            
        </Label>
        <Button Name="ZoomMinus" 
                Content="-"
                Width="20" 
                Click="ZoomMinus_Click" 
                Background="#FF293954" 
                Foreground="#FF00FA00">
        </Button>
       <Slider Name="Zoomer"
               Width="100"
               Minimum="20"
               Maximum="400"
               Cursor="Hand"
               Value="100"
               Ticks="20,50,70,100,150,200,400"
               TickPlacement="BottomRight"
               ValueChanged="Zoomer_ValueChanged"
               SmallChange="1"
               IsSnapToTickEnabled="True"
               Foreground="#FFAF1F28">
            <Slider.Background>
                <RadialGradientBrush>
                    <GradientStop Color="#FFBE2C00" Offset="0" />
                    <GradientStop Color="#FF9DB7B7" Offset="1" />
                </RadialGradientBrush>
            </Slider.Background>
        </Slider>
        <Button Name="ZoomPlus"
                Content="+"
                Width="20"
                Click="ZoomPlus_Click"
                Foreground="Lime"
                Background="#FF293855">
        </Button>
        
    </StackPanel>

Now all the events for button and slider control are handled in the xaml.cs file. Code for handling the slider value and setting the zoom level of textview is as below. We need to just increase and decrease the value of slider and based on that, we have set the TextView Zoom Level. Here SliderControl(IWpfTextView view) gets initialized from zoomslider class and Slider control receives the reference of WPFTextView.

namespace ZoomSliderVSX
{
    /// <summary>
    /// Interaction logic for Slider.xaml
    /// </summary>
    public partial class SliderControl : UserControl
    {
        //Initilize TextView
        IWpfTextView _view = null;
        public SliderControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initilize Slider  Control
        /// </summary>
        /// <param name=""view"">
        public SliderControl(IWpfTextView view)
        {
            this._view = view;
            InitializeComponent();
        }
        /// <summary>
        /// Click on Plus Button
        /// </summary>
        /// <param name=""sender"">
        /// <param name=""e"">
        private void ZoomPlus_Click(object sender, RoutedEventArgs e)
        {
            Zoomer.Value = Zoomer.Value + 10;
            _view.ZoomLevel = Zoomer.Value;
            ZoomValue.Content = "%" + Zoomer.Value;
        }

        /// <summary>
        /// Click on Minus Button
        /// </summary>
        /// <param name=""sender"">
        /// <param name=""e"">
        private void ZoomMinus_Click(object sender, RoutedEventArgs e)
        {
            Zoomer.Value = Zoomer.Value - 10;
            _view.ZoomLevel = Zoomer.Value;
            ZoomValue.Content = "%" + Zoomer.Value;
        }

        /// <summary>
        /// On Change of Slider Value
        /// </summary>
        /// <param name=""sender"">
        /// <param name=""e"">
        private void Zoomer_ValueChanged(object sender, 
			RoutedPropertyChangedEventArgs<double> e)
        {
            ZoomValue.Content = "%" + Zoomer.Value ;
            
            //Set the Zoom Level
            _view.ZoomLevel = Zoomer.Value;
        }

        /// <summary>
        /// Update Slider on Layout Changed
        /// </summary>
        public void UpdateSlider()
        {
            //Check if need be update the slider 
            if (Zoomer.Value != _view.ZoomLevel)
            {
                //Set the Zoom Value to Content and Slider
                ZoomValue.Content = "%" + _view.ZoomLevel;
                Zoomer.Value = _view.ZoomLevel;
            }  
        }
    }

Here I have also handled the layout changed event for Visual Studio editor. As I have already mentioned, VS 2010 has inbuilt Zooming dropdown to zoom the text, so I have integrated those changes with the ZoomSlider. In ZoomSlider.Cs file, inside ZoomSlider() constructor I have handled the event for layout change event.

private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
    if (_slider != null)
    //Call Update method of SliderControl
    _slider.UpdateSlider();
}

So, on layout change, it will call the UpdateSlider() method of my Slider usercontrol, which will automatically set the value of Slider and label based on the changes in dropdown list.

public void UpdateSlider()
{
    //Check if need be update the slider 
    if (Zoomer.Value != _view.ZoomLevel)
    {
        //Set the Zoom Value to Content and Slider
        ZoomValue.Content = "%" + _view.ZoomLevel;
        Zoomer.Value = _view.ZoomLevel;
    }  
}

We are now done with our development. Now, just build the application and run.

Run and Test Application

Whenever we run any extention control to test, Visual Studio will create one "Experimental Instance" where you can see your extension is on placed inside the VS IDE. This instance can also be used for debugging your control.

Figure: Test Application in VS Experimental Instance

Installation of ZoomSliderVSX

This is very easy. First of all, we need to run the .vsix file to setup the addin within Visual Studio IDE. Then we have to open "ExtentionManager" from Tool > Extension Manager to manage the extension where we can easily "Install", "Uninstall" or "Disabled" any extension.

Figure: Installing and Uninstalling ZoomSliderVSX

How to Use ZoomSliderVSX

Using Zoom Slider is very similar to using any zooming control. This is similar to all other Microsoft products like Microsoft-Office, Microsoft-Excel zooming control. Use Scroll to Zoom-in or Zoom-Out. You can use "+" and "-" button to achieve the same.

Figure: Using ZoomSlider

If you change the value of Dropdown list zoom level, that will also be reflected in ZoomSlider as these are integrated with each other.

Publishing Extension - MSDN Visual Studio Gallery

I have published the extension in MSDN Visual Studio Gallery. This is also very simple. You just need to upload the VSIX file. Www.visualstudiogallery.com. Here is my Extension in MSDN Visual Studio Library.

Summary

Developing an extension for VS 2010 is very easy and lots of fun. This is my first extension for VS 2010. Though this is very simple, I have enjoyed a lot. I have published the same in MSDN Visual Studio Gallery also. Please provide your feedback and suggestions.

History

  • Initial draft: 9th Feb - 2010

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhijit Jana


Member
Abhijit has done Master Degree in Computer Application from Heritage Institute of Technology (HIT-K) ,Kolkata, West Bengal, India . He is an interested, committed, creative Software professional having Approx. 3 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0, AJAX, Web Services, MS SQL Server 2005, Exchange Server, Active Directory, and Dot Net Nuke (DNN),Win Forms, WinServices, WSS (Windows Sharepoint Server 3.0 ), WPF, WWF. He is also an MCP (Microsoft Certified Professional) and MCTS (Microsoft Certified Technology Specialist) on Web Development. He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Design Patterns as well as good command over IIS (IIS 5.1,IIS 6.0, IIS 7.0) and deployment of Application on Live Production Environment . His hobbies, listing to music and Developing Own small Tools Utilities and Knowledge sharing.

Awards CodeProject MVP 2010 CodeProject MVP 2009 Prize winner "Best ASP.NET article of Sep 2009 Prize winner "Best ASP.NET article of July 2009 Prize winner "Best ASP.NET article of June 2009" Prize winner "Best ASP.NET article of January 2009" Prize winner "Best ASP.NET article of November 2008" Prize winner "Best ASP.NET article of October 2008"
Abhijit's CodeProject Guru : Sacha
Occupation: Software Developer (Senior)
Location: India India

Other popular Macros and Add-ins articles:

Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 45 (Total in Forum: 45) (Refresh)FirstPrevNext
GeneralGreat article & Congrats! PinmemberSandeep Mewara22:34 15 Mar '10  
GeneralRe: Great article & Congrats! PinmvpAbhijit Jana22:41 15 Mar '10  
GeneralCongratulations, Abhijit PinmemberMarcelo Ricardo de Oliveira5:39 12 Mar '10  
GeneralRe: Congratulations, Abhijit PinmvpAbhijit Jana5:58 12 Mar '10  
GeneralExcellent PinmemberKatka Vaughan4:18 11 Mar '10  
GeneralRe: Excellent PinmvpAbhijit Jana4:24 11 Mar '10  
GeneralExtremelly well done PinmemberRavenet18:28 9 Mar '10  
GeneralRe: Extremelly well done PinmvpAbhijit Jana7:05 10 Mar '10  
GeneralGreat idea but consumes to much screen real estate Pinmembertheperm0:20 9 Mar '10  
GeneralRe: Great idea but consumes to much screen real estate PinmvpAbhijit Jana4:45 9 Mar '10  
GeneralWell done for winning a Zune PinmvpSacha Barber22:46 8 Mar '10  
GeneralRe: Well done for winning a Zune PinmvpAbhijit Jana22:52 8 Mar '10  
GeneralGreat Work PinmemberGaurav Dudeja India21:05 7 Mar '10  
GeneralRe: Great Work PinmvpAbhijit Jana21:20 7 Mar '10  
GeneralNice man Pinmemberbhatia.sumeet18:50 16 Feb '10  
GeneralRe: Nice man PinmvpAbhijit Jana19:11 16 Feb '10  
GeneralGreat job PinmemberDr.Luiji21:56 15 Feb '10  
GeneralRe: Great job PinmvpAbhijit Jana22:50 15 Feb '10  
GeneralAwesome job Abhijit - Vote 5 PinmemberKunalChowdhury19:59 15 Feb '10  
GeneralRe: Awesome job Abhijit - Vote 5 PinmvpAbhijit Jana20:04 15 Feb '10  
Generalas usual... PinmemberPetr Pechovic12:16 12 Feb '10  
GeneralRe: as usual... PinmvpAbhijit Jana18:47 12 Feb '10  
GeneralGreat Effort...... Pinmembertushar_6086:02 9 Feb '10  
GeneralRe: Great Effort...... PinmvpAbhijit Jana6:05 9 Feb '10  
GeneralGood job son PinmvpSacha Barber4:21 9 Feb '10  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 8 Feb 2010
Editor: Deeksha Shenoy
Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project