Freigeben über


Have you had fun with Fonts and Colors yet?

Believe me I and many others have had a tough time figuring out how to consume and extend Visual Studio’s Fonts and Color options. It is one of those important features that has no to very little documentation in VSIP 2003. Let’s take a look at how easily we can extend this feature and use it.

The Fonts & Colors page is a property page in the Tools/Options dialog. It provides a centralized service through which any component within Visual Studio can proffer one or more categories of font and color information. You can both consume the service and extend it by providing your own category of settings. Good doctor is going to show you how to go about doing both.

Consuming the Fonts and Colors Available

The Fonts & Colors page manages the storage and retrieval of font and color data. To save or load the data programmatically, use the SVsFontAndColorStorage service to obtain an IVsFontAndColorStorage interface pointer.

Obtaining Font and Color service

 

IVsFontAndColorStorage pStorage = null;

pStorage = (IVsFontAndColorStorage)GetService(

                           typeof(IVsFontAndColorStorage));

To open a category's registry key for reading or writing, call IVsFontAndColorStorage ::OpenCategory. The parameters are the category GUID and a combination (binary OR) of FCSTORAGEFLAGS members (see vsshell.idl). One important flag bears mentioning here: you can use the FCSF_LOADDEFAULTS flag to load settings directly from the category provider if the settings have not been saved to the registry. Otherwise, only settings that have been changed from their defaults will be retrievable. The category guids can either be obtained programmatically or simply by using pre-known keys under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\FontAndColors.

            The pattern to use this service is call IVsFontAndColorStorage ::OpenCategory followed by any action that you want to take on the open font and color category. When done using it call IVsFontAndColorStorage::CloseCategory. It is designed this way to save against the accidental overwrites.

Contributing to the Fonts and Colors Options Page

Any window or other identifiable UI component for which the user can control font and/or color information can be represented in this dialog. For example, the font and colors of normal text editing for all languages are covered under the “Text Editor” category; the general environment font is covered under the “Dialogs and Tool Windows” category (BTW this category has been removed in Whidbey, since we now use Windows' font settings for the IDE.). To participate in the Fonts and Colors dialog, a package can proffer a service implementing IVsFontAndColorDefaultsProvider, along with IVsFontAndColorDefaults and IVsFontAndColorEvents. In addition to registering your service you also need to register your category. Each Fonts & Colors client must place one or more registry keys under HKLM\SOFTWARE\Microsoft\VisualStudio\[version]\FontAndColors. Each key identifies a category, and its name should be the non-localized name of the category. The key should contain at the minimum the following values:

“Category” = the GUID you created for your category.

 “Package” = the service (not package) ID for service that you are going to use or the service that provides the IVsFontAndColorDefaultsProvider interface. This Guid can be an existing service Guid. In the example above it is using the Text Management service. If you are implementing your own service then it should point to that Service Guid. (This key is unfortunately misnamed.)

IVsFontAndColorEvents implementation is minimal. Only methods that are worth implementing are the Apply and Reset methods, as you don't really want to be changing the color and font data until the user hits the OK button. You also don't need to implement IVsFontAndColorStorage. The environment will store any customized settings to the registry. You only need IVsFontAndColorStorage to read the persisted font/color settings, or to clear the registry completely. While the Visual Studio 2003 VSIP documentation does not cover this in detail the 2005 version will detail these interfaces.

In Visual Studio 2005 if you want to provide fonts & colors for a tool window that displays text using an IVsTextView (such as the Output Window or Command Window), and you don’t need to specify your own custom default settings, then you don’t have to implement any interfaces. You need to do the following:

§ Create a category GUID that you would want to be available in the Font and Colors options page

§ Create new registry entries under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\FontAndColors for your category that has the following entries.

Registering Custom Font and Color Category without implementing interfaces

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0Exp\FontAndColors\CSharp Tool Window]

"Package"="{F5E7E71D-1401-11D1-883B-0000F87579D2}"

"Category"="{9FF46859-A47E-47bf-8AC5-EC3DBE69D1FE}"

"ToolWindowPackage"="{7259e420-6241-4e0d-b535-5b820671d183}"

"NameID"=dword:00000064

Note:

“NameID” = the resource ID of the localized category name in your package.

“ToolWindowPackage” = Package GUID.

"Category"="{9FF46859-A47E-47bf-8AC5-EC3DBE69D1FE}" is just an example and the actual value can be a new GUID provided by the implementor.

§ Set the Font and Color Property category GUID

Setting the category GUIDs

// m_pView is your IVsTextView

 

IVsTextEditorPropertyCategoryContainer spPropCatContainer =

                          (IVsTextEditorPropertyCategoryContainer)m_pView;

if (spPropCatContainer != null)

{

     IVsTextEditorPropertyContainer spPropContainer;

     Guid GUID_EditPropCategory_View_MasterSettings =

                           new Guid("{D1756E7C-B7FD-49a8-B48E-87B14A55655A}");

     hr = spPropCatContainer.GetPropertyCategory(

                           ref GUID_EditPropCategory_View_MasterSettings,

                           out spPropContainer);

     if(hr == 0)

     {

            hr =

                 spPropContainer.SetProperty(

                     VSEDITPROPID.VSEDITPROPID_ViewGeneral_FontCategory,

                     catGUID);

            hr =

                 spPropContainer.SetProperty(

                     VSEDITPROPID.VSEDITPROPID_ViewGeneral_ColorCategory,

                     catGUID);

      }

}

 

That’s it! Your tool window will now show up in the Fonts & Colors dialog, and by adding the code

above, you’ll enable your tool window to use the current settings for that category.

Thanks!

Dr. eX

Comments