Timwi
Timwi

Reputation: 66584

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?

Upvotes: 2

Views: 219

Answers (2)

Timwi
Timwi

Reputation: 66584

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.

Upvotes: 3

ceztko
ceztko

Reputation: 15207

+1 to Timwi. As a bonus to his answer, the following ones were useful to me to change properties in Tools -> Options:

_DTE2.Properties["TextEditor", "General"].Item("DetectUTF8WithoutSignature").Value = true;
_DTE2.Properties["Environment", "Documents"].Item("CheckLineEndingsOnLoad").Value = true;

Upvotes: 0

Related Questions