Click here to Skip to main content
Click here to Skip to main content

ZoomSliderVSX - Visual Studio Extension (VSX) 2010

By , 8 Feb 2010
 
Prize winner in Competition "Visual Studio 2010 Extension Contest"
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
Software Developer (Senior)
India India
Member
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband
 
Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGreat ArticlememberArsenmkrt3 Jun '12 - 23:19 
Great article,
VS extensions are great, but unfortunately there are not learning resources for that. I am having some problem described here(MSDN[^]), could you plz response if you know how to solve this?
GeneralMy vote of 5memberashutosh k. shukla18 Jan '12 - 2:34 
Good Job
QuestionMy vote of 5memberFilip D'haene8 Sep '11 - 6:21 
Just excellent!
 
Thanks for sharing. Smile | :)
GeneralMy vote of 5groupSaraf Talukder31 Aug '11 - 3:02 
Great Article. Thanks for sharing.
GeneralMy vote of 5memberSChristmas3 Mar '11 - 5:57 
Nice one
GeneralNice Article!memberxzz019528 Oct '10 - 10:25 
Thanks for posting this. Smile | :)
GeneralRe: Nice Article!mvpAbhijit Jana30 Oct '10 - 11:49 
Thanks !!
Cheers !
Abhijit Jana | My Blog | @Twitter | Disclaimer

GeneralCongratsmemberBlue_Boy17 Aug '10 - 0:37 
Congrats dude, nice and great work Smile | :)

I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
 
www.aktualiteti.com

AnswerRe: CongratsmvpAbhijit Jana27 Aug '10 - 8:08 
Thank you very much !!! Smile | :)
Cheers !
Abhijit Jana | MVP
Web Site : abhijitjana.net | Follow Me @ twitter
Read my Latest Article :Mastering Debugging in VS 2010

GeneralCongrats for the Grand Prize - Sony VAIOmentorKunalChowdhury10 May '10 - 7:57 
Hi Abhijit,
 
Congrats for the Grand Prize. Thumbs Up | :thumbsup: You are doing a great job.
 
Second, we've picked our Visual Studio 2010 Extension Grand Prize winner: Abhijit Jana - who wins a Sony VAIO® Notebook for his ZoomSliderVSX article.


 
You will definitely win more prizes. Keep up the good thing. Thumbs Up | :thumbsup:
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

GeneralRe: Congrats for the Grand Prize - Sony VAIOmvpAbhijit Jana10 May '10 - 8:22 
Thanks Kunal !!!
Cheers !
Abhijit Jana | MVP
Web Site : abhijitjana.net | Follow Me @ twitter
Read my Latest Article :Mastering Debugging in VS 2010

GeneralRe: Congrats for the Grand Prize - Sony VAIOmvpRajesh R Subramanian10 May '10 - 8:38 
Congrats man! Smile | :)

“Follow your bliss.” – Joseph Campbell

GeneralRe: Congrats for the Grand Prize - Sony VAIOmvpAbhijit Jana10 May '10 - 9:16 
Thank You very much Rajesh Smile | :)
Cheers !
Abhijit Jana | MVP
Web Site : abhijitjana.net | Follow Me @ twitter
Read my Latest Article :Mastering Debugging in VS 2010

GeneralRe: Congrats for the Grand Prize - Sony VAIOmemberDr.Luiji10 May '10 - 11:54 
I'm agree, outstanding work.
 
Congrats Abhijit Thumbs Up | :thumbsup:
Dr.Luiji
 
Trust and you'll be trusted.
 
My Blog : The Code Is Art

GeneralRe: Congrats for the Grand Prize - Sony VAIOmvpAbhijit Jana10 May '10 - 18:10 
Thank You Smile | :)
Cheers !
Abhijit Jana | MVP
Web Site : abhijitjana.net | Follow Me @ twitter
Read my Latest Article :Mastering Debugging in VS 2010

GeneralNicely donememberMohammed Gouda31 Mar '10 - 11:45 
5 from me
And congratulations
foreach(Minute m in MyLife)
myExperience++;

GeneralRe: Nicely donemvpAbhijit Jana31 Mar '10 - 20:34 
Thank you So much !!!
Cheers !
Abhijit
Codeproject MVP

GeneralRe: Nicely donememberMohammed Gouda1 Apr '10 - 10:25 
Have a look at my one and tell me your opinion, champ
foreach(Minute m in MyLife)
myExperience++;

GeneralGreat article & Congrats!memberSandeep Mewara15 Mar '10 - 21:34 
Good work! Thumbs Up | :thumbsup:
Congrats on winning the competition Thumbs Up | :thumbsup: Thumbs Up | :thumbsup:
Smile | :)
GeneralRe: Great article & Congrats!mvpAbhijit Jana15 Mar '10 - 21:41 
Thanks Sandeep !
Cheers !
Abhijit
Codeproject MVP

GeneralCongratulations, AbhijitmemberMarcelo Ricardo de Oliveira12 Mar '10 - 4:39 
Outstanding work, and well deserved prize Smile | :) Thumbs Up | :thumbsup:
 
cheers
marcelo
Take a look at XNA Snooker Club game here in Code Project.

GeneralRe: Congratulations, AbhijitmvpAbhijit Jana12 Mar '10 - 4:58 
Thank you very much mate !!! Thumbs Up | :thumbsup:
GeneralExcellentmemberKatka Vaughan11 Mar '10 - 3:18 
Great article and congratulations for the extension contest.
GeneralRe: ExcellentmvpAbhijit Jana11 Mar '10 - 3:24 
Thank you so much Katka !! Thumbs Up | :thumbsup:
GeneralExtremelly well donememberRavenet9 Mar '10 - 17:28 
Dear Freind,
 

This is nice work and excellent find from your mind.
 
tnx
raveen
RRave
MCTS,MCPD
All experts are welcome to http://codegain.com
 

 

GeneralRe: Extremelly well donemvpAbhijit Jana10 Mar '10 - 6:05 
Thank you so much !
GeneralGreat idea but consumes to much screen real estatemembertheperm8 Mar '10 - 23:20 
Love the concept but it wastes so much screen real estate, have you looked at the possibility of positioning it next to the zoom drop down bringin it more inline with MS office?
 
Dont know why MS didnt do this in the first place.
GeneralRe: Great idea but consumes to much screen real estatemvpAbhijit Jana9 Mar '10 - 3:45 
Thank you. Nice to know you liked the idea.
 
Yes, Its took too much space, and Paul has also inform me the same thing, Check This[^]
 
Let see, how I can utilize the place. If you have any idea then please let me know.
 
theperm wrote:
Dont know why MS didnt do this in the first place.

Smile | :)
 
Thanks again for appreciation and vote Thumbs Up | :thumbsup:
GeneralWell done for winning a ZunemvpSacha Barber8 Mar '10 - 21:46 
Well done champ
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralRe: Well done for winning a ZunemvpAbhijit Jana8 Mar '10 - 21:52 
Thank you so much Boss for your good wishes !!Thumbs Up | :thumbsup:
GeneralGreat WorkmemberGaurav Dudeja India7 Mar '10 - 20:05 
Very Good Article.
GeneralRe: Great WorkmvpAbhijit Jana7 Mar '10 - 20:20 
Thanks Gaurav ! Smile | :)
GeneralNice manmemberbhatia.sumeet16 Feb '10 - 17:50 
Very gud stuff
GeneralRe: Nice manmvpAbhijit Jana16 Feb '10 - 18:11 
Thanks Sumeet. Thumbs Up | :thumbsup:
GeneralGreat jobmemberDr.Luiji15 Feb '10 - 20:56 
This is an amazing extension!! Cool | :cool:
I like it!
Dr.Luiji
 
Trust and you'll be trusted.
 
My Blog : The Code Is Art

GeneralRe: Great jobmvpAbhijit Jana15 Feb '10 - 21:50 
Thanks ! Thumbs Up | :thumbsup:
GeneralAwesome job Abhijit - Vote 5memberKunalChowdhury15 Feb '10 - 18:59 
Abhijit,
 
Nice one. Keep it up.
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets

GeneralRe: Awesome job Abhijit - Vote 5mvpAbhijit Jana15 Feb '10 - 19:04 
Thank you Kunal. Smile | :)
Generalas usual...memberPetr Pechovic12 Feb '10 - 11:16 
... very good article Wink | ;) Smile | :) (5)
GeneralRe: as usual...mvpAbhijit Jana12 Feb '10 - 17:47 
Petr,
 
Thank you Smile | :)
GeneralGreat Effort......membertushar_6089 Feb '10 - 5:02 
Keep it up man Smile | :) !!!!
GeneralRe: Great Effort......mvpAbhijit Jana9 Feb '10 - 5:05 
Thanks mate Smile | :)
GeneralGood job sonmvpSacha Barber9 Feb '10 - 3:21 
I like it.
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralRe: Good job sonmvpAbhijit Jana9 Feb '10 - 3:25 
Love you Dad Rose | [Rose] Smile | :)
GeneralRe: Good job sonmvpSacha Barber9 Feb '10 - 3:36 
Ha Ha
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralRe: Good job sonmvpAbhijit Jana9 Feb '10 - 4:08 
You are my hero Smile | :) Thumbs Up | :thumbsup:
GeneralRe: Good job sonmembersashidhar9 Feb '10 - 19:06 
Abhijit Jana wrote:
Love you Dad

 
Laugh | :laugh:

LatestArticle :Log4Net

Why Do Some People Forget To Mark as Answer .If It Helps.

GeneralNice articlememberBrij8 Feb '10 - 22:13 
Its a very good learning material.. Good work.
Thanks a lot..
Cheers!!
Brij

GeneralRe: Nice articlemvpAbhijit Jana9 Feb '10 - 3:26 
Thank you Brij !!! Smile | :)
GeneralTake my 5membersashidhar8 Feb '10 - 17:35 
Good..!Visual Studio 2010 great start.Looking forward having for more from you in visual studio2010..!

LatestArticle :Log4Net

Why Do Some People Forget To Mark as Answer .If It Helps.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 8 Feb 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid