40、wxWidgets拖放 Drag and drop

#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.开始拖拽
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值