Share via


How to: Add Colors to User Interface Elements

By using the Visual Studio SDK, you can create a consistent user interface (UI) that supports themes in Windows and multiple color schemes. When you draw a UI element, you should first query the Visual Studio integrated development environment (IDE) to determine the appropriate color for that kind of element.

The SVsUIShell service implements coloring support for UI elements in Visual Studio through the IVsUIShell2 interface.

To use the Visual Studio coloring support

  1. Obtain an instance of the IVsUIShell2 interface.

    For COM-based VSPackages, call QueryService, with a service ID of SID_SVsUIShell and an interface ID of IID_IVsUIShell2.

    For managed code-based VSPackages, call Package.GetService with an argument of (typeof(SVsUIShell)) and cast the result to the IVsUIShell2 interface.

  2. Choose a value from the __VSSYSCOLOREX enumeration to specify the kind of component to be colored.

    Detailed guidelines on choosing appropriate colors by name for a given UI element are discussed in User Interface Design Guidelines.

  3. Use the selected member of the __VSSYSCOLOREX enumeration as an argument to the GetVSSysColorEx method of the IVsUIShell2 interface to obtain the RGB value with which to paint the element.

  4. Use the returned color information.

    The color information is always returned as a 32-bit Windows color value in a COLOREF format.

    VSPackages that are written in unmanaged code or C++ can access this value by using COLOREF macros.

    VSPackages that are created by using managed code should use an instance of the Color structure, which can be obtained from the 32-bit Windows color value by using FromWin32.

Example

The following method paints the background of a tooltip when invoked by the paint event of a managed control.

Private Sub VSColorPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    ' Get IVSUIShell2.
    Dim uiShell2 As IVsUIShell2 = TryCast(GetService(GetType(SVsUIShell)), IVsUIShell2)

    Debug.Assert(Not uiShell2 Is Nothing, "failed to get IVsUIShell2")

    If Not uiShell2 Is Nothing Then
        ' Get the COLORREF structure.
        Dim win32Color As UInteger
        uiShell.GetVSSysColorEx(__VSSYSCOLOREX.VSCOLOR_SMARTTAG_HOVER_FILL as Int32, win32Color)

        ' Translate it to a managed Color structure.
        Dim myColor As Color = ColorTranslator.FromWin32(CInt(Fix(win32Color)))
        e.Graphics.FillRectangle(New SolidBrush(myColor), 0, 0, 100, 100)
    End If
End Sub
private void VSColorPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Get IVSUIShell2.
    IVsUIShell2 uiShell2 = GetService(typeof(SVsUIShell)) as IVsUIShell2;

    Debug.Assert (uiShell2 != null, "failed to get IVsUIShell2");

    if (uiShell2 != null)
    {
        // Get the COLORREF structure.
        uint win32Color;
        uiShell.GetVSSysColorEx((int)__VSSYSCOLOREX.VSCOLOR_SMARTTAG_HOVER_FILL, out win32Color);

        // Translate it to a managed Color structure.
        Color myColor = ColorTranslator.FromWin32((int)win32Color);
        e.Graphics.FillRectangle(new SolidBrush(myColor), 0, 0, 100, 100);
    }
}

In the above example, the method:

  • Obtains an IVsUIShell2 interface.

  • Queries the shell for the correct value for a ToolTip's background.

  • Gets the appropriate 32-bit windows color value.

  • Converts the color value to a System.Drawing.Color structure.

  • Fills the tip's background rectangle with the specific color.

See Also

Reference

COLOREF

Color

FromWin32

Other Resources

Colors and Gradients