这里只介绍report风格。
准备工作:
(1)建立MFC Application
(2)建立一个对话框,添加列表控件
(3)删除TODO文本和OK按钮
(4)调整列表框大小 修改其ID为IDC_STORE_ITEMS
(5)给列表框添加control变量,m_store_items
一些成员函数
(1)LVS_EX_GRIDLINESL:也就是表格的网格线
(2)LVS_EX_TRACKSELECT:当鼠标在某个变量哪里停顿时,默认会选中
一、创建列表
(1)CListCtrl::InsertItem() 方法
*
解释:
int InsertItem(const LVITEM* pItem );
typedef struct _LVITEM {
UINT mask;
int iItem; 表示索引
int iSubItem; 一个索引下的另一个数组下标
UINT state;
UINT stateMask;
LPTSTR pszText;
int cchTextMax;
int iImage;
LPARAM
lParam;
#if (_WIN32_IE >= 0x0300)
int iIndent;
#endif } LVITEM,
FAR *LPLVITEM;
填充列表
01、创建一个新对话框
02、ID改成IDD_SToREITEMS_DLG
3、添加类CNewStoreItemDlg 基类选择CDialog
4、添加一个编辑框edit control 关联Cstring Value变量,
5、在此对话框再添加一个按钮,命名NewItem,ID改为IDD_NEWITEM
6、为按钮添加双击时间函数
void CDeptStore2Dlg::OnBnClickedNewitem()
{
// TODO: Add your control notification handler code here
CNewStoreItemDlg dlg;
srand( (unsigned)time(NULL) );
char strNumber[20];
int number1 = rand() % 999;
int number2 = rand() % 999;
sprintf(strNumber, "%d-%d", number1, number2);
dlg.m_ItemNumber = strNumber;
if( dlg.DoModal() )
{
LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
lvItem.pszText = strNumber;
this->m_StoreItems.InsertItem(&lvItem);
}
7、记得添加头文件
#include "NewStoreItemDlg.h"
列表风格详述
1、函数:CListCtrl::InsertColumn()
int InsertColumn(int nCol, const LVCOLUMN* pColumn);
The nCol argument is the index of the column that this call will create.
The pColumn argument is an LVCOLUMN pointer. This structure is defined
as follows:
也即是对要插入的列进行具体化
typedef struct _LVCOLUMN {
UINT mask; i
nt fmt;
int cx;
LPTSTRpszText;
int cchTextMax;
int iSubItem;
#if (_WIN32_IE >= 0x0300)
int iImage;
int iOrder;
#endif } LVCOLUMN, FAR *LPLVCOLUMN;
2、写进OnInitDialog即可
BOOL COthersDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
LVCOLUMN lvColumn;
int nCol;
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = "Full Name";
nCol = m_List.InsertColumn(0, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 100;
lvColumn.pszText = "Profession";
m_List.InsertColumn(1, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 80;
lvColumn.pszText = "Fav Sport";
m_List.InsertColumn(2, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 75;
lvColumn.pszText = "Hobby";
m_List.InsertColumn(3, &lvColumn);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
或者,使用另外一种方法:
int InsertColumn(int nCol,
LPCTSTR lpszColumnHeading,
int nFormat = LVCFMT_LEFT,//左对齐
int nWidth = -1,//列标题的宽度,如果不想实例化这一列就赋值-1
int nSubItem = -1);//子项的当前序号
需要注意的是InsertItem()方法返回值是一个整数,是新item的index,可以用这个返回值来确定哪个是你添加进来的item,
3、使用string值来填充每一列的值,函数: CListCtrl::SetItemText()
BOOL SetItemText(int nItem, int nSubItem, LPTSTR lpszText);
> nItem:要添加信息的列的编号
> 每个信息存放在一个从零开始的数组里 nSubItem
> lpszText 存放实际文本数据
同样,还是写在InitialDialog中
BOOL COthersDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
LVCOLUMN lvColumn;
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = "Full Name";
m_List.InsertColumn(0, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 75;
lvColumn.pszText = "Profession";
m_List.InsertColumn(1, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 80;
lvColumn.pszText = "Fav Sport";
m_List.InsertColumn(2, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 75;
lvColumn.pszText = "Hobby";
m_List.InsertColumn(3, &lvColumn);
LVITEM lvItem;
int nItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
lvItem.pszText = "Sandra C. Anschwitz";
nItem = m_List.InsertItem(&lvItem);
m_List.SetItemText(nItem, 1, "Singer");
m_List.SetItemText(nItem, 2, "HandBall");
m_List.SetItemText(nItem, 3, "Beach");
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 1;
lvItem.iSubItem = 0;
lvItem.pszText = "Roger A. Miller";
nItem = m_List.InsertItem(&lvItem);
m_List.SetItemText(nItem, 1, "Footballer");
m_List.SetItemText(nItem, 2, "Tennis");
m_List.SetItemText(nItem, 3, "Teaching");
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 2;
lvItem.iSubItem = 0;
lvItem.pszText = "Marie-Julie W. Gross";
nItem = m_List.InsertItem(&lvItem);
m_List.SetItemText(nItem, 1, "Student");
m_List.SetItemText(nItem, 2, "Boxing");
m_List.SetItemText(nItem, 3, "Programming");
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 3;
lvItem.iSubItem = 0;
lvItem.pszText = "Ella Pius Roger";
nItem = m_List.InsertItem(&lvItem);
m_List.SetItemText(nItem, 1, "Architect");
m_List.SetItemText(nItem, 2, "Ping-Pong");
m_List.SetItemText(nItem, 3, "Songo");
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE