从ColorableItemInfo获取Visual Studio背景色

本文关键字:Studio 背景色 Visual 获取 ColorableItemInfo | 更新日期: 2023-09-27 18:04:36

好了,这段代码让你可以在VS中访问用户设置的背景色:

        System.Guid guid = new System.Guid("{58E96763-1D3B-4E05-B6BA-FF7115FD0B7B}");
        IVsFontAndColorStorage fontAndColorStorage =
           Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider
           .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'FontAndColo‌​rs'Text Editor, where VS_VER is the actual Visual Studio version: 10.0, 11.0, 12.0, 14.0, etc.
            if (fontAndColorStorage.OpenCategory(guid/*Microsoft.VisualStudio.Editor.DefGuidList.guidTextEditorFont‌​Category*/,
                (uint)__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS) == VSConstants.S_OK)
            {
                ColorableItemInfo[] info = new ColorableItemInfo[] { new ColorableItemInfo() };
                fontAndColorStorage.GetItem("Plain Text", info);
                fontAndColorStorage.CloseCategory();
            }
        }

如何从ColorableItemInfouint crBackground性质中得到实际的System.Color ??

从ColorableItemInfo获取Visual Studio背景色

编辑:这并不总是有效-如果主题是白色,它会得到黑色。

                byte[] intBytesFO = BitConverter.GetBytes(info[0].crForeground);
                Color colBG = Color.FromArgb(intBytesBG[3], intBytesBG[0], intBytesBG[1], intBytesBG[2]);