IVsHiColorItem 接口

提供对返回完整的 24 位颜色值支持。

命名空间:  Microsoft.VisualStudio.TextManager.Interop
程序集:  Microsoft.VisualStudio.TextManager.Interop.8.0(在 Microsoft.VisualStudio.TextManager.Interop.8.0.dll 中)

语法

声明
<InterfaceTypeAttribute()> _
<GuidAttribute("16C240B3-4773-43C2-932A-1E8DD2F6F0F8")> _
Public Interface IVsHiColorItem
[InterfaceTypeAttribute()]
[GuidAttribute("16C240B3-4773-43C2-932A-1E8DD2F6F0F8")]
public interface IVsHiColorItem
[InterfaceTypeAttribute()]
[GuidAttribute(L"16C240B3-4773-43C2-932A-1E8DD2F6F0F8")]
public interface class IVsHiColorItem
[<InterfaceTypeAttribute()>]
[<GuidAttribute("16C240B3-4773-43C2-932A-1E8DD2F6F0F8")>]
type IVsHiColorItem =  interface end
public interface IVsHiColorItem

IVsHiColorItem 类型公开以下成员。

方法

  名称 说明
公共方法 GetColorData 检索具有指定元素的 RGB 值。

页首

备注

此接口用于提供对 24 位或深颜色值支持。 它在实现 IVsColorableItemIVsPackageDefinedTextMarkerType 接口的同一个类中实现。

对实现者的说明

如果为的颜色值的支持是必需的,在实现 IVsColorableItemIVsPackageDefinedTextMarkerType 接口相同的类必须实现此接口。

对调用者的说明

如果此接口可以从对象获取实现 IVsColorableItemIVsPackageDefinedTextMarkerType 接口,则该对象播发为的颜色值支持。 调用 GetColorData 方法获取各个前景色、背景和线条颜色的 RGB 值。 如果 GetColorData 方法返回错误,请正常低于回访问原始 IVsColorableItemIVsPackageDefinedTextMarkerType 接口的颜色。

示例

这是在托管代码中的一个示例演示如何实现 IVsHiColorItem 接口和其实例化。 说明设置,红色、绿色和蓝色值如何转换为 MyRGB 构造函数的 COLORREF 。 此示例是从语言服务。

using Microsoft.VisualStudio.TextManager.Interop;

namespace MyNamespace
{
    internal struct MyRGB
    {
        public uint ColorRef;

        public MyRGB(int r, int g, int b)
        {
            ColorRef = (uint)System.Drawing.ColorTranslator.ToWin32(
                                 System.Drawing.Color.FromArgb(r, g, b));
        }
        public MyRGB(bool fInvalid)
        {
            ColorRef = unchecked((uint)-1);
        }

        public bool IsValid()
        {
            return ColorRef != unchecked((uint)-1);
        }
    }


    internal class MyColorItem : IVsColorableItem, IVsHiColorItem
    {
        // Indicates that the returned RGB value is really an index
        // into a predefined list of colors.
        private const uint COLOR_INDEXED = 0x01000000;

        //==========================================================
        // Private fields.
        private COLORINDEX foreColor;
        private COLORINDEX backColor;
        private FONTFLAGS  fontFlags;
        private MyRGB      foreRGB;
        private MyRGB      backRGB;
        private string     name;
        private string     displayName;

        //==========================================================
        // Public constructors.

        public MyColorItem(string name,
                           string displayName,
                           COLORINDEX foreColor,
                           COLORINDEX backColor,
                           FONTFLAGS fontFlags)
        {
            this.name        = name;
            this.displayName = displayName;
            this.foreColor   = foreColor;
            this.backColor   = backColor;
            this.fontFlags   = fontFlags;
            this.foreRGB     = new MyRGB(false);
            this.backRGB     = new MyRGB(false);
        }

        public MyColorItem(string name,
                           string displayName,
                           COLORINDEX foreColor,
                           COLORINDEX backColor,
                           FONTFLAGS fontFlags,
                           MyRGB foreRGB,
                           MyRGB backRGB)
        {
            this.name        = name;
            this.displayName = displayName;
            this.foreColor   = foreColor;
            this.backColor   = backColor;
            this.fontFlags   = fontFlags;
            this.foreRGB     = foreRGB;
            this.backRGB     = backRGB;
        }

        //==========================================================
        // IVsColorableItem methods.
        #region IVsColorableItem Members

        int IVsColorableItem.GetDefaultColors(COLORINDEX[] piForeground,
                                              COLORINDEX[] piBackground)
        {
            int retval = VSConstants.E_POINTER;
            if (piForeground != null)
            {
                piForeground[0] = this.foreColor;
                retval = VSConstants.S_OK;
            }
            if (piBackground != null)
            {
                piBackground[0] = this.backColor;
            }
            return retval;
        }

        int IVsColorableItem.GetDefaultFontFlags(out uint pdwFontFlags)
        {
            pdwFontFlags = (uint)this.fontFlags;
            return VSConstants.S_OK;
        }

        int IVsColorableItem.GetDisplayName(out string pbstrName)
        {
            pbstrName = this.displayName;
            return VSConstants.S_OK;
        }

        #endregion


        //==========================================================
        // IVsHiColorItem methods.
        #region IVsHiColorItem Members

        int IVsHiColorItem.GetColorData(int cdElement, out uint pcrColor)
        {
            int retval = VSConstants.E_NOTIMPL;
            pcrColor = 0;

            switch ((__tagVSCOLORDATA)cdElement)
            {
                case __tagVSCOLORDATA.CD_BACKGROUND:
                    pcrColor = this.backRGB.IsValid() ?
                                   this.backRGB.ColorRef :
                                   (uint)backColor | COLOR_INDEXED;
                    retval = VSConstants.S_OK;
                    break;

                case __tagVSCOLORDATA.CD_FOREGROUND:
                case __tagVSCOLORDATA.CD_LINECOLOR:
                    pcrColor = this.foreRGB.IsValid() ?
                                   this.foreRGB.ColorRef :
                                   (uint)foreColor | COLOR_INDEXED;
                    retval = VSConstants.S_OK;
                    break;

                default:
                    retval = VSConstants.E_INVALIDARG;
                    break;
            }
            return retval;
        }

        #endregion
    }


    //==============================================================
    // Example of how to instantiate the MyColorItem class.

    public class MyLanguageService
    {
        private ColorableItem[] colorableItemsList;

        public MyLanguageService()
        {
            colorableItemsList = {
                new MyColorableItem("MyLanguage- Text",
                                    "MyLanguage- Text",
                                    COLORINDEX.CI_SYSPLAINTEXT_FG,
                                    COLORINDEX.CI_SYSPLAINTEXT_BK,
                                    FONTFLAGS.FF_BOLD),

                new MyColorableItem("MyLanguage- Keyword",
                                    "MyLanguage- Keyword",
                                    COLORINDEX.CI_MAROON,
                                    COLORINDEX.CI_SYSPLAINTEXT_BK,
                                    FONTFLAGS.FF_BOLD,
                                    new MyRGB(192,64,0),
                                    new MyRGB(false)),

                new MyColorableItem("MyLanguage- Operator",
                                    "MyLanguage- Operator",
                                    COLORINDEX.CI_DARKBLUE,
                                    COLORINDEX.CI_BLUE,
                                    FONTFLAGS.FF_PLAIN,
                                    new MyRGB(0,64,192),
                                    new MyRGB(128,128,255))
            };
        }
    }
}

请参阅

参考

Microsoft.VisualStudio.TextManager.Interop 命名空间