vc listctrl

p161
// 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

Construction

CListCtrlConstructs a CListCtrl object.
CreateCreates a list control and attaches it to a CListCtrl object.

Attributes

GetBkColorRetrieves the background color of a list view control.
SetBkColorSets the background color of the list view control.
GetImageListRetrieves the handle of an image list used for drawing list view items.
SetImageListAssigns an image list to a list view control.
GetItemCountRetrieves the number of items in a list view control.
GetItemRetrieves a list view item’s attributes.
SetItemSets some or all of a list view item’s attributes.
GetCallbackMaskRetrieves the callback mask for a list view control.
SetCallbackMaskSets the callback mask for a list view control.
GetNextItemSearches for a list view item with specified properties and with specified relationship to a given item.
GetFirstSelectedItemPositionRetrieves the position of the first selected list view item in a list view control.
GetNextSelectedItemRetrieves the next selected list view item for iterating.
GetItemRectRetrieves the bounding rectangle for an item.
SetItemPositionMoves an item to a specified position in a list view control.
GetItemPositionRetrieves the position of a list view item.
GetStringWidthDetermines the minimum column width necessary to display all of a given string.
GetEditControlRetrieves the handle of the edit control used to edit an item’s text.
GetColumnRetrieves the attributes of a control’s column.
SetColumnSets the attributes of a list view column.
GetColumnWidthRetrieves the width of a column in report view or list view.
SetColumnWidthChanges the width of a column in report view or list view.
GetCheckRetrieves the current display status of the state image associated with an item.
SetCheckSets the the current display status of the state image associated with an item.
GetViewRectRetrieves the bounding rectangle of all items in the list view control.
GetTextColorRetrieves the text color of a list view control.
SetTextColorSets the text color of a list view control.
GetTextBkColorRetrieves the text background color of a list view control.
SetTextBkColorSets the background color of text in a list view control.
GetTopIndexRetrieves the index of the topmost visible item.
GetCountPerPageCalculates the number of items that can fit vertically in a list view control.
GetOriginRetrieves the current view origin for a list view control.
SetItemStateChanges the state of an item in a list view control.
GetItemStateRetrieves the state of a list view item.
GetItemTextRetrieves the text of a list view item or subitem.
SetItemTextChanges the text of a list view item or subitem.
SetItemCountPrepares a list view control for adding a large number of items.
SetItemDataSets the item’s application-specific value.
GetItemDataRetrieves the application-specific value associated with an item.
GetSelectedCountRetrieves the number of selected items in the list view control.
SetColumnOrderArraySets the column order (left to right) of a list view control.
GetColumnOrderArrayRetrieves the column order (left to right) of a list view control.
SetIconSpacingSets the spacing between icons in a list view control.
GetHeaderCtrlRetrieves the header control of a list view control.
GetHotCursorRetrieves the cursor used when hot tracking is enabled for a list view control.
SetHotCursorSets the cursor used when hot tracking is enabled for a list view control.
GetSubItemRectRetrieves the bounding rectangle of an item in a list view control.
GetHotItemRetrieves the list view item currently under the cursor.
SetHotItemSets the current hot item of a list view control.
GetSelectionMarkRetrieves the selection mark of a list view control.
SetSelectionMarkSets the selection mark of a list view control.
GetExtendedStyleRetrieves the current extended styles of a list view control.
SetExtendedStyleSets the current extended styles of a list view control.
SubItemHitTestDetermines which list view item, if any, is at a given position.
GetWorkAreasRetrieves the current working areas of a list view control.
GetNumberOfWorkAreasRetrieves the current number of working areas for a list view control.
SetItemCountExSets the item count for a virtual list view control.
SetWorkAreasSets the area where icons can be displayed in a list view control.
ApproximateViewRectDetermines the width and height required to display the items of a list view control.
GetBkImageRetreives the current background image of a list view control.
SetBkImageSets the current background image of a list view control.
GetHoverTimeRetrieves the current hover time of a list view control.
SetHoverTimeSets the current hover time of a list view control.

Operations

InsertItemInserts a new item in a list view control.
DeleteItemDeletes an item from the control.
DeleteAllItemsDeletes all items from the control.
FindItemSearches for a list view item having specified characteristics.
SortItemsSorts list view items using an application-defined comparison function.
HitTestDetermines which list view item is at a specified position.
EnsureVisibleEnsures that an item is visible.
ScrollScrolls the content of a list view control.
RedrawItemsForces a list view control to repaint a range of items.
UpdateForces the control to repaint a specified item.
ArrangeAligns items on a grid.
EditLabelBegins in-place editing of an item’s text.
InsertColumnInserts a new column in a list view control.
DeleteColumnDeletes a column from the list view control.
CreateDragImageCreates a drag image list for a specified item.

Overridables

DrawItemCalled when a visual aspect of an owner-draw control changes.

http://download.csdn.net/detail/luck_good/3786746
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值