Главная страница | назад

Article #15890: Creating a palette with more than one entry

 Question and Answer Database

FAQ890D.txt   Creating a palette with more than one entry
Category   :Windows API
Platform    :All
Product    :All 32 bit  

The RTL defines the TLogPallette as having only one palette entry.
How can I create a palette with more than one entry?

The RTL defines the TLogPallette as having only one entry to allow
you to dynamically create a palette with the number of palette entries
you wish. To access the additional entries, range checking must be
turned off (this can be done in code), and you must access the palette
entry index using a variable.

The following example demonstrates dynamically creating a logical
palette structure with 256 entries, turns off range checking (if it is
on), accesses the palette entries by filling them with gray shades,
turns range checking back on (if it was off), creates a handle to
palette, and then cleans up the allocated resources.


procedure TForm1.Button1Click(Sender: TObject);
  lpPalette : PLogPalette;
  hPal : hPalette;
  i : integer;

 {Allocate the memory used by the palette}
         sizeof(TLogPalette) + (255 * sizeof(TPaletteEntry)));

 {Fill out the palette header}
  lpPalette^.palVersion := $300;
  lpPalette^.palNumEntries := 256;

 {Turn range checking off if it is on and}
 {remember the range checking state}
 {$IFOPT R+}

 {Fill in the palette structure color table with shades of gray}
  for i := 0 to 255 do begin
    lpPalette^.PalPalEntry[i].peRed := i;
    lpPalette^.PalPalEntry[i].peGreen := i;
    lpPalette^.PalPalEntry[i].peBlue := i;

 {Turn range checking back on if it was on when we started}

 {Create a palette handle}
  hPal := CreatePalette(lpPalette^);

 {Free the memory use by the palette structure}
          sizeof(TLogPalette) + (255 * sizeof(TPaletteEntry)));

 {Do something with the palette here}

 {Delete the palette handle after use}

7/16/98 4:31:28 PM

Last Modified: 01-SEP-99