ListGroups (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how list groups can be customized and displayed in list view controls using the properties that are only available in Vista.

Three list groups are created:

  • For the digits
  • For the small letters
  • For the capital letters.

The images for each list item are automatically generated and stored inside a TImageList component.

The example also includes the getImagesFromASCII method that generates a series of images corresponding to the characters within a specified ASCII code interval.

Preliminary Steps

Before running this sample project, in RAD Studio IDE, create a VCL Forms Application project, and perform the following steps:

  1. Put a TListView component on the application form.
  2. In the private part of the TForm1 class, declare the getImagesFromASCII method and the digitsLetters variable of the TImageList type.
  3. In the public part of the TForm1 class, declare the ~TForm1() destructor.
  4. Copy and paste the sample code from the Code section of this page.

Code

__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
  // align the list view to the form
  ListView1->Align = alClient;

  // center and stretch the form to fit the screen
  Form1->Position = poScreenCenter;
  Form1->Height = 600;
  Form1->Width = 800;

  /*
  change the view style of the list view
  such that the icons are displayed
  */
  ListView1->ViewStyle = vsIcon;

  // enable group view
  ListView1->GroupView = True;

  // create a 32 by 32 image list
  digitsLetters = new TImageList(32, 32);

  /*
  generate the DigitsLetters image list with the digits,
  the small letters and the capital letters
  */
  getImagesFromASCII('0', '9');
  getImagesFromASCII('a', 'z');
  getImagesFromASCII('A', 'Z');

  /*
  add an empty image to the list
  used to emphasize the top and bottom descriptions
  of the digits group
  */
  Graphics::TBitmap *image = new Graphics::TBitmap;
  image->Height = 32;
  image->Width = 32;
  digitsLetters->Add(image, NULL);
  delete image;

  // create a title image for the small letters category
  image = new Graphics::TBitmap;
  image->Height = 32;
  image->Width = 32;
  image->Canvas->Brush->Color = clYellow;
  image->Canvas->FloodFill(0, 0, clYellow, fsBorder);
  image->Canvas->Font->Name = "Times New Roman";
  image->Canvas->Font->Size = 14;
  image->Canvas->Font->Color = clRed;
  image->Canvas->TextOut(3, 5, "a..z");
  digitsLetters->Add(image, NULL);
  delete image;

  // create a title image for the capital letters category
  image = new Graphics::TBitmap;
  image->Height = 32;
  image->Width = 32;
  image->Canvas->Brush->Color = clYellow;
  image->Canvas->FloodFill(0, 0, clYellow, fsBorder);
  image->Canvas->Font->Name = "Times New Roman";
  image->Canvas->Font->Size = 13;
  image->Canvas->Font->Color = clRed;
  image->Canvas->TextOut(2, 5, "A..Z");
  digitsLetters->Add(image, NULL);
  delete image;

  // associate the image list with the list view
  ListView1->LargeImages = digitsLetters;
  ListView1->GroupHeaderImages = digitsLetters;

  // set up the digits group
  TListGroup *group = ListView1->Groups->Add();
  group->State = TListGroupStateSet() << lgsNormal << lgsCollapsible;
  group->Header = "Digits";
  group->HeaderAlign = taCenter;
  group->Footer = "End of the Digits category";
  group->FooterAlign = taCenter;
  group->Subtitle = "The digits from 0 to 9";

  /*
  use the empty image as the title image
  to emphasize the top and bottom descriptions
  */
  group->TitleImage = digitsLetters->Count - 3;
  // create the actual items in the digits group
  for (wchar_t c = '0'; c <= '9'; ++c)
  {
    // add a new item to the list view
    TListItem *listItem = ListView1->Items->Add();

    // ...customize it
    listItem->Caption = String(c) + " digit";
    listItem->ImageIndex = c - '0';

    // ...and associate it with the digits group
    listItem->GroupID = group->GroupID;
  }

  // set up the small letters group
  group = ListView1->Groups->Add();
  group->State = TListGroupStateSet() << lgsNormal << lgsCollapsible;
  group->Header = "Small Letters";
  group->HeaderAlign = taRightJustify;
  group->Footer = "End of the Small Letters category";
  group->FooterAlign = taLeftJustify;
  group->Subtitle = "The small letters from 'a' to 'z'";
  group->TitleImage = digitsLetters->Count - 2;

  // create the actual items in the small letters group
  for (wchar_t c = 'a'; c <= 'z'; ++c)
  {
    // add a new item to the list view
    TListItem *listItem = ListView1->Items->Add();

    // ...customize it
    listItem->Caption = String("letter ") + c;
    listItem->ImageIndex = c - 'a' + 10;

    // ...and associate it with the small letters group
    listItem->GroupID = group->GroupID;
  }

  /*
  to see how the NextGroupID property can be used,
  the following lines of code show how an item can be associated
  with a group ID, prior to creating the group
  */

  // create the actual items in the capital letters group
  for (wchar_t c = 'A'; c <= 'Z'; ++c)
  {
    // add a new item to the list view
    TListItem *listItem = ListView1->Items->Add();

    // ->->->customize it
    listItem->Caption = String("letter ") + c;
    listItem->ImageIndex = c - 'A' + 36;

    // ->->->and associate it with the capital letters group
    listItem->GroupID = ListView1->Groups->NextGroupID;
  }

  // set up the capital letters group
  group = ListView1->Groups->Add();
  group->State = TListGroupStateSet() << lgsNormal << lgsCollapsible;
  group->Header = "Capital Letters";
  group->HeaderAlign = taRightJustify;
  group->Footer = "End of the Capital Letters category";
  group->FooterAlign = taLeftJustify;
  group->Subtitle = "The capital letters from 'A' to 'Z'";
  group->TitleImage = digitsLetters->Count - 1;
}
//---------------------------------------------------------------------------

/*
Generates a series of images for the characters
starting with ASCII code First and ending with Last.
All images are added to the DigitsLetters variable.
*/
void TForm1::getImagesFromASCII(wchar_t first, wchar_t last)
{
  for (wchar_t c = first; c <= last; ++c)
  {
    Graphics::TBitmap *image = new Graphics::TBitmap;
    image->Height = 32;
    image->Width = 32;
    image->Canvas->Font->Name = "Times New Roman";
    image->Canvas->Font->Size = 22;
    image->Canvas->TextOut((image->Width - image->Canvas->TextWidth(c)) / 2, 0, c);
    digitsLetters->Add(image, NULL);
    delete image;
  }
}
//---------------------------------------------------------------------------

__fastcall TForm1::~TForm1()
{
  // remove the image list from memory
  delete digitsLetters;
}
//---------------------------------------------------------------------------

Uses

Personal tools
Assistance