首先在FormCreate時調用DragAcceptFiles(this,true)註冊你的程式,使得你的程式可以接受檔案的DragDrop。然後處理WM_DROPFILES訊息,獲得DropDrag的訊息,調用如下函數獲得相關的參數::
UINT DragQueryFile(
HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
);
BOOL DragQueryPoint(
HDROP hDrop,
LPPOINT lppt
);
最後用下面的函數接受DragDrop的動作。
VOID DragFinish(
HDROP hDrop
);
三.
在標頭檔裡加上:
private: // User declarations
void __fastcall AcceptFiles(TMessage& Msg);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES,TMessage,AcceptFiles)
END_MESSAGE_MAP(TForm)
一段代碼,希望對你有些協助:
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DragAcceptFiles(this->Handle,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AcceptFiles(TMessage& Msg)
{
const int m_nMaxFileNameLen=255;
int i,m_nCount;
char m_sFileName[m_nMaxFileNameLen];
m_nCount=DragQueryFile((HANDLE)Msg.WParam,0xffffffff,m_sFileName,m_nMaxFileNameLen);
for(i=0;i<m_nCount;i++)
{
DragQueryFile((HANDLE)Msg.WParam,i,m_sFileName,m_nMaxFileNameLen);
MessageBox(this->Handle,m_sFileName,"Drop File",MB_OK);
}
DragFinish((HANDLE)Msg.WParam);
}
四.
在需要拖放的表單類中加入訊息處理映射,如下:
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES,TWMDropFiles,WMDropFiles)
END_MESSAGE_MAP (TForm);
在相應的cpp檔案進入訊息處理函數,如下:
void __fastcall TForm1::WMDropFiles(TWMDropFiles &message)
{
UINT filecount = DragQueryFile((HDROP) message.Drop, 0xFFFFFFFF, NULL, 0); //查詢拖放的檔案數量
for(int n=0;n<=filecount-1;n++)
{
String filename;
filename.SetLength(MAX_PATH);
int length = DragQueryFile ((HDROP) message.Drop,n,filename.c_str (), MAX_PATH);
filename.SetLength (length);
//在這兒替換你的處理代碼
}
DragFinish ((HDROP) message.Drop);
}
在表單類的OnCreate事件中加入如下代碼,
DragAcceptFiles(Handle,true);