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





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  

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


Answer:
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.

Example:

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

 {Allocate the memory used by the palette}
  GetMem(lpPalette,
         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+}
   {$DEFINE CKRANGE}
   {$R-}
 {$ENDIF}

 {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;
  end;

 {Turn range checking back on if it was on when we started}
 {$IFDEF CKRANGE}
   {$UNDEF CKRANGE}
   {$R+}
 {$ENDIF}

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

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

 {Do something with the palette here}

 {Delete the palette handle after use}
  DeleteObject(hPal);
end;

7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99