Sample Application: CPPFileTest.cpp

Project source
Form header (C++)
Form source (C++)
Form source (DFM)

Design-time form image
Project source: CPPFileTest.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("CPPFileTest.res");
USEFORM("fmCPPFileTest.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    try
    {
        Application->Initialize();
        Application->CreateForm(__classid(TForm1), &Form1);
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    return 0;
}
//---------------------------------------------------------------------------
Back to top
Form header: fmCPPFileTest.h

//---------------------------------------------------------------------------
#ifndef fmCPPFileTestH
#define fmCPPFileTestH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "uoole.hpp"
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TPanel *Panel1;
    TListView *ListView1;
    TUOTextTarget *UOTextTarget1;
    TPanel *Panel2;
    TPanel *Panel3;
    TButton *Button1;
    void __fastcall UOTextTarget1Drop(TObject *Sender,
          TWinControl *Acceptor, const AnsiString dropText, int X, int Y);
    void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Back to top
Form source: fmCPPFileTest.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "fmCPPFileTest.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "uoole"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
//
long __fastcall GetFileSize(const AnsiString FileName)
{
  // Utility function to get the size of any given file.
  // Fixed version of one in fmxUtils - you have to call FindClose()
  // or you can get problems in NTFS
  long aLong;
  TSearchRec SearchRec;

  if (FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec)==0) {
    aLong = SearchRec.Size;
    FindClose(SearchRec);
  }
  else
    aLong = -1;

  return aLong;
}

void __fastcall TForm1::UOTextTarget1Drop(TObject *Sender,
      TWinControl *Acceptor, const AnsiString dropText, int X, int Y)
{
  // A list of files (maybe containing only a single file) has been dropped
  // on us.  Add the files to the list view.
  TUOTextTarget* uott = (TUOTextTarget*) Sender;
  int j;
  TListItem* aLi;
  int attr;
  AnsiString s;
  TDateTime fileDate;
  double fileSize;

  ListView1->Items->BeginUpdate(); // Efficiency

  // The list is in uott->DroppedLines, a TStringList
  for (j = 0; j < uott->DroppedLines->Count; j++) {
    // Get the name of the current file
    s = uott->DroppedLines->Strings[j];
    // Add an item to the list
    aLi = ListView1->Items->Add();
    // Its caption should be the file name
    aLi->Caption = s;
    // Get the file attributes for the file
    attr = FileGetAttr(s);
    // Is it a folder or a regular file?  If you want more info, e.g., the
    // registration info, icon, etc. for this file type, then you can use
    // the function SHGetFileInfo() instead
    attr = FileGetAttr(s);
    if ((attr & faDirectory) == faDirectory) {
      // It's a directory
      aLi->SubItems->Add("");  // No size entry
      aLi->SubItems->Add("File Folder");
    } else {
      // It's a regular file
      fileDate = FileDateToDateTime(FileAge(s)); // Date as a TDateTime
      fileSize = GetFileSize(s); // Size in bytes
      aLi->SubItems->Add(Format("%0.0n bytes", ARRAYOFCONST((fileSize))));
      aLi->SubItems->Add("Regular File");
      aLi->SubItems->Add(DateTimeToStr(fileDate));
    };
  }
  ListView1->Items->EndUpdate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Clear the list view 
  ListView1->Items->BeginUpdate();
  ListView1->Items->Clear();
  ListView1->Items->EndUpdate();
}
//---------------------------------------------------------------------------
Back to top
Form source: fmCPPFileTest.dfm

object Form1: TForm1
  Left = 165
  Top = 199
  Width = 570
  Height = 319
  Caption = 'Drag files onto this list view'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 562
    Height = 256
    Align = alClient
    BevelOuter = bvNone
    BorderWidth = 3
    Caption = 'Panel1'
    TabOrder = 0
    object ListView1: TListView
      Left = 3
      Top = 3
      Width = 556
      Height = 250
      Align = alClient
      ColumnClick = False
      Columns = <
        item
          Caption = 'Name'
          Width = 225
        end
        item
          Alignment = taRightJustify
          Caption = 'Size'
          Width = 125
        end
        item
          Caption = 'Type'
          Width = 80
        end
        item
          Caption = 'Modified'
          Width = 120
        end>
      TabOrder = 0
      ViewStyle = vsReport
    end
  end
  object Panel2: TPanel
    Left = 0
    Top = 256
    Width = 562
    Height = 36
    Align = alBottom
    BevelOuter = bvNone
    TabOrder = 1
    object Panel3: TPanel
      Left = 431
      Top = 0
      Width = 131
      Height = 36
      Align = alRight
      BevelOuter = bvNone
      TabOrder = 0
      object Button1: TButton
        Left = 29
        Top = 6
        Width = 97
        Height = 25
        Caption = 'Clear file list'
        TabOrder = 0
        OnClick = Button1Click
      end
    end
  end
  object UOTextTarget1: TUOTextTarget
    AcceptorControl = ListView1
    AcceptTextFormats = [dtfFiles]
    OnDrop = UOTextTarget1Drop
    Left = 64
    Top = 64
  end
end
Back to top

Back to the examples page