LoginSignup
2
1

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > fileIO > ファイルのドロップに対応

Last updated at Posted at 2015-03-18
動作確認
C++ builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/09)

ファイルのドラッグ&ドロップに対応する方法

---
Unit1.hの追加項目

Unit1.h
private:
  void __fastcall ReceiveDropFiles(TWMDropFiles Msg);
  BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, ReceiveDropFiles)
  END_MESSAGE_MAP(TForm)

---
Unit1.cppの追加項目

Unit1.cpp
//---------------------------------------------------------------------------
void __fastcall TForm1::ReceiveDropFiles(TWMDropFiles Msg)
{
    int fileCount;
    wchar_t fileName[MAX_PATH + 10]; // 10: 余分に取る

    Application->BringToFront();

    memset(fileName, 0, sizeof(fileName));

    fileCount=(int)DragQueryFile((HDROP)Msg.Drop,  0xffffffff, NULL, MAX_PATH);

    for(int i=0; i < fileCount; i++){
        DragQueryFile((HDROP)Msg.Drop, i, fileName, MAX_PATH);
    }

    DragFinish((HDROP)Msg.Drop);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    DragAcceptFiles(Handle, True); //ドラッグ&ドロップを有効に
}

v0.2

ヘッダーファイルとcppファイルを全部掲載。
OnCreate()の関連付けは必要になる。

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    void __fastcall FormCreate(TObject *Sender);
private:    // ユーザー宣言
    void __fastcall TForm1::processFile(String filepath);
    void __fastcall ReceiveDropFiles(TWMDropFiles Msg);
    BEGIN_MESSAGE_MAP
    MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, ReceiveDropFiles)
    END_MESSAGE_MAP(TForm)
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
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::ReceiveDropFiles(TWMDropFiles Msg)
{
    int fileCount;
    wchar_t fileName[MAX_PATH + 10]; // 10: 余分に取る

    Application->BringToFront();

    memset(fileName, 0, sizeof(fileName));

    fileCount=(int)DragQueryFile((HDROP)Msg.Drop,  0xffffffff, NULL, MAX_PATH);

    for(int i=0; i < fileCount; i++){
        DragQueryFile((HDROP)Msg.Drop, i, fileName, MAX_PATH);
    }

    DragFinish((HDROP)Msg.Drop);

    processFile(String(fileName));
    int nop=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    DragAcceptFiles(Handle, True); //ドラッグ&ドロップを有効に

}
//---------------------------------------------------------------------------

void __fastcall TForm1::processFile(String filepath)
{
    // ここにファイルの処理を書く
    ShowMessage(filepath + L" is dropped");
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1