CListCtrl控件中InsertItem和SettItemtext函数的用法简介
来源:互联网 发布:java hashcode 算法 编辑:程序博客网 时间:2024/06/05 21:04
本人初次用CListCtrl控件的时候,对于 InsertItem和SetrtItemtext两个函数的作用始终不是太懂,比如如果不先调用InsertItem这个函数,后面的InsertItemtext函数写了,也不起作用.查了MSDN,也没有收获,经过有人点拔,才恍然大悟.今天将经验说一下,希望有共同爱好者少走弯路. InsertItem在MSDN中的函数原型如下 要调用第三个,可以先声明一个CImageList对象 现在转入正题,先介绍一下INSERTITEM个个参数的意义 CListCtrl m_listctrl; m_listctrl = new ClistCtrl(); m_listctrl.Creae(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS m_SetItemtext(0,1,"1"); m_SetItemtext(0,2,"2"); 获得可以这样的道 int columns= m_listctr.GetHeaderCtrl()->GetItemCount(); 获得可以这样的道 int rows= m_listctrl.GetItemCount(); #include "stdafx.h" #include "DataDoc.h" ///////////////////////////////////////////////////////////////////////////// IMPLEMENT_DYNCREATE(CDataView, CView) BEGIN_MESSAGE_MAP(CDataView, CView) ///////////////////////////////////////////////////////////////////////////// CDataView::CDataView() CDataView::~CDataView() BOOL CDataView::PreCreateWindow(CREATESTRUCT& cs) return CView::PreCreateWindow(cs); ///////////////////////////////////////////////////////////////////////////// void CDataView::OnDraw(CDC* pDC) ///////////////////////////////////////////////////////////////////////////// BOOL CDataView::OnPreparePrinting(CPrintInfo* pInfo) void CDataView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) void CDataView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) ///////////////////////////////////////////////////////////////////////////// #ifdef _DEBUG void CDataView::Dump(CDumpContext& dc) const CDataDoc* CDataView::GetDocument() // non-debug version is inline ///////////////////////////////////////////////////////////////////////////// void CDataView::OnInitialUpdate() m_ListCtrl.InsertItem(0,"01"); m_ListCtrl.InsertItem(1,"02"); m_ListCtrl.InsertItem(2,"03"); extern CDataApp theApp; m_ListCtrl.InsertItem(2,(_bstr_t)m_pRecordset->GetCollect("ID")); m_pRecordset->MoveNext(); } if(IDOK == dlg.DoModal()) } void CDataView::OnUp() m_ListCtrl.DeleteItem(m_nIndex); m_ListCtrl.DeleteItem(m_nIndex); void CDataView::OnEdit() if(IDOK == dlg.DoModal()) void CDataView::OnAdd() void CDataView::OnDelete() void CDataView::OnSave()
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种和第三种,其中第三种时建立一个带有头标的项
m_ImageList.Create(16,16,true,2,2);
m_ImageList.Add(theApp.LoadIcon(IDR_MAINFRAME));
m_ListCtrl.SetImageList(&m_ImageList,LVSIL_SMALL);
这样,控件中的每一行的开头就有一个应用程序图标
nItem: 控件中行的索引
lpszItem : 控件头的名字
.........................................................................................................................................................................................................
SettItemtext函数的原型
BOOL SetItemText( int nItem, int nSubItem, LPTSTR lpszText );
******************注意这个函数中的参数nItem就是INSERTITEM中的NITEM,
也就是说InsertItem是向控件声请一行来放数据,记住只是声请,那么方数据的工作就又SETITEMTEXT来实现
举例如下:
,bigRect,this,IDC_LIST1
);
m_listctrl.InsertItem(0,"01");
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
至于要添多少多少个数据,就由你的控件由多少列了
下面附一个本人学习的实例
// DataView.cpp : implementation of the CDataView class
//
#include "Data.h"
#include "DataView.h"
#include "EditDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CDataView
//{{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
{
// TODO: add construction code here
m_nIndex = -1;
}
{
}
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
}
// CDataView drawing
{
CDataDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
// CDataView printing
{
// default preparation
return DoPreparePrinting(pInfo);
}
{
// TODO: add extra initialization before printing
}
{
// TODO: add cleanup after printing
}
// CDataView diagnostics
void CDataView::AssertValid() const
{
CView::AssertValid();
}
{
CView::Dump(dc);
}
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDataDoc)));
return (CDataDoc*)m_pDocument;
}
#endif //_DEBUG
// CDataView message handlers
{
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.SetItemText(0,1,"chetnao");
m_ListCtrl.SetItemText(0,2,"921984");
m_ListCtrl.SetItemText(1,1,"mada");
m_ListCtrl.SetItemText(1,2,"345678");
m_ListCtrl.SetItemText(2,1,"luoxian");
m_ListCtrl.SetItemText(2,2,"987654");
}
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(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"));
}*/
}
}
}
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);
{
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;
{
// 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.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.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);
}
{
// 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);
{
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);
}
}
{
// 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);
}
}
{
// 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);
}
{
// 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("保存失败!");
}
}
- CListCtrl控件中InsertItem和SettItemtext函数的用法简介
- CListCtrl控件中InsertItem和SettItemtext函数的用法简介
- CListCtrl控件中InsertItem和SettItemtext函数的用法简介
- CListCtrl控件中InsertItem和SettItemtext函数的用法简介
- 怎样使用CListCtrl控件中InsertItem和SettItemtext函数
- CListCtrl控件中InsertItem和SetItemText函数
- CListCtrl控件中InsertItem和SetItemText函数
- CListCtrl控件中InsertItem和SetItemText函数
- CListCtrl控件中InsertItem和SetItemText函数
- CListCtrl控件中InsertItem和SetItemText函数
- CListCtrl控件中InsertItem和SetItemText函数及CTreeCtrl::SetItemText
- CListCtrl控件的InsertItem和SetItemText和SetItem三个函数的区别
- CListCtrl控件的InsertItem和SetItemText和SetItem三个函数的区别
- CListCtrl InsertItem和SetItemText
- 关于MFC中CListCtrl::InsertColumn()和InsertItem()和SetItemText()的一些认识
- CListCtrl::InsertItem和CImageList::Add
- CListCtrl 类中InsertItem, InsertColumn, SetItemText 的使用
- mfc的CListCtrl关于InsertItem
- 收发邮件的礼仪汇总
- C++ using namespace std 详解
- 关于显卡的基本工作原理
- D-Bus Tutorial
- 帮你入门 详解JRE和JDK的区别
- CListCtrl控件中InsertItem和SettItemtext函数的用法简介
- PowerDesign高级应用
- Jquery Ajax GetJson实现跨域调用
- 显卡的vbe标准
- 显卡的vbe标准
- Fedora13 鼠标左键失灵修改配置
- Ruby中的百分号表示法
- C#压缩解压缩 网上提供的一个解决方案的BUG修正
- BO报表展示