c# - How to use IVsFontAndColorEvents? -


microsoft's own site not explain in details how use interface. claim way notified if fonts & colors change in visual studio.

i tried seemed obvious choice , implemented interface on package, there no attributes mentioned should set on vspackage. unfortunately doesn't seem enough.

here's sample of did:

public class scevsipackage : package, ivsfontandcolorevents {     public int onapply()     {         return vsconstants.s_ok;     }      public int onfontchanged(ref guid rguidcategory, fontinfo[] pinfo, logfontw[] plogfont, uint hfont)     {         return vsconstants.s_ok;     }      public int onitemchanged(ref guid rguidcategory, string szitem, int iitem, colorableiteminfo[] pinfo, uint crliteralforeground, uint crliteralbackground)     {         return vsconstants.s_ok;     }      public int onreset(ref guid rguidcategory)     {         return vsconstants.s_ok;     }      public int onresettobasecategory(ref guid rguidcategory)     {         return vsconstants.s_ok;     } } 

unfortunately none of ivsfontandcolorevent members (all methods above) called.

do miss else? attribute? or proffering service?

i tried servicecontainer.addservice(typeof(ivsfontandcolorevent), this, true); didn't either.

workaround

unfortunately couldn't make ivsfontandcolorevents working. however, achieve same (getting notified when fonts change in tools\options\fonts , colors\text editor) code found here.

the idea use textmanagerevents instead of ivsfontandcolorevents:

//using microsoft.visualstudio.textmanager.interop; ivstextmanager textmanager = getservice(typeof(svstextmanager)) ivstextmanager; if (textmanager != null) {     iconnectionpointcontainer container = textmanager iconnectionpointcontainer;     if (container != null)     {         iconnectionpoint textmanagereventsconnection;         guid eventguid = typeof(ivstextmanagerevents).guid;         container.findconnectionpoint(ref eventguid, out textmanagereventsconnection);         if (textmanagereventsconnection != null)         {             textmanagerevents textmanagerevents = new textmanagerevents();             uint textmanagercookie;             textmanagereventsconnection.advise(textmanagerevents, out textmanagercookie);             if (textmanagercookie != 0)             {                 textmanagerevents.fontcolorpreferenceschanged += onfontcolorpreferenceschanged;             }         }     } } 

notes

1. onfontcolorpreferenceschanged

just in case interested in how extract font , color information, here how did it:

private fontinfo prevfontinfo;  // store previous fontinfo prevent execution of event handler multiple times.  private void onfontcolorpreferenceschanged(object sender, eventargs e) {     ivsfontandcolorstorage fontandcolorstorage = getservice(typeof(svsfontandcolorstorage)) ivsfontandcolorstorage;     if (fontandcolorstorage != null)     {         // globalvalues.fontsandcolors_texteditor found in registry: hkey_users\.default\software\microsoft\visualstudio\[vs_ver]_config\fontandcolo‌​rs\text editor, vs_ver 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];  // 1 item expected             fontinfo[] fontinfo = new fontinfo[1];  // 1 item expected             if (fontandcolorstorage.getfont(logfontw, fontinfo) == vsconstants.s_ok &&                 !prevfontinfo.equals(fontinfo[0]))             {                 prevfontinfo = fontinfo[0];                  // fontinfo uses pixels units, wpf uses points. conversion between 2 required.                 double fontsize = (double)new fontsizeconverter().convertfrom(string.format("{0}pt", fontinfo.wpointsize));                     fontfamily fontfamily = new fontfamily(fontinfo.bstrfacename);                 // there go, have fontfamily , size ready use.             }             fontandcolorstorage.closecategory();         }     } } 

2. limitations

although solutions usable workaround me, has problems:

  • when changing font of text editor, onfontcolorpreferenceschanged event raised multiple times. can't tell if ivsfontandcolorevents raise event once or had same problem (as never got working.) i solved issue introducing prevfontinfo , don't invoke logic unless value different fontinfo[0], values read.
  • the event fires when text editor fonts , colors changed, not when of rest (e.g. environment font or output window)
  • the event not fire when bold option changed. nevertheless, font weight not seemed used ide anyway...
  • the event not fire when "use defaults" selected in options/fonts , colors. matter of fact doesn't fire either when it's reset default values manually entering them (e.g.: font size 10)

i hope of these might useful stumbling upon question.


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -