C# 如何使用IVsFontAndColorEvents?
微软自己没有详细解释如何使用这个界面。他们声称,如果在VisualStudio中字体和颜色发生变化,这是获得通知的一种方式 我尝试了一个看起来很明显的选择,并在我的包上实现了接口,但是没有提到我应该在我的VSPackage上设置的属性。不幸的是,这似乎还不够 以下是我所做工作的示例:C# 如何使用IVsFontAndColorEvents?,c#,visual-studio,fonts,colors,vspackage,C#,Visual Studio,Fonts,Colors,Vspackage,微软自己没有详细解释如何使用这个界面。他们声称,如果在VisualStudio中字体和颜色发生变化,这是获得通知的一种方式 我尝试了一个看起来很明显的选择,并在我的包上实现了接口,但是没有提到我应该在我的VSPackage上设置的属性。不幸的是,这似乎还不够 以下是我所做工作的示例: 公共类SceVSIPackage:包、IVsFontAndColorEvents { 公共int OnApply() { 返回VSConstants.S_OK; } public int OnFontChanged
公共类SceVSIPackage:包、IVsFontAndColorEvents
{
公共int OnApply()
{
返回VSConstants.S_OK;
}
public int OnFontChanged(参考Guid rguidCategory、FontInfo[]pInfo、LOGFONTW[]pLOGFONT、uint HFONT)
{
返回VSConstants.S_OK;
}
public int-OnItemChanged(参考Guid rguidCategory、字符串szItem、int-iItem、ColorableItemInfo[]pInfo、uint crLiteralForeground、uint crLiteralBackground)
{
返回VSConstants.S_OK;
}
公共int OnReset(参考Guid rguidCategory)
{
返回VSConstants.S_OK;
}
公共int OnResetToBaseCategory(参考Guid rguidCategory)
{
返回VSConstants.S_OK;
}
}
不幸的是,没有一个IVsFontAndColorEvent成员(上述所有方法)被调用
我还错过了什么吗?像一个属性?还是提供服务
我还尝试了serviceContainer.AddService(typeof(IVsFontAndColorEvent)),这是真的代码>但也没有帮助。解决方法
不幸的是,我无法使IVsFontAndColorEvents
工作。但是,我可以用找到的代码实现同样的效果(在Tools\Options\Fonts and Color\Text Editor中字体更改时得到通知)
想法是使用textmanagervents
而不是IVsFontAndColorEvents
:
//使用Microsoft.VisualStudio.TextManager.Interop;
IVsTextManager textManager=GetService(typeof(SVsTextManager))作为IVsTextManager;
if(textManager!=null)
{
IConnectionPointContainer=textManager作为IConnectionPointContainer;
if(容器!=null)
{
i连接点文本管理器预览连接;
Guid eventGuid=typeof(ivstextmanagervents).Guid;
container.FindConnectionPoint(ref eventGuid,out textManagerEventsConnection);
if(textManagerEventsConnection!=null)
{
textmanagervents textmanagervents=新的textmanagervents();
uint textManagerCookie;
textmanagerventsconnection.advice(textmanagervents,out textManagerCookie);
如果(textManagerCookie!=0)
{
textManagerEvents.FontColorPreferencesChanged+=OnFontColorPreferencesChanged;
}
}
}
}
笔记
1。OnFontColorPreferencesChanged
如果您还对如何提取字体和颜色信息感兴趣,请看我是如何做到的:
private FontInfo prevFontInfo; // Store previous FontInfo to prevent execution of the event handler multiple times.
private void OnFontColorPreferencesChanged(object sender, EventArgs e)
{
IVsFontAndColorStorage fontAndColorStorage = GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorStorage;
if (fontAndColorStorage != null)
{
// GlobalValues.FontsAndColors_TextEditor is found in the registry: HKEY_USERS\.DEFAULT\Software\Microsoft\VisualStudio\[VS_VER]_Config\FontAndColors\Text Editor, where VS_VER is the actual Visual Studio version: 10.0, 11.0, 12.0, 14.0, etc.
if (fontAndColorStorage.OpenCategory(GlobalValues.FontsAndColors_TextEditor, (uint)__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS) == VSConstants.S_OK)
{
LOGFONTW[] logFontw = new LOGFONTW[1]; // Only 1 item expected
FontInfo[] fontInfo = new FontInfo[1]; // Only 1 item expected
if (fontAndColorStorage.GetFont(logFontw, fontInfo) == VSConstants.S_OK &&
!prevFontInfo.Equals(fontInfo[0]))
{
prevFontInfo = fontInfo[0];
// FontInfo uses pixels as units, WPF uses points. Conversion between the two is required.
double fontSize = (double)new FontSizeConverter().ConvertFrom(string.Format("{0}pt", fontInfo.wPointSize));
FontFamily fontFamily = new FontFamily(fontInfo.bstrFaceName);
// There you go, you have the FontFamily and size ready to use.
}
fontAndColorStorage.CloseCategory();
}
}
}
2。限制
虽然此解决方案对我来说是一个可用的解决方案,但它存在一些问题:
- 更改文本编辑器的字体时,会多次引发
OnFontColorPreferencesChanged
事件。我无法判断IVsFontAndColorEvents
是否只引发一次事件,或者是否存在相同的问题(因为我从未让它工作)。我通过引入prevFontInfo
解决了这个问题,并且不调用我的逻辑,除非这个值与我刚刚读取的值fontInfo[0]不同
- 仅当文本编辑器字体和颜色发生更改时,事件才会激发,但在其他任何情况下(例如,环境字体或输出窗口)都不会激发
- 更改粗体选项时不会触发事件。尽管如此,IDE似乎并没有使用字体大小
- 在选项/字体和颜色中选择“使用默认值”时,不会触发事件。事实上,当手动输入默认值(例如:字体大小为10)将其重置为默认值时,也不会触发
我希望其中的一些可能会对遇到这个问题的人有用。以下内容应该会有所帮助,但似乎仍然不够/有效:,(注意:在Matthew-s的回复中,站点2是“FontsandColor”,应该只是“FontsandColor”),您在哪里找到了GlobalValues.FontsandColor\u TextEditor的值
(指向文本编辑器类别的guid)?在注册表(regedit.exe)中HKLM\SOFTWARE\Microsoft\VisualStudio\10.0\FontAndColors\Text Editor\Category
保存GlobalValues.FontAndColors\u textdeditor
的GUID值。它可能位于不同Visual Studio版本的其他位置。更好的位置是HKEY\u用户\.DEFAULT\SOFTWARE\Microsoft\VisualStudio\[VS\VER]_Config\FontAndColors\Text Editor
,其中VS\u VER是Visual Studio的版本:10.0、11.0、12.0、14.0等等。谢谢!我刚刚找到了另一种方法:Microsoft.VisualStudio.Editor.DefGuidList.guidtexteditorfontcography
编程之美:你可以用许多不同的方式实现类似的事情。:)