Search code examples
visual-studio-2012visual-studio-addins

How to change editor font from within a Visual Studio addin?


I would like to be able to change the editor font in Visual Studio 2012 by using a keyboard shortcut. As macros have been removed, I understand this leaves only the option of writing an add-in.

So to clarify, I want to write an add-in that sets the editor font to a single specific font. I don’t want it to pop up any dialogs, as if it did that, I might as well use Tools → Options.

I already have an add-in that adds a Visual Studio command, so I already know how to do that. I also know how to assign a keyboard shortcut to it, so this question is not about either of those.

What are the commands in the Visual Studio add-in API to change the text editor font?


Solution

  • Here’s the answer:

    private void setFont(string fontFamily, int fontSize)
    {
        foreach (Property prop in _applicationObject.Properties["FontsAndColors", "TextEditor"])
        {
            if (prop.Name == "FontFamily")
                prop.Value = fontFamily;
            else if (prop.Name == "FontSize")
                prop.Value = fontSize;
        }
    }
    

    _applicationObject is assumed to contain the DTE2 object for the host environment.