How To Reference Colors in a DIB Section by Index (138256)



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional
  • Microsoft Win32 Software Development Kit (SDK) 3.1
  • Microsoft Win32 Software Development Kit (SDK) 3.5
  • Microsoft Win32 Software Development Kit (SDK) 3.51
  • Microsoft Win32 Software Development Kit (SDK) 4.0

This article was previously published under Q138256

SUMMARY

When using the GDI with DIB sections (HBITMAPs returned by a call to CreateDIBSection()), you may need a way to reference the colors in the color table of the DIB section by index rather than by RGB value. This article shows you how.

MORE INFORMATION

To reference a color table value by index, use the DIBINDEX macro defined in Mmsystem.h. It allows you to index the colors in the color table of a DIB section in a manner similar to the way PALETTEINDEX indexes a color in a logical palette. In other words, you can create COLORREFs that reference a DIB section's color table rather than the logical palette in the DC that the DIB section is selected into. Because DIBINDEX references values in a color table, it only works on DCs where you have a DIB section selected.

The DIBINDEX macro accepts an index to a color table entry and returns a color table specifier consisting of a 32-bit COLORREF value that specifies the color associated with the given index. An application using a display context with a DIB section selected into it can pass this specifier, instead of an explicit red, green, blue (RGB) value, to GDI functions that expect a color. This allows the function to use the color at the specified color table index.

LONG DIBINDEX(
    WORD  wColorTableIndex    // index to color table entry
   );

Parameters
  wColorTableIndex - Specifies an index to the palette entry containing
                     the color to be used for a graphics operation.

Return Value
  The return value is a color table index specifier.

Remarks
  DIBINDEX is defined as:

  #define DIBINDEX(n)     MAKELONG((n),0x10FF)
				


NOTE: DIBINDEX also works with 16-bit WinGBitmaps and WinGDCs.

Modification Type:MinorLast Reviewed:3/21/2005
Keywords:kbcode kbhowto KB138256