p161
1.添加一个listctrl控件,为其关联ClistCtrl类型的变量(对象)m_list1
2.
使用 m_list1.InsertColumn添加列头
使用m_list1.InsertItem添加列表条目
使用m_list1.SetItemText为某条目的某个列段设置text
使用m_list1.SetImageList关联imagelist
使用m_list1.GetFirstSelectedItemPosition();取得所选择的条目
使用m_list1.GetItemText取de某条目的某个列段设置text
使用GetWindowLong取得list的显示类型
使用SetWindowLong设置list的显示类型
http://download.csdn.net/detail/luck_good/3786746
// Ex040202Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Ex040202.h"
#include "Ex040202Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CEx040202Dlg dialog
CEx040202Dlg::CEx040202Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CEx040202Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEx040202Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEx040202Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx040202Dlg)
DDX_Control(pDX, IDC_LIST1, m_list1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEx040202Dlg, CDialog)
//{{AFX_MSG_MAP(CEx040202Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CEx040202Dlg message handlers
BOOL CEx040202Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//增加列头
m_list1.InsertColumn(0,"姓名",LVCFMT_LEFT,100);
m_list1.InsertColumn(1,"特长",LVCFMT_LEFT,100);
//增加内容
int nItem=m_list1.InsertItem(0,"张3",0) ;
//为m_list1增加一项,
//第一个0表示在list中的序号,返回值nItem代表在list中的实际序号
//张3,设定本条目的第0个域即姓名为是张3,相当于执行m_list1.SetItemText(nItem,0,"张3");
//第二个0表示使用于imagelsit第0项作为图标
m_list1.SetItemText(nItem,1,"热爱自然");
//设置第nItem条项目的第1个域即特长为"热爱自然"
nItem=m_list1.InsertItem(1,"李4",1) ;
m_list1.SetItemText(nItem,1,"热爱祖国");
nItem=m_list1.InsertItem(2,"王5",2) ;
m_list1.SetItemText(nItem,1,"热爱老婆");
{//大图标。正常图标使用大图标
CBitmap bt;
bt.LoadBitmap(IDB_BITMAP1);
m_imageList.Create(32,32,ILC_COLOR24,3,1);
m_imageList.Add(&bt,RGB(216,233,236));//m_imageList关联IDB_BITMAP1
}
{//小图标。小图标,列表,详细均使用小图标
CBitmap bt;
bt.LoadBitmap(IDB_BITMAP2);
m_smallImageList.Create(16,16,ILC_COLOR24,3,1);
m_smallImageList.Add(&bt,RGB(216,233,236));//m_smallImageList关联IDB_BITMAP2
}
m_list1.SetImageList(&m_imageList,LVSIL_NORMAL);//m_list1关联正常imageList,LVSIL_NORMAL
m_list1.SetImageList(&m_smallImageList,LVSIL_SMALL);//m_list1关联小m_imageList,LVSIL_SMALL
return TRUE; // return TRUE unless you set the focus to a control
}
void CEx040202Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEx040202Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEx040202Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEx040202Dlg::OnOK()
{
// TODO: Add extra validation here
CString strMess="you selected the student whose name and hobby is \n \n";
POSITION pos=m_list1.GetFirstSelectedItemPosition();
int nItem=m_list1.GetNextSelectedItem(pos);
strMess+=m_list1.GetItemText(nItem,0);//取得选中的那项的name
strMess+="\t";
strMess+=m_list1.GetItemText(nItem,1);//取得选中的那项的hobby
strMess+="\n";
AfxMessageBox(strMess);
}
void CEx040202Dlg::SetStyle(DWORD uStyle)
{
long lStyle;
lStyle=GetWindowLong(m_list1.m_hWnd,GWL_STYLE);//取得list窗口即m_list1.m_hWnd的类型
lStyle&=~LVS_TYPEMASK;//清除类型位
lStyle|=uStyle;//添加类型位
SetWindowLong(m_list1.m_hWnd,GWL_STYLE,lStyle);//为list窗口即m_list1.m_hWnd设置类型
}
void CEx040202Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
SetStyle(LVS_ICON);//正常图标
}
void CEx040202Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
SetStyle(LVS_LIST);//列表
}
void CEx040202Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
SetStyle(LVS_REPORT);//详细
}
void CEx040202Dlg::OnButton4()
{
// TODO: Add your control notification handler code here
SetStyle(LVS_SMALLICON);//小图标
}
1.添加一个listctrl控件,为其关联ClistCtrl类型的变量(对象)m_list1
2.
使用 m_list1.InsertColumn添加列头
使用m_list1.InsertItem添加列表条目
使用m_list1.SetItemText为某条目的某个列段设置text
使用m_list1.SetImageList关联imagelist
使用m_list1.GetFirstSelectedItemPosition();取得所选择的条目
使用m_list1.GetItemText取de某条目的某个列段设置text
使用GetWindowLong取得list的显示类型
使用SetWindowLong设置list的显示类型
CListCtrl Class Members
Construction
Attributes
Operations
Overridables
CListCtrl | Constructs a CListCtrl object. |
Create | Creates a list control and attaches it to a CListCtrl object. |
GetBkColor | Retrieves the background color of a list view control. |
SetBkColor | Sets the background color of the list view control. |
GetImageList | Retrieves the handle of an image list used for drawing list view items. |
SetImageList | Assigns an image list to a list view control. |
GetItemCount | Retrieves the number of items in a list view control. |
GetItem | Retrieves a list view item’s attributes. |
SetItem | Sets some or all of a list view item’s attributes. |
GetCallbackMask | Retrieves the callback mask for a list view control. |
SetCallbackMask | Sets the callback mask for a list view control. |
GetNextItem | Searches for a list view item with specified properties and with specified relationship to a given item. |
GetFirstSelectedItemPosition | Retrieves the position of the first selected list view item in a list view control. |
GetNextSelectedItem | Retrieves the next selected list view item for iterating. |
GetItemRect | Retrieves the bounding rectangle for an item. |
SetItemPosition | Moves an item to a specified position in a list view control. |
GetItemPosition | Retrieves the position of a list view item. |
GetStringWidth | Determines the minimum column width necessary to display all of a given string. |
GetEditControl | Retrieves the handle of the edit control used to edit an item’s text. |
GetColumn | Retrieves the attributes of a control’s column. |
SetColumn | Sets the attributes of a list view column. |
GetColumnWidth | Retrieves the width of a column in report view or list view. |
SetColumnWidth | Changes the width of a column in report view or list view. |
GetCheck | Retrieves the current display status of the state image associated with an item. |
SetCheck | Sets the the current display status of the state image associated with an item. |
GetViewRect | Retrieves the bounding rectangle of all items in the list view control. |
GetTextColor | Retrieves the text color of a list view control. |
SetTextColor | Sets the text color of a list view control. |
GetTextBkColor | Retrieves the text background color of a list view control. |
SetTextBkColor | Sets the background color of text in a list view control. |
GetTopIndex | Retrieves the index of the topmost visible item. |
GetCountPerPage | Calculates the number of items that can fit vertically in a list view control. |
GetOrigin | Retrieves the current view origin for a list view control. |
SetItemState | Changes the state of an item in a list view control. |
GetItemState | Retrieves the state of a list view item. |
GetItemText | Retrieves the text of a list view item or subitem. |
SetItemText | Changes the text of a list view item or subitem. |
SetItemCount | Prepares a list view control for adding a large number of items. |
SetItemData | Sets the item’s application-specific value. |
GetItemData | Retrieves the application-specific value associated with an item. |
GetSelectedCount | Retrieves the number of selected items in the list view control. |
SetColumnOrderArray | Sets the column order (left to right) of a list view control. |
GetColumnOrderArray | Retrieves the column order (left to right) of a list view control. |
SetIconSpacing | Sets the spacing between icons in a list view control. |
GetHeaderCtrl | Retrieves the header control of a list view control. |
GetHotCursor | Retrieves the cursor used when hot tracking is enabled for a list view control. |
SetHotCursor | Sets the cursor used when hot tracking is enabled for a list view control. |
GetSubItemRect | Retrieves the bounding rectangle of an item in a list view control. |
GetHotItem | Retrieves the list view item currently under the cursor. |
SetHotItem | Sets the current hot item of a list view control. |
GetSelectionMark | Retrieves the selection mark of a list view control. |
SetSelectionMark | Sets the selection mark of a list view control. |
GetExtendedStyle | Retrieves the current extended styles of a list view control. |
SetExtendedStyle | Sets the current extended styles of a list view control. |
SubItemHitTest | Determines which list view item, if any, is at a given position. |
GetWorkAreas | Retrieves the current working areas of a list view control. |
GetNumberOfWorkAreas | Retrieves the current number of working areas for a list view control. |
SetItemCountEx | Sets the item count for a virtual list view control. |
SetWorkAreas | Sets the area where icons can be displayed in a list view control. |
ApproximateViewRect | Determines the width and height required to display the items of a list view control. |
GetBkImage | Retreives the current background image of a list view control. |
SetBkImage | Sets the current background image of a list view control. |
GetHoverTime | Retrieves the current hover time of a list view control. |
SetHoverTime | Sets the current hover time of a list view control. |
InsertItem | Inserts a new item in a list view control. |
DeleteItem | Deletes an item from the control. |
DeleteAllItems | Deletes all items from the control. |
FindItem | Searches for a list view item having specified characteristics. |
SortItems | Sorts list view items using an application-defined comparison function. |
HitTest | Determines which list view item is at a specified position. |
EnsureVisible | Ensures that an item is visible. |
Scroll | Scrolls the content of a list view control. |
RedrawItems | Forces a list view control to repaint a range of items. |
Update | Forces the control to repaint a specified item. |
Arrange | Aligns items on a grid. |
EditLabel | Begins in-place editing of an item’s text. |
InsertColumn | Inserts a new column in a list view control. |
DeleteColumn | Deletes a column from the list view control. |
CreateDragImage | Creates a drag image list for a specified item. |
DrawItem | Called when a visual aspect of an owner-draw control changes. |
http://download.csdn.net/detail/luck_good/3786746