Creating Custom Palettes in Delphi.Net

Creating my own custom palettes is not something that I have had any real experience with, but the Essentials component suite uses custom palettes so I had to delve into this.  In Win32, you used a TLogPalette record as a template.   You had to allocate memory for a TLogPalette structure large enough to hold the number of palette entries that you were going to need.  Something like this:


var
plt: PLogPalette;
size: Integer;
begin
size := SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * numEntries);
GetMem(plt, size);
plt.palVersion := $300;
plt.palNumEntries := numEntries’

Obviously, this is considered unsafe in a .Net program.  Fortunately, CodeGear has created another record that is large enough to hold the biggest possible palette.  So, rather than allocating memory based on the TLogPalette record template, you just use a TMaxLogPalette record.  This record is defined with 256 palette entries, which is the maximum that you can have.   Like this:

var
plt: TMaxLogPalette;
begin
plt.palVersion := $300;
plt.palNumEntries := numEntries;

Leave a Reply