<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # wxWidgets 中的拖放 > 原文: [http://zetcode.com/gui/wxwidgets/dragdrop/](http://zetcode.com/gui/wxwidgets/dragdrop/) Wikipedia 將拖放定義為單擊虛擬對象并將其拖動到其他位置或另一個虛擬對象的動作(或支持該動作)。 通常,它可用于調用多種動作,或在兩個抽象對象之間創建各種類型的關聯。 拖放操作使我們能夠直觀地完成復雜的事情。 在拖放中,我們基本上將一些數據從數據源拖到數據目標。 我們處理以下對象: * 數據對象 * 數據源 * 數據目標 對于拖放&文本,wxWidgets 具有預定義的`wxTextDropTarget`類。 在下面的示例中,我們將文件名從上方的列表控件拖放到底部的控件中。 `textdrop.h` ```cpp #include <wx/wx.h> #include <wx/dnd.h> class TextDrop : public wxFrame { public: TextDrop(const wxString& title); void OnSelect(wxCommandEvent& event); void OnDragInit(wxListEvent& event); wxGenericDirCtrl *m_gdir; wxListCtrl *m_lc1; wxListCtrl *m_lc2; }; class MyTextDropTarget : public wxTextDropTarget { public: MyTextDropTarget(wxListCtrl *owner); virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& data); wxListCtrl *m_owner; }; ``` `textdrop.cpp` ```cpp #include "textdrop.h" #include <wx/treectrl.h> #include <wx/dirctrl.h> #include <wx/dir.h> #include <wx/splitter.h> TextDrop::TextDrop(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200)) { wxSplitterWindow *spl1 = new wxSplitterWindow(this, -1); wxSplitterWindow *spl2 = new wxSplitterWindow(spl1, -1); m_gdir = new wxGenericDirCtrl(spl1, -1, wxT("/home/"), wxPoint(-1, -1), wxSize(-1, -1), wxDIRCTRL_DIR_ONLY); m_lc1 = new wxListCtrl(spl2, -1, wxPoint(-1, -1), wxSize(-1, -1), wxLC_LIST); m_lc2 = new wxListCtrl(spl2, -1, wxPoint(-1, -1), wxSize(-1, -1), wxLC_LIST); MyTextDropTarget *mdt = new MyTextDropTarget(m_lc2); m_lc2->SetDropTarget(mdt); Connect(m_lc1->GetId(), wxEVT_COMMAND_LIST_BEGIN_DRAG, wxListEventHandler(TextDrop::OnDragInit)); wxTreeCtrl *tree = m_gdir->GetTreeCtrl(); spl2->SplitHorizontally(m_lc1, m_lc2); spl1->SplitVertically(m_gdir, spl2); Connect(tree->GetId(), wxEVT_COMMAND_TREE_SEL_CHANGED, wxCommandEventHandler(TextDrop::OnSelect)); Center(); } MyTextDropTarget::MyTextDropTarget(wxListCtrl *owner) { m_owner = owner; } bool MyTextDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) { m_owner->InsertItem(0, data); return true; } void TextDrop::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 TextDrop::OnDragInit(wxListEvent& event) { wxString text = m_lc1->GetItemText(event.GetIndex()); wxTextDataObject tdo(text); wxDropSource tds(tdo, m_lc1); tds.DoDragDrop(wxDrag_CopyOnly); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "textdrop.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { TextDrop *td = new TextDrop(wxT("TextDrop")); td->Show(true); return true; } ``` 在我們的示例中,我們將窗口分為三個部分。 這是通過`wxSplitterWindow`小部件完成的。 在窗口的左側,我們有一個通用的目錄控件。 我們顯示文件系統下所有可用的目錄。 右側有兩個窗口。 第一個顯示所選目錄下的所有文件。 第二個用于拖動文件。 ```cpp MyTextDropTarget *mdt = new MyTextDropTarget(m_lc2); m_lc2->SetDropTarget(mdt); ``` 在這里,我們定義了文本放置目標。 ```cpp wxString text = m_lc1->GetItemText(event.GetIndex()); wxTextDataObject tdo(text); wxDropSource tds(tdo, m_lc1); tds.DoDragDrop(wxDrag_CopyOnly); ``` 在`OnDragInit()`方法中,我們定義了一個文本數據對象和一個放置源對象。 我們稱為`DoDragDrop()`方法。 `wxDrag_CopyOnly`常數僅允許復制數據。 ```cpp bool MyTextDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) { m_owner->InsertItem(0, data); return true; } ``` 在放置操作期間,我們將文本數據插入到列表控件中。 ![Drag & Drop](https://img.kancloud.cn/59/03/59035e2e02d96f51a05d6d2f4e4c446b_516x359.jpg) 圖:拖放 在本章中,我們介紹了 wxWidgets 中的拖放操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看