原文: http://zetcode.com/gui/wxwidgets/dragdrop/

    Wikipedia 将拖放定义为单击虚拟对象并将其拖动到其他位置或另一个虚拟对象的动作(或支持该动作)。 通常,它可用于调用多种动作,或在两个抽象对象之间创建各种类型的关联。

    拖放操作使我们能够直观地完成复杂的事情。

    在拖放中,我们基本上将一些数据从数据源拖到数据目标。 我们处理以下对象:

    • 数据对象
    • 数据源
    • 数据目标

    对于拖放&文本,wxWidgets 具有预定义的wxTextDropTarget类。

    在下面的示例中,我们将文件名从上方的列表控件拖放到底部的控件中。

    textdrop.h

    1. #include <wx/wx.h>
    2. #include <wx/dnd.h>
    3. class TextDrop : public wxFrame
    4. {
    5. public:
    6. TextDrop(const wxString& title);
    7. void OnSelect(wxCommandEvent& event);
    8. void OnDragInit(wxListEvent& event);
    9. wxGenericDirCtrl *m_gdir;
    10. wxListCtrl *m_lc1;
    11. wxListCtrl *m_lc2;
    12. };
    13. class MyTextDropTarget : public wxTextDropTarget
    14. {
    15. public:
    16. MyTextDropTarget(wxListCtrl *owner);
    17. virtual bool OnDropText(wxCoord x, wxCoord y,
    18. const wxString& data);
    19. wxListCtrl *m_owner;
    20. };

    textdrop.cpp

    1. #include "textdrop.h"
    2. #include <wx/treectrl.h>
    3. #include <wx/dirctrl.h>
    4. #include <wx/dir.h>
    5. #include <wx/splitter.h>
    6. TextDrop::TextDrop(const wxString& title)
    7. : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
    8. {
    9. wxSplitterWindow *spl1 = new wxSplitterWindow(this, -1);
    10. wxSplitterWindow *spl2 = new wxSplitterWindow(spl1, -1);
    11. m_gdir = new wxGenericDirCtrl(spl1, -1, wxT("/home/"),
    12. wxPoint(-1, -1), wxSize(-1, -1), wxDIRCTRL_DIR_ONLY);
    13. m_lc1 = new wxListCtrl(spl2, -1, wxPoint(-1, -1),
    14. wxSize(-1, -1), wxLC_LIST);
    15. m_lc2 = new wxListCtrl(spl2, -1, wxPoint(-1, -1),
    16. wxSize(-1, -1), wxLC_LIST);
    17. MyTextDropTarget *mdt = new MyTextDropTarget(m_lc2);
    18. m_lc2->SetDropTarget(mdt);
    19. Connect(m_lc1->GetId(), wxEVT_COMMAND_LIST_BEGIN_DRAG,
    20. wxListEventHandler(TextDrop::OnDragInit));
    21. wxTreeCtrl *tree = m_gdir->GetTreeCtrl();
    22. spl2->SplitHorizontally(m_lc1, m_lc2);
    23. spl1->SplitVertically(m_gdir, spl2);
    24. Connect(tree->GetId(), wxEVT_COMMAND_TREE_SEL_CHANGED,
    25. wxCommandEventHandler(TextDrop::OnSelect));
    26. Center();
    27. }
    28. MyTextDropTarget::MyTextDropTarget(wxListCtrl *owner)
    29. {
    30. m_owner = owner;
    31. }
    32. bool MyTextDropTarget::OnDropText(wxCoord x, wxCoord y,
    33. const wxString& data)
    34. {
    35. m_owner->InsertItem(0, data);
    36. return true;
    37. }
    38. void TextDrop::OnSelect(wxCommandEvent& event)
    39. {
    40. wxString filename;
    41. wxString path = m_gdir->GetPath();
    42. wxDir dir(path);
    43. bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES);
    44. int i = 0;
    45. m_lc1->ClearAll();
    46. m_lc2->ClearAll();
    47. while ( cont )
    48. {
    49. m_lc1->InsertItem(i, filename);
    50. cont = dir.GetNext(&filename);
    51. i++;
    52. }
    53. }
    54. void TextDrop::OnDragInit(wxListEvent& event)
    55. {
    56. wxString text = m_lc1->GetItemText(event.GetIndex());
    57. wxTextDataObject tdo(text);
    58. wxDropSource tds(tdo, m_lc1);
    59. tds.DoDragDrop(wxDrag_CopyOnly);
    60. }

    main.h

    1. #include <wx/wx.h>
    2. class MyApp : public wxApp
    3. {
    4. public:
    5. virtual bool OnInit();
    6. };

    main.cpp

    1. #include "main.h"
    2. #include "textdrop.h"
    3. IMPLEMENT_APP(MyApp)
    4. bool MyApp::OnInit()
    5. {
    6. TextDrop *td = new TextDrop(wxT("TextDrop"));
    7. td->Show(true);
    8. return true;
    9. }

    在我们的示例中,我们将窗口分为三个部分。 这是通过wxSplitterWindow小部件完成的。 在窗口的左侧,我们有一个通用的目录控件。 我们显示文件系统下所有可用的目录。 右侧有两个窗口。 第一个显示所选目录下的所有文件。 第二个用于拖动文件。

    1. MyTextDropTarget *mdt = new MyTextDropTarget(m_lc2);
    2. m_lc2->SetDropTarget(mdt);

    在这里,我们定义了文本放置目标。

    1. wxString text = m_lc1->GetItemText(event.GetIndex());
    2. wxTextDataObject tdo(text);
    3. wxDropSource tds(tdo, m_lc1);
    4. tds.DoDragDrop(wxDrag_CopyOnly);

    OnDragInit()方法中,我们定义了一个文本数据对象和一个放置源对象。 我们称为DoDragDrop()方法。 wxDrag_CopyOnly常数仅允许复制数据。

    1. bool MyTextDropTarget::OnDropText(wxCoord x, wxCoord y,
    2. const wxString& data)
    3. {
    4. m_owner->InsertItem(0, data);
    5. return true;
    6. }

    在放置操作期间,我们将文本数据插入到列表控件中。

    wxWidgets 中的拖放 - 图1

    图:拖放

    在本章中,我们介绍了 wxWidgets 中的拖放操作。