Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:3k
源码类别:

VC书籍

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. //---------------------------------------------------------------------------
  10. __fastcall TForm1::TForm1(TComponent* Owner)
  11.         : TForm(Owner)
  12. {
  13. }
  14. //---------------------------------------------------------------------------
  15. void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
  16.       TRect &Rect, TOwnerDrawState State)
  17. {
  18.   Graphics::TBitmap *pBitmap;
  19.   int     Offset = 2;
  20.   TCanvas *pCanvas = ((TListBox *)Control)->Canvas;
  21.   pCanvas->FillRect(Rect); // clear the rectangle
  22.   pBitmap = (Graphics::TBitmap *)((TListBox *)Control)->Items->Objects[Index];
  23.   if (pBitmap)
  24.   {
  25.     pCanvas->CopyRect(Bounds(Rect.Left + Offset, Rect.Top, pBitmap->Width, pBitmap->Height), pBitmap->Canvas,Bounds(0, 0, pBitmap->Width, pBitmap->Height));
  26.     Offset += pBitmap->Width + 4;   // add four pixels between bitmap and text
  27.   }
  28.   // display the text
  29.   pCanvas->TextOut(Rect.Left + Offset, Rect.Top+6,((TListBox *)Control)->Items->Strings[Index]);
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TForm1::DirectoryListBox1Change(TObject *Sender)
  33. {
  34.   ListBox1->Clear();
  35.   TSearchRec sr;
  36.   int iAttributes = 0;
  37.   iAttributes |=faHidden;
  38.   iAttributes |=faSysFile;
  39.   if (FindFirst(DirectoryListBox1->Directory+"\*.*", iAttributes, sr) == 0)
  40.   {
  41.     AddFileToList(sr.Name,sr.Attr);
  42.     while (FindNext(sr) == 0)
  43.     {
  44.       AddFileToList(sr.Name,sr.Attr);
  45.     }
  46.     FindClose(sr);
  47.   }
  48. }
  49. //---------------------------------------------------------------------------
  50. void __fastcall TForm1::AddFileToList(AnsiString filename,int attr)
  51. {
  52.   switch(attr)
  53.   {
  54.     case faSysFile:
  55.       ListBox1->Items->AddObject(filename,Image4->Picture->Bitmap);
  56.       break;
  57.     case faHidden:
  58.       ListBox1->Items->AddObject(filename,Image3->Picture->Bitmap);
  59.       break;
  60.     case faReadOnly:
  61.       ListBox1->Items->AddObject(filename,Image2->Picture->Bitmap);
  62.       break;
  63.     case faArchive:
  64.       ListBox1->Items->AddObject(filename,Image1->Picture->Bitmap);
  65.       break;
  66.     default:
  67.       break;
  68.   }
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TForm1::DriveComboBox1Change(TObject *Sender)
  72. {
  73.   DirectoryListBox1->Drive=DriveComboBox1->Drive;
  74. }
  75. //---------------------------------------------------------------------------