C++ Builder支持文件拖放

来源:互联网 发布:网络销售是什么职业 编辑:程序博客网 时间:2024/06/09 13:42

首先在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);

原创粉丝点击