#ifndef WX_PRECOMP
#define WX_PRECOMP
#include <wx/wx.h>
#endif // WX_PRECOMP
#include <wx/dnd.h>
#include <wx/dir.h>
#include <wx/dirctrl.h>
#include <wx/splitter.h>
#include <wx/listctrl.h>
#include "mytextdroptarget.h"
class Simple:public wxFrame
{
public:
Simple(const wxString& title);
protected:
void OnQuit(wxCloseEvent& event);
void OnSelect(wxCommandEvent& event);
void OnDragInit(wxListEvent& event);
wxSplitterWindow* spl1;
wxSplitterWindow* spl2;
wxGenericDirCtrl* m_gdir;
wxListCtrl* m_lc1;
wxListCtrl* m_lc2;
};
class MyTextDropTarget : public wxTextDropTarget
{
public:
MyTextDropTarget(wxListCtrl* owner);//把目标控件传递进来
//拖动源放到拖放目的地时会执行该函数 此函数重写了基类的OnDropText
virtual bool OnDropText(wxCoord x, wxCoord y,const wxString& data);
wxListCtrl* m_owner;
};
MyTextDropTarget::MyTextDropTarget(wxListCtrl* owner)
{
m_owner = owner;
}
bool MyTextDropTarget::OnDropText(wxCoord x,wxCoord y,const wxString& data)
{
m_owner->InsertItem(0,data);
return true;
}
Simple::Simple(const wxString& title)
:wxFrame(NULL,-1,title)
{
spl1 = new wxSplitterWindow(this,-1);
spl2 = new wxSplitterWindow(spl1,-1);
m_gdir = new wxGenericDirCtrl(spl1,-1,wxT("."),wxPoint(-1,-1)
,wxSize(this->GetClientSize().GetWidth()*0.4,-1)
,wxDIRCTRL_DIR_ONLY
);
wxTreeCtrl* tree= m_gdir->GetTreeCtrl();
tree->Bind(wxEVT_COMMAND_TREE_SEL_CHANGED,Simple::OnSelect,this);
m_lc1 = new wxListCtrl(spl2,-1,wxPoint(-1,-1)
,wxSize(-1,this->GetClientSize().GetHeight()/2)
,wxLC_LIST
);
m_lc1->Bind(wxEVT_COMMAND_LIST_BEGIN_DRAG,wxListEventHandler(Simple::OnDragInit),this);
m_lc2 = new wxListCtrl(spl2,-1,wxPoint(-1,-1),wxSize(-1,-1),wxLC_LIST);
m_lc2->SetDropTarget(new MyTextDropTarget(m_lc2));//绑定拖拽目标对象
spl2->SplitHorizontally(m_lc1,m_lc2);
spl1->SplitVertically(m_gdir,spl2);
Bind(wxEVT_CLOSE_WINDOW,Simple::OnQuit,this);
Centre();
}
void Simple::OnQuit(wxCloseEvent& event)
{
Close(true);
}
void Simple::OnSelect(wxCommandEvent& event)
{
wxString filename;
wxString path = m_gdir->GetPath();
wxDir dir(path);
bool cont = dir.GetFirst(&filename,wxEmptyString,wxDIR_FILES);
int i =0;
m_lc1->ClearAll();
m_lc2->ClearAll();
while(cont)
{
m_lc1->InsertItem(i,filename);
cont = dir.GetNext(&filename);
i++;
}
}
void Simple::OnDragInit(wxListEvent& event)
{
wxString text = m_lc1->GetItemText(event.GetIndex());
//
wxTextDataObject tdo(text);//1.创建数据源,并使用您要拖动的数据进行初始化
wxDropSource tds(tdo,m_lc1);//2.传入源控件
tds.DoDragDrop(wxDrag_CopyOnly);//3.开始拖拽
}