Sample Application: CPPDragFileTest.cpp

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

Design-time form image
Project source: CPPDragFileTest.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("CPPDragFileTest.res");
USEFORM("fmCPPDragFileTest.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: fmCPPDragFileTest.h

//---------------------------------------------------------------------------
#ifndef fmCPPDragFileTestH
#define fmCPPDragFileTestH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "uoole.hpp"
#include <FileCtrl.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TDriveComboBox *DriveComboBox1;
        TDirectoryListBox *DirectoryListBox1;
        TFileListBox *FileListBox1;
        TUOTextSource *UOTextSource1;
        TCheckBox *CheckBox1;
        TCheckBox *CheckBox2;
        TCheckBox *CheckBox3;
        TLabel *Label1;
        void __fastcall FileListBox1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall CheckBox1Click(TObject *Sender);
        void __fastcall FormCreate(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Back to top
Form source: fmCPPDragFileTest.cpp

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

#include "fmCPPDragFileTest.h"
#include "uoUtil.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "uoole"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FileListBox1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{ TStringList* aSL;
  POINT pt;

  pt.x = X;  pt.y = Y;

  // Look for a drag
  if (DragDetect(Handle, pt)) {
    // We've started a drag.  Clear the drag and drop data
    UOTextSource1->CustomFormatData->Clear();


    aSL = new TStringList;
    try  {
      // Get the single selected file name in the file list, and put it in the string list
      aSL->Add(FileListBox1->FileName);

      // Create and add the shell link format
      UOTextSource1->CustomFormatData->AddFormat("Shell IDList Array",
        uoShellIDListFromFileList("", aSL));
      // Create and add the file copy format
      UOTextSource1->CustomFormatData->AddFormat(IntToStr(CF_HDROP),
        uoHDROPFromFileList("", aSL));

      // Execute the drag
      UOTextSource1->Execute();
    }
    __finally {
      // Clean up
      aSL->Free();
    }

  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
// One of the checkboxes has been clicked.  Build the drop effects for the drag.
{ TDropEffects de;

  de.Clear();

  if (CheckBox1->Checked) {
    de << deMove;
  }
  if (CheckBox2->Checked) {
    de << deCopy;
  }
  if (CheckBox3->Checked) {
    de << deLink;
  }

  UOTextSource1->DropEffects = de;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Initialize the checkboxes to the initial state of the
  CheckBox1->Checked = UOTextSource1->DropEffects.Contains(deMove);
  CheckBox2->Checked = UOTextSource1->DropEffects.Contains(deCopy);
  CheckBox3->Checked = UOTextSource1->DropEffects.Contains(deLink);
}
//---------------------------------------------------------------------------

Back to top
Form source: fmCPPDragFileTest.dfm

object Form1: TForm1
  Left = 178
  Top = 142
  Width = 324
  Height = 258
  Caption = 'Drag files and shortcuts'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 6
    Width = 281
    Height = 26
    Caption = 
      'Drag a file from the file list at the right to another applicati' +
      'on (including Explorer or the desktop).'
    WordWrap = True
  end
  object DriveComboBox1: TDriveComboBox
    Left = 8
    Top = 48
    Width = 145
    Height = 19
    DirList = DirectoryListBox1
    TabOrder = 0
  end
  object DirectoryListBox1: TDirectoryListBox
    Left = 8
    Top = 72
    Width = 145
    Height = 97
    FileList = FileListBox1
    ItemHeight = 16
    TabOrder = 1
  end
  object FileListBox1: TFileListBox
    Left = 160
    Top = 48
    Width = 145
    Height = 121
    ItemHeight = 13
    TabOrder = 2
    OnMouseDown = FileListBox1MouseDown
  end
  object CheckBox1: TCheckBox
    Left = 8
    Top = 172
    Width = 297
    Height = 17
    Caption = 'Allow file move'
    TabOrder = 3
    OnClick = CheckBox1Click
  end
  object CheckBox2: TCheckBox
    Left = 8
    Top = 190
    Width = 297
    Height = 17
    Caption = 'Allow file copy'
    TabOrder = 4
    OnClick = CheckBox1Click
  end
  object CheckBox3: TCheckBox
    Left = 8
    Top = 209
    Width = 297
    Height = 17
    Caption = 'Allow file link (shortcut)'
    TabOrder = 5
    OnClick = CheckBox1Click
  end
  object UOTextSource1: TUOTextSource
    DropEffects = [deCopy, deLink]
    Left = 224
    Top = 176
  end
end
Back to top

Back to the examples page