控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

实现列表控件的子条目编辑

作者:未知 出处:不详 2010年01月26日 阅读:

这篇文章提供了一个子类化列表控件中编辑控件的方法。与其他使用LVM_GETEDITCONTROL进行子类化不同,我使用一种更简捷的方法。步骤如下:

变量
public:
  CSubEdit m_editWnd;
  int m_item;
  int m_subitem;

CSubeditListView::CSubeditListView()
{
  m_subitem = 0;
}
监视鼠标点击动作
void CSubeditListView::OnLButtonDown(UINT nFlags, CPoint point)
{
  LVHITTESTINFO  lvhit;
  lvhit.pt = point;
  int item = GetListCtrl().SubItemHitTest(&lvhit);

  //if (over a subitem)
  if (item != -1 && lvhit.iSubItem && (lvhit.flags &
                    LVHT_ONITEM ))
    {
    //mouse click outside the editbox in an already
    //editing cell cancels editing
    if (m_subitem == lvhit.iSubItem && item == m_item)
      {
      CListView::OnLButtonDown(nFlags, point);
      }
    else
      {
      CListView::OnLButtonDown(nFlags, point);

      m_subitem = lvhit.iSubItem;
      m_item = item;
      GetListCtrl().EditLabel(item);
      }

    }
  else
    CListView::OnLButtonDown(nFlags, point);
}
调用OnBeginLabelEdit函数
void CSubeditListView::OnBeginLabelEdit(NMHDR* pNMHDR,
    LRESULT* pResult)
{
  LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;

  //if (subitem selected for editing)
  if (m_subitem)
    {
    ASSERT(m_item == pDispInfo->item.iItem);

    CRect  subrect;
    GetListCtrl().GetSubItemRect( pDispInfo->item.iItem,
                                  m_subitem,
        LVIR_BOUNDS , subrect );

    //get edit control and subclass
    HWND hWnd=(HWND)SendMessage(LVM_GETEDITCONTROL);
    ASSERT(hWnd!=NULL);
    VERIFY(m_editWnd.SubclassWindow(hWnd));

    //move edit control text 1 pixel to the right of org label,
    //as Windows does it...
    m_editWnd.m_x=subrect.left + 6;
    m_editWnd.SetWindowText(GetListCtrl().GetItemText
        (pDispInfo->item.iItem,m_subitem));

    //hide subitem text so it don't show if we delete some
    //text in the edit control
    //OnPaint handles other issues also regarding this
    CRect  rect;
    GetListCtrl().GetSubItemRect(pDispInfo->item.iItem,m_subitem,
        LVIR_LABEL ,rect);
    CDC* hDc = GetDC();
    hDc->FillRect(rect,&CBrush(::GetSysColor(COLOR_WINDOW)));
    ReleaseDC(hDc);

    }

  //return: editing permitted
  *pResult = 0;

}
定位编辑控件
void CSubEdit::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
  lpwndpos->x=m_x;

  CEdit::OnWindowPosChanging(lpwndpos);
}
调用paint函数
void CSubeditListView::OnPaint()
{
  //if (subitem editing)
  if (m_subitem)
    {

    CRect  rect;
    CRect  editrect;

    GetListCtrl().GetSubItemRect(m_item,m_subitem,LVIR_LABEL,
                                 rect);

    m_editWnd.GetWindowRect(editrect);
    ScreenToClient(editrect);

    //block text redraw of the subitems text (underneath the
    //editcontrol)
    //if we didn't do this and deleted some text in the edit
    //control, the subitems original label would show
    if (editrect.right < rect.right)
      {
      rect.left = editrect.right ;
      ValidateRect(rect);
      }

    //block filling redraw of leftmost item (caused by FillRect)
    GetListCtrl().GetItemRect(m_item,rect,LVIR_LABEL );
    ValidateRect(rect);
    }

  CListView::OnPaint();
}
调用OnSize函数,使列表控件大小改变时不能进行编辑工作
void CSubeditListView::OnSize(UINT nType, int cx, int cy)
{
  //stop editing if resizing
  if( GetFocus() != this ) SetFocus();

  CListView::OnSize(nType, cx, cy);

}
调用OnEndLabelEdit函数
void CSubeditListView::OnEndLabelEdit(NMHDR* pNMHDR,
                                      LRESULT* pResult)
{
  LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
  LV_ITEM  *plvItem = &plvDispInfo->item;

  //if (end of sub-editing) do cleanup
  if (m_subitem)
    {
    //plvItem->pszText is NULL if editing canceled
    if (plvItem->pszText != NULL )
      GetListCtrl().SetItemText(plvItem->iItem,m_subitem,
          plvItem->pszText);

    VERIFY(m_editWnd.UnsubclassWindow()!=NULL);

    m_subitem = 0;
    //always revert to org label (Windows thinks we are editing
    //the leftmost item)
    *pResult = 0;
    }
  else
    //return: update label on leftmost item
    *pResult = 1;

}

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统