Using TListWindow (continued)

Adding Columns

Addings columns to the list window is simple. The function InsertColumn takes an index and a TListWindColumn. The index (starting from zero) is the position of the column from left to right. To create the TListWindColumn, we just specify the title, width and, where necessary, the alignment:

void TLWWindow::AddListColumns()
{
  List->InsertColumn(0, TListWindColumn("Name"200));
  List->InsertColumn(1, TListWindColumn("Size"80, TListWindColumn::Right));
  List->InsertColumn(2, TListWindColumn("Type"120));
  List->InsertColumn(3, TListWindColumn("Modified"200));
}

Setting the Imagelist

To keep the usage of imagelists simple, we will create each one from a single bitmap.
  1. We add 2 bitmaps to our resources:  
    Don't worry about the ugly green colour - it is being used as a mask.
  2. We add the code to create and delete the imagelists:

    TLWWindow::TLWWindow(TWindow* parent,
                         const char far* title,
                         TModule* module)
    :
      TWindow(parent, title, module)
    {
      // ...

      // create the imagelists for the list window from a bitmap
      //
      SmallImg = new TImageList(*GetApplication(),
                                  // the app instance
                                TResId(IDB_SMALLIMAGES),
                                  // the bitmap to use
                                16,
                                  // width of each image
                                1,
                                  // amount to grow by
                                TColor::LtGreen,
                                  // mask colour
                                IMAGE_BITMAP,
                                  // type of resource
                                0);
                                  // other flags
      LargeImg = new TImageList(*GetApplication(),
                                TResId(IDB_LARGEIMAGES),
                                321, TColor::LtGreen,
                                IMAGE_BITMAP, 0);
    }

    TLWWindow::~TLWWindow()
    {
      // ...

      delete SmallImg;
      delete LargeImg;
    }

  3. Lastly we add the imagelist to the list window:

    void TLWWindow::SetupWindow()
    {
      // ...

      // set the image lists for use with the list window
      //
      List->SetImageList(*SmallImg, TListWindow::Small);
      List->SetImageList(*LargeImg, TListWindow::Normal);
    }

Adding Items

The items we will add will be the contents of the current directory. When an item is first added (that is, a new row) you must call AddItem, however to add extra columns (subitems) to that row you call SetItemText (or SetItem with Borland C++ 5.01) and specify a different text string for the column.

This code is for Borland C++ 5.02 only. If you are using Borland C++ 5.01, click here.

void TLWWindow::AddListItems()
{
  HANDLE findHandle;
  WIN32_FIND_DATA findData;
  char text[256];
  SYSTEMTIME st;

  // remove all of the items from the list
  //
  List->DeleteAllItems();

  // open the current directory for reading
  //
  if ((findHandle = FindFirstFile(".\\*.*", &findData)) == INVALID_HANDLE_VALUE)
    return;

  // add each of the files in the directory
  //
  do
  {
    // add the name
    //
    TListWindItem item(findData.cFileName);
    item.SetImageIndex(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0);
    int index = List->AddItem(item);

    // add the size of the item
    //
    itoa((findData.nFileSizeHigh * MAXDWORD) + findData.nFileSizeLow, text, 10);
    List->SetItemText(index, 1, text);

    // add the type of the item
    //
    if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
      List->SetItemText(index, 2, "Directory");
    else
      List->SetItemText(index, 2, "File");

    // add the time modified
    //
    FileTimeToSystemTime(&findData.ftLastWriteTime, &st);
    TTime time(TDate(st.wDay, st.wMonth, st.wYear), st.wHour, st.wMinute, st.wSecond);
    List->SetItemText(index, 3, time.AsString().c_str());
  }
  while (FindNextFile(findHandle, &findData));

  FindClose(findHandle);
}

Previous Page
Next Page