CListCtrl控件中InsertItem和SettItemtext函数的用法简介

本人初次用CListCtrl控件的时候,对于 InsertItem和SetrtItemtext两个函数的作用始终不是太懂,比如如果不先调用InsertItem这个函数,后面的InsertItemtext函数写了,也不起作用.查了MSDN,也没有收获,经过有人点拔,才恍然大悟.今天将经验说一下,希望有共同爱好者少走弯路.

         InsertItem在MSDN中的函数原型如下
        int InsertItem( const LVITEM* pItem 
);
       int InsertItem( int nItem, LPCTSTR lpszItem
 );
       int InsertItem( int nItem, LPCTSTR lpszItem, int nImage
 );
      
int InsertItem( UINT nMask, int nItem, LPCTSTR lpszItem, UINTnState, UINT nStateMask, int nImage, LPARAM lParam
 );
     一般我们用的比较多的时第2种和第三种,其中第三种时建立一个带有头标的项

     要调用第三个,可以先声明一个CImageList对象
m_ImageList.Create(16,16,true,2,2);
m_ImageList.Add(theApp.LoadIcon(IDR_MAINFRAME));
m_ListCtrl.SetImageList(&m_ImageList,LVSIL_SMALL);
这样,控件中的每一行的开头就有一个应用程序图标
     

      现在转入正题,先介绍一下INSERTITEM个个参数的意义
     nItem:    控件中行的索引
     lpszItem : 控件头的名字
     .........................................................................................................................................................................................................
     SettItemtext函数的原型
    BOOL SetItemText( int nItem, int nSubItem, LPTSTR lpszText 
);
     ******************注意这个函数中的参数
nItem就是INSERTITEM中的NITEM,
    也就是说InsertItem是向控件声请一行来放数据,记住只是声请,那么方数据的工作就又SETITEMTEXT来实现
    举例如下:

      CListCtrl m_listctrl;

     m_listctrl = new ClistCtrl();

     m_listctrl.Creae(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS
   ,bigRect,this,IDC_LIST1
     );
     
     m_listctrl.InsertItem(0,"01");

     m_SetItemtext(0,1,"1");

     m_SetItemtext(0,2,"2");
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     至于要添多少多少个数据,就由你的控件由多少列了

     获得可以这样的道 int columns= m_listctr.GetHeaderCtrl()->GetItemCount();

     获得可以这样的道 int   rows= m_listctrl.GetItemCount();
     
       下面附一个本人学习的实例
  // DataView.cpp : implementation of the CDataView class
//

#include "stdafx.h"
#include "Data.h"

#include "DataDoc.h"
#include "DataView.h"
#include "EditDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CDataView

IMPLEMENT_DYNCREATE(CDataView, CView)

BEGIN_MESSAGE_MAP(CDataView, CView)
//{{AFX_MSG_MAP(CDataView)
ON_COMMAND(IDM_USER, OnUser)
ON_COMMAND(IDM_UP, OnUp)
ON_COMMAND(IDM_DOWN, OnDown)
ON_COMMAND(IDM_EDIT, OnEdit)
ON_COMMAND(IDM_ADD, OnAdd)
ON_COMMAND(IDM_DELETE, OnDelete)
ON_COMMAND(IDM_SAVE, OnSave)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
ON_NOTIFY(NM_KILLFOCUS, IDC_LIST1, OnKillfocusList1)
END_MESSAGE_MAP()

/
// CDataView construction/destruction

CDataView::CDataView()
{
// TODO: add construction code here
    m_nIndex = -1;
}

CDataView::~CDataView()
{
}

BOOL CDataView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

/
// CDataView drawing

void CDataView::OnDraw(CDC* pDC)
{
CDataDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}

/
// CDataView printing

BOOL CDataView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CDataView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CDataView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/
// CDataView diagnostics

#ifdef _DEBUG
void CDataView::AssertValid() const
{
CView::AssertValid();
}

void CDataView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CDataDoc* CDataView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDataDoc)));
return (CDataDoc*)m_pDocument;
}
#endif //_DEBUG

/
// CDataView message handlers

void CDataView::OnInitialUpdate() 
{
CView::OnInitialUpdate();

// TODO: Add your specialized code here and/or call the base class
    CDC *dc = GetDC();
CRect rect;
this->GetClientRect(&rect);
    CString str = "加载数据库资源到列表中";
CSize sz = dc->GetTextExtent(str);
rect.right = sz.cx;
rect.bottom = sz.cy;
CStatic *state = new CStatic;
state->Create(str,WS_CHILD,rect,this);
    state->ShowWindow(SW_SHOW);
    this->ReleaseDC(dc);

InitListCtrl(rect);
m_ImageList.Create(16,16,true,2,2);
m_ImageList.Add(theApp.LoadIcon(IDR_MAINFRAME));

}


void CDataView::InitListCtrl(CRect rect)
{
CRect bigRect;
this->GetClientRect(&bigRect);
bigRect.top = rect.bottom;
m_ListCtrl.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS
   ,bigRect,this,IDC_LIST1);
//m_ListCtrl.Insert(0,"用户名");
//m_ListCtrl.InsertItem(1,"密码");
CFont *m_pFont = new CFont();
m_pFont->CreateFont(20,                        // nHeight
   0,                         // nWidth
   0,                         // nEscapement
   0,                         // nOrientation
   FW_NORMAL,                 // nWeight
   FALSE,                     // bItalic
   FALSE,                     // bUnderline
   0,                         // cStrikeOut
   DEFAULT_CHARSET,              // nCharSet
   OUT_DEFAULT_PRECIS,        // nOutPrecision
   CLIP_DEFAULT_PRECIS,       // nClipPrecision
   DEFAULT_QUALITY,           // nQuality
   DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
   "Arial");   
    m_ListCtrl.SetFont(m_pFont);
    m_ListCtrl.SetImageList(&m_ImageList,LVSIL_SMALL);
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_ListCtrl.SetBkColor(RGB(247,247,255));
    m_ListCtrl.SetTextColor(RGB(0,0,255));
    m_ListCtrl.SetTextBkColor(RGB(247,247,255));
m_ListCtrl.InsertColumn(0,"ID",LVCFMT_LEFT,100,-1);
m_ListCtrl.InsertColumn(1,"用户名",LVCFMT_LEFT,100,-1);
m_ListCtrl.InsertColumn(2,"密码",LVCFMT_LEFT,200,-1);

m_ListCtrl.InsertItem(0,"01");
m_ListCtrl.SetItemText(0,1,"chetnao");
m_ListCtrl.SetItemText(0,2,"921984");

    m_ListCtrl.InsertItem(1,"02");
m_ListCtrl.SetItemText(1,1,"mada");
m_ListCtrl.SetItemText(1,2,"345678");

m_ListCtrl.InsertItem(2,"03");
m_ListCtrl.SetItemText(2,1,"luoxian");
m_ListCtrl.SetItemText(2,2,"987654");
}

extern CDataApp theApp;
void CDataView::OnUser() 
{
// TODO: Add your command handler code here
    _RecordsetPtr m_pRecordset;
try
{
   m_pRecordset = theApp.m_pConnection->Execute("select * from user12",NULL,adCmdText);
}
catch(_com_error e)
{
   AfxMessageBox(e.ErrorMessage());
   AfxMessageBox(e.Description());
   CString str;
   str.Format("%ld",e.Error);
   AfxMessageBox(str);
   return;
}
if(m_pRecordset->BOF)
{
   MessageBox("表中无记录");
   return;
}
else
{
   int len = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
   while(!m_pRecordset->adoEOF)
   {

            m_ListCtrl.InsertItem(2,(_bstr_t)m_pRecordset->GetCollect("ID"));
      m_ListCtrl.InsertItem(1,(_bstr_t)m_pRecordset->GetCollect("username"));
         m_ListCtrl.InsertItem(0,(_bstr_t)m_pRecordset->GetCollect("password"));
    /*for(int i=0;i<len;i++)
    {
     m_ListCtrl.SetItemText(i,0,(_bstr_t)m_pRecordset->GetCollect("ID"));
     m_ListCtrl.SetItemText(i,1,(_bstr_t)m_pRecordset->GetCollect("username"));
     m_ListCtrl.SetItemText(i,2,(_bstr_t)m_pRecordset->GetCollect("password"));
    }*/

    m_pRecordset->MoveNext();
   }
}
}
void CDataView::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
POSITION pos = m_ListCtrl.GetFirstSelectedItemPosition();
    m_nIndex = m_ListCtrl.GetNextSelectedItem(pos);

}
void CDataView::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    POSITION pos = m_ListCtrl.GetFirstSelectedItemPosition();
    m_nIndex = m_ListCtrl.GetNextSelectedItem(pos);
    CEditDlg dlg;
dlg.m_strNo = m_ListCtrl.GetItemText(m_nIndex,0);
dlg.m_strName = m_ListCtrl.GetItemText(m_nIndex,1);
dlg.m_strPassword = m_ListCtrl.GetItemText(m_nIndex,2);

    if(IDOK == dlg.DoModal())
{
   m_ListCtrl.SetItemText(m_nIndex,0,dlg.m_strNo);
        m_ListCtrl.SetItemText(m_nIndex,1,dlg.m_strName);
   m_ListCtrl.SetItemText(m_nIndex,2,dlg.m_strPassword);
}
*pResult = 0;

}

void CDataView::OnUp() 
{
// TODO: Add your command handler code here
    m_ListCtrl.SetFocus();
if(m_nIndex == -1)
{
   MessageBox("请选择一行!");
   return ;
}
if(m_nIndex == 0)
{
   MessageBox("你选中的行已经时顶行!");
   return ;
}
CString strNo,strName,strPassword;
strNo = m_ListCtrl.GetItemText(m_nIndex,0);
strName = m_ListCtrl.GetItemText(m_nIndex,1);
strPassword = m_ListCtrl.GetItemText(m_nIndex,2);

m_ListCtrl.DeleteItem(m_nIndex);
m_ListCtrl.InsertItem(m_nIndex-1,strNo);
m_ListCtrl.SetItemText(m_nIndex-1,1,strName);
    m_ListCtrl.SetItemText(m_nIndex-1,2,strPassword);
    m_nIndex--;
m_ListCtrl.SetItemState(m_nIndex,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);


}
void CDataView::OnKillfocusList1(NMHDR* pNMHDR, LRESULT* pResult)
{
m_nIndex = -1;
}
void CDataView::OnDown() 
{
// TODO: Add your command handler code here
m_ListCtrl.SetFocus();
    if(m_nIndex == -1)
{
   MessageBox("请选择一行!");
   return ;
}
if(m_nIndex == m_ListCtrl.GetItemCount()-1)
{
   MessageBox("已经时最后一行!");
   return ;
}
CString strNo,strName,strPassword;
strNo = m_ListCtrl.GetItemText(m_nIndex,0);
strName = m_ListCtrl.GetItemText(m_nIndex,1);
strPassword = m_ListCtrl.GetItemText(m_nIndex,2);

m_ListCtrl.DeleteItem(m_nIndex);
m_ListCtrl.InsertItem(m_nIndex+1,strNo);
m_ListCtrl.SetItemText(m_nIndex+1,1,strName);
    m_ListCtrl.SetItemText(m_nIndex+1,2,strPassword);
    m_nIndex++;
    m_ListCtrl.SetItemState(m_nIndex,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);
}

void CDataView::OnEdit() 
{
// TODO: Add your command handler code here
POSITION pos = m_ListCtrl.GetFirstSelectedItemPosition();
    m_nIndex = m_ListCtrl.GetNextSelectedItem(pos);
    if(m_nIndex == -1)
{
   MessageBox("请选择一行!");
   return ;
}
CEditDlg dlg;
dlg.m_strNo = m_ListCtrl.GetItemText(m_nIndex,0);
dlg.m_strName = m_ListCtrl.GetItemText(m_nIndex,1);
dlg.m_strPassword = m_ListCtrl.GetItemText(m_nIndex,2);

    if(IDOK == dlg.DoModal())
{
   m_ListCtrl.SetRedraw(false);
   m_ListCtrl.DeleteItem(m_nIndex);
        m_ListCtrl.InsertItem(m_nIndex,dlg.m_strNo);
        m_ListCtrl.SetItemText(m_nIndex,1,dlg.m_strName);
   m_ListCtrl.SetItemText(m_nIndex,2,dlg.m_strPassword);
   m_ListCtrl.SetRedraw(true);
}
}

void CDataView::OnAdd() 
{
// TODO: Add your command handler code here
CEditDlg dlg;
if(dlg.DoModal() == IDOK)
{
   int count=m_ListCtrl.InsertItem(m_ListCtrl.GetItemCount(),dlg.m_strNo);
   m_ListCtrl.SetItemText(count,1,dlg.m_strName);
        m_ListCtrl.SetItemText(count,2,dlg.m_strPassword);
}
}

void CDataView::OnDelete() 
{
// TODO: Add your command handler code here
if(m_nIndex == -1)
{
   MessageBox("请选择一行!");
   return ;
}
m_ListCtrl.DeleteItem(m_nIndex);
UINT flag = LVIS_SELECTED|LVIS_FOCUSED;
    m_ListCtrl.SetItemState(m_nIndex-1, flag, flag);
}

 

void CDataView::OnSave() 
{
// TODO: Add your command handler code here
CFile f;
if(f.Open("save.txt",CFile::modeCreate|CFile::modeWrite,NULL))
{
   CString str="ID号/t/tusername/t/tpassword/r/n";
   f.Write(str,strlen(str)+1);
   for(int i=0;i<m_ListCtrl.GetItemCount();i++)
   {
    str=m_ListCtrl.GetItemText(i,0)+"/t/t"+m_ListCtrl.GetItemText(i,1)+
     "/t/t"+m_ListCtrl.GetItemText(i,2)+"/r/n";
            f.Write(str,strlen(str)+1);
   }
   f.Close();
   MessageBox("保存成功!");
}
else
{
   MessageBox("保存失败!");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值