C++Builder Tips


エクスプローラからドロップ


エクスプローラからフォームにドロップしてそのファイル名を得るサンプルですが、本サンプルではもうちょっと凝ってみてドロップされたコンポーネントにファイル名を一覧表示します。


//---------------------------------------------------------------------------
// Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE 管理のコンポーネント
  TListBox *ListBox1;
  TMemo *Memo1;
  void __fastcall FormShow(TObject *Sender);
  void __fastcall FormCreate(TObject *Sender);
private:
  void __fastcall WMDROPFILES(TWMDropFiles & msg); // ユーザー宣言
public: // ユーザー宣言
  __fastcall TForm1(TComponent* Owner);
protected:
  BEGIN_MESSAGE_MAP
    VCL_MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WMDROPFILES)
  END_MESSAGE_MAP(TForm)

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------
// Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Memo1->Clear();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
  DragAcceptFiles(Handle,true);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::WMDROPFILES(TWMDropFiles & msg)
{
  int iMax = DragQueryFile((HDROP)msg.Drop,0xFFFFFFFF,NULL,255);
  char cFileName[256];
  TPoint pntXY;
  TControl *ctl;

  for (int i=0;i<iMax;i++){
    DragQueryFile((HDROP)msg.Drop,i,cFileName,255);
    GetCursorPos(&pntXY); //マウスカーソルの座標取得
    ctl = FindDragTarget(pntXY,false); //マウスカーソルの下のコンポーネントを取得
    if (ctl->Name=="Memo1"){
      Memo1->Lines->Add(cFileName);
    }
    if (ctl->Name=="ListBox1"){
      ListBox1->Items->Add(cFileName);
    }
  }
  DragFinish((HDROP)msg.Drop);
}
//---------------------------------------------------------------------------

DownLoad bcbtips066.lzh 4KB(BCB5)