FontInfo?GetTextEditorFontInfo() { IVsFontAndColorStorage fontStorage = (IVsFontAndColorStorage)VSAsmPackage.GetGlobalService(typeof(SVsFontAndColorStorage)); if (fontStorage == null) { return(null); } if (fontStorage.OpenCategory(TextEditorFontGuid, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) != VSConstants.S_OK) { return(null); } FontInfo[] info = new FontInfo[1]; int result = fontStorage.GetFont(null, info); fontStorage.CloseCategory(); if (result != VSConstants.S_OK) { return(null); } return(info[0]); }
internal static void AdjustFontSize(IServiceProvider serviceProvider, Guid category, short change) { IVsFontAndColorStorage storage = (IVsFontAndColorStorage)serviceProvider.GetService(typeof(SVsFontAndColorStorage)); IVsFontAndColorUtilities utilities = storage as IVsFontAndColorUtilities; LOGFONTW[] pLOGFONT = new LOGFONTW[1]; FontInfo[] pInfo = new FontInfo[1]; ErrorHandler.ThrowOnFailure(storage.OpenCategory(category, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES))); try { if (!ErrorHandler.Succeeded(storage.GetFont(pLOGFONT, pInfo))) { return; } pInfo[0].wPointSize = checked ((ushort)(pInfo[0].wPointSize + change)); ErrorHandler.ThrowOnFailure(storage.SetFont(pInfo)); ErrorHandler.ThrowOnFailure(utilities.FreeFontInfo(pInfo)); } finally { storage.CloseCategory(); } }
private async Task LoadSystemTextSettingsAsync(CancellationToken cancellationToken) { await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); try { IVsFontAndColorStorage storage = (IVsFontAndColorStorage)GetGlobalService(typeof(IVsFontAndColorStorage)); var guid = new Guid("A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0"); if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK) { #pragma warning disable SA1129 // Do not use default value type constructor LOGFONTW[] fnt = new LOGFONTW[] { new LOGFONTW() }; FontInfo[] info = new FontInfo[] { new FontInfo() }; #pragma warning restore SA1129 // Do not use default value type constructor if (storage.GetFont(fnt, info) == Microsoft.VisualStudio.VSConstants.S_OK) { var fontSize = info[0].wPointSize; if (fontSize > 0) { ResourceAdornmentManager.TextSize = fontSize; } } } if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK) { var info = new ColorableItemInfo[1]; // Get the color value configured for regular string display if (storage.GetItem("String", info) == Microsoft.VisualStudio.VSConstants.S_OK) { var win32Color = (int)info[0].crForeground; int r = win32Color & 0x000000FF; int g = (win32Color & 0x0000FF00) >> 8; int b = (win32Color & 0x00FF0000) >> 16; var textColor = Color.FromRgb((byte)r, (byte)g, (byte)b); ResourceAdornmentManager.TextForegroundColor = textColor; } } } catch (Exception exc) { ExceptionHelper.Log(exc, "Error in LoadSystemTextSettingsAsync"); } }
private FontFamily GetFontFamily(FontAndColorsResourceKey key) { const __FCSTORAGEFLAGS flags = __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS; fncStorage.OpenCategory(key.Category, (uint)flags); try { var fontInfos = new FontInfo[1]; if (fncStorage.GetFont(null, fontInfos) != VSConstants.S_OK) { return(null); } if (fontInfos[0].bFaceNameValid == 1) { return(new FontFamily(fontInfos[0].bstrFaceName)); } return(null); } finally { fncStorage.CloseCategory(); } }
public (string name, float size) GetFontInfo() { ThreadHelper.ThrowIfNotOnUIThread(); try { var fontw = new LOGFONTW[1]; var fontInfo = new FontInfo[1]; ErrorHandler.ThrowOnFailure(_storage.GetFont(fontw, fontInfo)); return(name : fontInfo[0].bstrFaceName, size : fontInfo[0].wPointSize); } catch { return(Control.DefaultFont.Name, Control.DefaultFont.Size); } }
private static void GetSize() { try { IVsFontAndColorStorage storage = (IVsFontAndColorStorage)Package.GetGlobalService(typeof(IVsFontAndColorStorage)); var guid = new Guid("A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0"); if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == VS.VSConstants.S_OK) { LOGFONTW[] Fnt = new LOGFONTW[] { new LOGFONTW() }; FontInfo[] Info = new FontInfo[] { new FontInfo() }; storage.GetFont(Fnt, Info); _fontSize = (int)Info[0].wPointSize; } if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == VS.VSConstants.S_OK) { var info = new ColorableItemInfo[1]; storage.GetItem("Plain Text", info); _backgroundColor = ConvertFromWin32Color((int)info[0].crBackground); } } catch { } }
public static async Task AdjustFontSizeAsync(string categoryGuid, short change) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); IVsFontAndColorStorage storage = await VS.Shell.GetFontAndColorStorageAsync(); Assumes.Present(storage); // ReSharper disable once SuspiciousTypeConversion.Global var utilities = storage as IVsFontAndColorUtilities; if (utilities == null) { return; } var pLOGFONT = new LOGFONTW[1]; var pInfo = new FontInfo[1]; var category = new Guid(categoryGuid); ErrorHandler.ThrowOnFailure(storage.OpenCategory(category, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES))); try { if (!ErrorHandler.Succeeded(storage.GetFont(pLOGFONT, pInfo))) { return; } pInfo[0].wPointSize = checked ((ushort)(pInfo[0].wPointSize + change)); ErrorHandler.ThrowOnFailure(storage.SetFont(pInfo)); ErrorHandler.ThrowOnFailure(utilities.FreeFontInfo(pInfo)); } finally { storage.CloseCategory(); } }
public bool GetTextItemInfo( Guid category, string name, ResourceDictionary values, bool includeFontInfo = false, double?backgroundOpacity = 0.4) { if (fncStorage == null) { return(false); } if (ErrorHandler.Failed(fncStorage.OpenCategory(category, OpenFlags))) { return(false); } try { var itemInfo = new ColorableItemInfo[1]; if (ErrorHandler.Failed(fncStorage.GetItem(name, itemInfo))) { return(false); } if (includeFontInfo) { var fontInfo = new FontInfo[1]; if (ErrorHandler.Failed(fncStorage.GetFont(null, fontInfo))) { return(false); } if (fontInfo[0].bFaceNameValid == 1) { values["Typeface"] = FontUtils.GetTypefaceFromFont(fontInfo[0].bstrFaceName); } if (fontInfo[0].bPointSizeValid == 1) { values["FontRenderingSize"] = FontUtils.FontSizeFromPointSize(fontInfo[0].wPointSize); } values["IsBold"] = (itemInfo[0].dwFontFlags & (uint)FONTFLAGS.FF_BOLD) != 0; } DecodePlainTextColors(ref itemInfo[0]); { Color color = GetWpfColor(itemInfo[0].crForeground); Brush brush = new SolidColorBrush(color); brush.Freeze(); values["Foreground"] = brush; } { Color color = GetWpfColor(itemInfo[0].crBackground); if (backgroundOpacity != null) { color.A = (byte)(255 * backgroundOpacity.Value); } Brush brush = new SolidColorBrush(color); brush.Freeze(); values["Background"] = brush; } return(true); } finally { fncStorage.CloseCategory(); } }
private async void HandleOpenSolution(object sender = null, EventArgs e = null) { await this.JoinableTaskFactory.SwitchToMainThreadAsync(); //Use nested methods to avoid prompt (and need) for multiple MainThead checks/switches IEnumerable <ProjectItem> RecurseProjectItems(ProjectItems projItems) { if (projItems != null) { foreach (ProjectItem item in projItems) { foreach (var subItem in RecurseProjectItem(item)) { yield return(subItem); } } } } IEnumerable <ProjectItem> RecurseProjectItem(ProjectItem item) { yield return(item); foreach (var subItem in RecurseProjectItems(item.ProjectItems)) { yield return(subItem); } } IEnumerable <ProjectItem> GetProjectFiles(Project proj) { foreach (ProjectItem item in RecurseProjectItems(proj.ProjectItems)) { yield return(item); } } // TODO: handle res files being removed or added to a project - currently will be ignored. Issue #2 // Get all resource files from the solution // Do this now, rather than in adornment manager for performance and to avoid thread issues if (await this.GetServiceAsync(typeof(DTE)) is DTE dte) { foreach (var project in dte.Solution.Projects) { foreach (var solFile in GetProjectFiles((Project)project)) { var filePath = solFile.FileNames[0]; var fileExt = System.IO.Path.GetExtension(filePath); // Only interested in resx files if (fileExt.Equals(".resx")) { // Only want neutral language ones, not locale specific versions if (!System.IO.Path.GetFileNameWithoutExtension(filePath).Contains(".")) { ResourceAdornmentManager.ResourceFiles.Add(filePath); } } } } IVsFontAndColorStorage storage = (IVsFontAndColorStorage)VSPackage.GetGlobalService(typeof(IVsFontAndColorStorage)); var guid = new Guid("A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0"); // Seem like reasonabel defaults as should be visible on light & dark theme int _fontSize = 10; Color _textColor = Colors.Gray; if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK) { LOGFONTW[] Fnt = new LOGFONTW[] { new LOGFONTW() }; FontInfo[] Info = new FontInfo[] { new FontInfo() }; storage.GetFont(Fnt, Info); _fontSize = Info[0].wPointSize; } if (storage != null && storage.OpenCategory(ref guid, (uint)(__FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)) == Microsoft.VisualStudio.VSConstants.S_OK) { var info = new ColorableItemInfo[1]; // Get the color value configured for regular string display storage.GetItem("String", info); var win32Color = (int)info[0].crForeground; int r = win32Color & 0x000000FF; int g = (win32Color & 0x0000FF00) >> 8; int b = (win32Color & 0x00FF0000) >> 16; _textColor = Color.FromRgb((byte)r, (byte)g, (byte)b); } ResourceAdornmentManager.TextSize = _fontSize; ResourceAdornmentManager.TextForegroundColor = _textColor; var plural = ResourceAdornmentManager.ResourceFiles.Count > 1 ? "s" : string.Empty; (await this.GetServiceAsync(typeof(DTE)) as DTE).StatusBar.Text = $"String Resource Visualizer initialized with {ResourceAdornmentManager.ResourceFiles.Count} resource file{plural}."; } }