我说的是用API!!!!!不要告诉我List.List(n) = "aaa"也不要说先List1.RemoveItem,再List1.AddItem呵呵,俺说的是用Sendmessage或CopyMemory设置ListBox指定位置的值.我找了一天,只有LB_GETTEXT和LB_GETTEXTLEN,可是没有像LB_SETTEXT这样的常数..现在我已经可以获取当前位置条目(选中条目)的Index要替换ListBox中的第N个条目中的内容....!!!!用Sendmessage或CopyMemory!!!!那位大侠做过?

解决方案 »

  1.   

    确实没有这个消息,不过你可以先发送LB_INSERTSTRING再LB_DELETESTRING达到目的,具体的你把下面这段代码翻译成vb的就可以了,应该没什么问题吧:)lTotal = SendMessage (hControl,LB_GETCOUNT,0,0L);
      if ((lIndex >= 0) && (lIndex < lTotal))
      {
        /* Find what the current selection is */
        lCurSel = SendMessage (hControl, LB_GETCURSEL, 
                              (WPARAM)0,0L);
        /* Insert new string at List(i) */
        SendMessage (hControl, LB_INSERTSTRING, 
                    (WPARAM)(lIndex), (LPARAM)szItem);
        /* Delete string at List(i+1) */
        SendMessage (hControl, LB_DELETESTRING, 
                    (WPARAM)(lIndex+1), 0L);
        /* Restore the current selection */
        SendMessage (hControl, LB_SETCURSEL, 
                    (WPARAM)lCurSel, 0L);
      }
      

  2.   

    rainstormmaster(暴风雨 v2.0) 老大
    '---------------------
    不要说先List1.RemoveItem,再List1.AddItem
    '---------------------
    lTotal = SendMessage (hControl,LB_GETCOUNT,0,0L);
      if ((lIndex >= 0) && (lIndex < lTotal))
      {
        /* Find what the current selection is */
        lCurSel = SendMessage (hControl, LB_GETCURSEL, 
                              (WPARAM)0,0L);
        /* Insert new string at List(i) */
        SendMessage (hControl, LB_INSERTSTRING, 
                    (WPARAM)(lIndex), (LPARAM)szItem);
        /* Delete string at List(i+1) */
        SendMessage (hControl, LB_DELETESTRING, 
                    (WPARAM)(lIndex+1), 0L);
        /* Restore the current selection */
        SendMessage (hControl, LB_SETCURSEL, 
                    (WPARAM)lCurSel, 0L);
      }
    '-------------------------------
    两者有区别吗?要是这样,我就不会提这个问题了!我想知道可不可以找到ListBox保存字符串的地址,用Copymemory写进去...这样应该会比较快...如果先 移除,再添加,太慢了,也太麻烦....
    rainstormmaster(暴风雨 v2.0) 
    你做过吗?  如何获得ListBox那个数组的地址?应该从那里下手?VB是怎么做到的?可以追踪到VB赋值的过程吗?
    '===================================================
    自已刨坑,自已埋.......      :|
      

  3.   

    嘻嘻,這代碼偶有,而且保証是你想要的,但是!!!!偏偏就不給你,至少看了三個版你問問題,
    我kao…最近火氣大,不氣不氣,反正就不答你就成了。我可不說假話
      

  4.   

    RE:
    '-----------------------------------------------
    主  题:  送分题!修改ListBox中的第n个位置的值! 
    作  者:  boyzhang (张郎)  
    '-----------------------------------------------
    嘻嘻,這代碼偶有,而且保証是你想要的,但是!!!!偏偏就不給你'##############################################
    1.rainstormmaster(暴风雨 v2.0)版主都搞不定,你?
    我不相信你能写出来!(上网找的不算)
    2.我学API也有年头了,提的问题均是自己先用很多种方
    法搞不定(包括上网找)才在CSDN上提的..
    3.我相信我在网上找东西还是有一套的,
    家里4M ADSL单位10M光纤学校上网不要钱
    我上网除了搜索资料也没有别的嗜好了..
    我也知道你说的是什么,我见过一个可以拖的
    ListBox控件(API处理),不过,我想知道的是我说的
    用CopyMemory可不可以实现(效率会比较高)....
    我的目的是做一个和Winamp列表一样的方便而强大的
    列表钩子,自动处理ListBox的消息(自画,和ZYL910做的可以换肤的按钮一样)
    现在已经可以实现单行/多行拖动了,不过是用的VB的
    List.List(X)=  这样的方法,觉得不太妥,也看了书
    搜了Google,MSDN,却没找到能我想要的那条消息...
    4.书也看了好几十本,不过API的都不太全,刚刚买了
    本O'REILLY的VB Win32API,搞定了
    http://expert.csdn.net/Expert/topic/2739/2739860.xml?temp=.7045404
    关于在VB中如何DLL注入 的问题
    有兴趣可以看看..我有那本书的光盘
    5.我没有别的意思,只是真搞不懂了才在CSDN上发贴,
    真想把一个问题搞懂,如此而已...
      

  5.   

    哈哈,自己找到了(好像只能这样了,也许这就是最好的方式)...http://www.yesky.com/20010718/189375.shtml// MyListBox.h : header file
    // 
    // CMyListBox windowclass 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));
       }
      }
      

  6.   

    好像也是  暴风雨的方法...m_ItemData = GetItemData(m_OldPosition);不过,这一句好像可以用子类处理来实现...算了,结帐啦...