C++ Builder支援檔案拖放

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.