Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Creating a palette with more than one entry


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

    procedure TForm .Button Click(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;
    

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use