• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    讓ListBox控件支持拖動

    發布: 2007-7-14 21:11 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 19次 | 進入軟件測試論壇討論

    領測軟件測試網 通常我們通過ListBox控件來顯示我們的信息列表,然后我們可以通過鼠標來選擇我們的條目信息,但VC中的ListBox控件是不支持拖動的。也許我們有時需要改變我們的列表順序,已適應我們的要求,下面是實現的方法。

      設計思路:


      1. 如果通過鼠標左鍵選中某一條目并拖動,此時我們通過變量記錄當前選中條目的位置和條目字符串以及此條目的副值。

      2. 鼠標移動到要移動到的位置后放開左鍵,此時我們把以前選中的條目插入到此處,同時,刪掉原位置的條目。

      實現步驟:

      1. 定義一個從ClistBox類擴展的類CMyListBox,代碼下面分析。

      2. 通過新類定義我們的列表控件變量。

      代碼分析:


    // MyListBox.h : header file
    //
    // CMyListBox window

    class CMyListBox : public CListBox
    {
     // Construction
     public:
     CMyListBox();

     // Attributes
     private:
      BOOL m_LButtonDownFlag;
      BOOL m_MouseMoveFlag;
      int m_OldPosition;
      int m_NewPosition;
      CString m_DragString;
      DWORD m_ItemData;
     public:

      // Operations
     public:

      // Overrides
      // ClassWizard generated virtual function overrides
      file://{{AFX_VIRTUAL(CMyListBox)
    file://}}AFX_VIRTUAL

     // Implementation
     public:
      virtual ~CMyListBox();

      // Generated message map functions
     protected:
      file://{{AFX_MSG(CMyListBox)
      afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
      afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
      afx_msg void OnMouseMove(UINT nFlags, CPoint point);
      // NOTE - the ClassWizard will add and remove member functions here.
    file://}}AFX_MSG

     DECLARE_MESSAGE_MAP()
    };

    file://{{AFX_INSERT_LOCATION}}

     #endif //  !defined(AFX_MYLISTBOX_H__CF3EDAA5_BBD7_43CD_80CB_A86B65D9A607__INCLUDED_)


     // MyListBox.cpp : implementation file
     file://

     #include "stdafx.h"
     #include "sditest.h"
     #include "MyListBox.h"
     
     #ifdef _DEBUG
     #define new DEBUG_NEW
     #undef THIS_FILE
     static char THIS_FILE[] = __FILE__;
     #endif

     // CMyListBox

     CMyListBox::CMyListBox()
      {
       m_LButtonDownFlag = FALSE;
       m_MouseMoveFlag = FALSE;
      }

     CMyListBox::~CMyListBox()
      {
     }


     BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
     file://{{AFX_MSG_MAP(CMyListBox)
     ON_WM_LBUTTONDOWN()
     ON_WM_LBUTTONUP()
     ON_WM_MOUSEMOVE()
     // NOTE - the ClassWizard will add and remove mapping macros here.
     file://}}AFX_MSG_MAP
    END_MESSAGE_MAP()

     // CMyListBox message handlers
     void CMyListBox::OnLButtonDown(UINT nFlags, CPoint point)
      {
       CListBox::OnLButtonDown(nFlags, point);
       file://如果選中一個條目,此時進行處理,否則會出錯。
      if(GetCurSel() != -1)
       m_LButtonDownFlag = TRUE;
      }

     void CMyListBox::OnLButtonUp(UINT nFlags, CPoint point)
      {
       CListBox::OnLButtonUp(nFlags, point);
       m_LButtonDownFlag = FALSE;
       if(m_MouseMoveFlag)
       {
        m_MouseMoveFlag = FALSE;
        POINT pt;
        ::GetCursorPos(&pt);
        CRect iRect;
        this->GetWindowRect(iRect);
        if(iRect.PtInRect(pt))//確定鼠標移動到了合適的位置
        {
         m_NewPosition = GetCurSel();
         if(m_NewPosition < m_OldPosition)
         {
          InsertString(m_NewPosition,m_DragString);
          DeleteString(m_OldPosition+1);
          this->SetCurSel(m_NewPosition);
          file://設置移動條目的副值,如果刪除或者添加一條記錄,副值會隨字符串一起移動
          SetItemData(m_NewPosition,m_ItemData);
          TRACE("%d%d%d%d%d%d%d%d",GetItemData(0),GetItemData(1),_
    GetItemData(2),GetItemData(3),GetItemData(4),_
    GetItemData(5),GetItemData(6),GetItemData(7));
    }
         else
          {
           InsertString(m_NewPosition+1,m_DragString);
           DeleteString(m_OldPosition);
           this->SetCurSel(m_NewPosition);
           SetItemData(m_NewPosition,m_ItemData);
           TRACE("%d%d%d%d%d%d%d%d",GetItemData(0),GetItemData(1),_
    GetItemData(2),GetItemData(3),GetItemData(4),_
    GetItemData(5),GetItemData(6),GetItemData(7));
    }
         }
       }

      }

      void CMyListBox::OnMouseMove(UINT nFlags, CPoint point)
       {
        CListBox::OnMouseMove(nFlags, point);
        if(m_LButtonDownFlag)
        {
         m_MouseMoveFlag = TRUE;
         m_OldPosition = GetCurSel();
         GetText(m_OldPosition,m_DragString);
         try{
          m_ItemData = GetItemData(m_OldPosition);
         }
        catch(...)
        {
         AfxMessageBox("Wrong!");
        }

        m_LButtonDownFlag = FALSE;
        SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
       }
      }


      實現了上面的代碼后,我們就可以在列表框中隨便改變我們的條目的順序了,趕快試一下吧!在例程中彈出關于對話框,在列表中就可以改變順序了。

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>