// ListCtrlDemoDlg.cpp : implementation file
//
// Written by matt weagle (matt_weagle@hotmail.com)
// Copyright (c) 2000.
//
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.
// Consider yourself lucky if it works, unlucky if it doesn't.
//
// Thanks to Chris Maunder (chris@codeproject.com) for the
// foregoing disclaimer.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
#include "stdafx.h"
#include "ListCtrlDemo.h"
#include "ListCtrlDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "Hyperlink.h"
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
CHyperLink m_cEmail;
//}}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)
virtual BOOL OnInitDialog();
//}}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)
DDX_Control(pDX, IDC_EMAIL, m_cEmail);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListCtrlDemoDlg dialog
CListCtrlDemoDlg::CListCtrlDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CListCtrlDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CListCtrlDemoDlg)
m_nItems = 100;
m_nSelectItem = 0;
m_bImage = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CListCtrlDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CListCtrlDemoDlg)
DDX_Control(pDX, IDC_LOG, m_Log);
DDX_Control(pDX, IDC_LIST_CTRL, m_cListCtrl);
DDX_Control(pDX, IDC_LIST_CTRL_MODE, m_cTabListMode);
DDX_Text(pDX, IDC_LIST_CTRL_NUMITEMS, m_nItems);
DDV_MinMaxInt(pDX, m_nItems, 0, 32257);
DDX_Text(pDX, IDC_LIST_CTRL_SELECTITEM, m_nSelectItem);
DDV_MinMaxInt(pDX, m_nSelectItem, 0, 32567);
DDX_Check(pDX, IDC_BK_IMAGE, m_bImage);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CListCtrlDemoDlg, CDialog)
//{{AFX_MSG_MAP(CListCtrlDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(TCN_SELCHANGE, IDC_LIST_CTRL_MODE, OnSelchangeListCtrlMode)
ON_BN_CLICKED(IDC_UPDATE_ITEM_COUNT, OnUpdateItemCount)
ON_BN_CLICKED(IDC_STYLES_EX, OnStylesEx)
ON_BN_CLICKED(IDC_STYLES, OnStyles)
ON_BN_CLICKED(IDC_GET_SELECTED, OnGetSelected)
ON_BN_CLICKED(IDC_ITEM_SELECT, OnItemSelect)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_CTRL, OnItemchangedListCtrl)
ON_NOTIFY(NM_CLICK, IDC_LIST_CTRL, OnClickListCtrl)
ON_NOTIFY(NM_RCLICK, IDC_LIST_CTRL, OnRclickListCtrl)
ON_BN_CLICKED(IDC_CLEAR_LOG, OnClearLog)
ON_BN_CLICKED(IDC_BK_IMAGE, OnBkImage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListCtrlDemoDlg message handlers
BOOL CListCtrlDemoDlg::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
// Initial extended style for the list control on this dialog
DWORD dwStyle = m_cListCtrl.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;
m_cListCtrl.SetExtendedStyle(dwStyle);
// initialize the standard and custom hot cursors
m_hCustomHotCursor= AfxGetApp()->LoadCursor(IDC_HOTCURSOR);
m_hHotCursor= m_cListCtrl.GetHotCursor();
m_bHotCursor = FALSE;
// Setup the tab header
InitTabCtrl();
// Setup the column headings
InitListCtrlCols();
// Create the image list that is attached to the list control
InitImageList();
// Insert the default dummy items
InsertItems();
return TRUE; // return TRUE unless you set the focus to a control
}
void CListCtrlDemoDlg::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 CListCtrlDemoDlg::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 CListCtrlDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////////////
/*
InitImageList
Create the image list that is associated with the list control. Then
attach the list of images to the control so that we can add items with
images.
Params
None
Returns
BOOL Image list created and attached?
*/
/////////////////////////////////////////////////////////////////////////////
BOOL CListCtrlDemoDlg::InitImageList()
{
// Create 256 color image lists
HIMAGELIST hList = ImageList_Create(32,32, ILC_COLOR8 |ILC_MASK , 8, 1);
m_cImageListNormal.Attach(hList);
hList = ImageList_Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 1);
m_cImageListSmall.Attach(hList);
// Load the large icons
CBitmap cBmp;
cBmp.LoadBitmap(IDB_IMAGES_NORMAL);
m_cImageListNormal.Add(&cBmp, RGB(255,0, 255));
cBmp.DeleteObject();
// Load the small icons
cBmp.LoadBitmap(IDB_IMAGES_SMALL);
m_cImageListSmall.Add(&cBmp, RGB(255,0, 255));
// Attach them
m_cListCtrl.SetImageList(&m_cImageListNormal, LVSIL_NORMAL);
m_cListCtrl.SetImageList(&m_cImageListSmall, LVSIL_SMALL);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
/*
InitTabCtrl
Setup the tab control with the differ
VC++ListCtrl
5星 · 超过95%的资源 需积分: 0 145 浏览量
更新于2008-10-31
收藏 86KB RAR 举报
**VC++ ListCtrl详解**
`CListCtrl`是Microsoft Visual C++ MFC库中的一个控件,用于在用户界面中显示列表数据。它通常用于创建类似Windows资源管理器的列表视图,可以显示多列数据,并支持各种定制,如图像、复选框和自定义消息处理。在本篇文章中,我们将深入探讨`CListCtrl`的使用方法、功能以及如何在实际项目中应用。
**1. 初始化与添加列**
在创建`CListCtrl`对象后,我们首先要添加列以定义显示的数据结构。可以通过调用`InsertColumn`函数来完成,该函数接受列标题、列宽、列格式(例如,文本左对齐或数值右对齐)等参数。
```cpp
int nCol = m_ListCtrl.InsertColumn(0, _T("Column1"), LVCFMT_LEFT, 100);
```
**2. 添加项与子项**
使用`InsertItem`函数添加主项,然后通过`SetItemText`设置每一列的值。对于具有多列的项,可以通过`SetSubItem`或`SetItem`函数来指定列索引和文本。
```cpp
int nItem = m_ListCtrl.InsertItem(LVIF_TEXT, 0, _T("Item1"));
m_ListCtrl.SetItemText(nItem, 1, _T("Value1"));
```
**3. 图像列表支持**
`CListCtrl`支持图像列表,可以为项和子项添加图标。首先创建一个`CImageList`对象,然后将其设置为`CListCtrl`的图像列表。通过`SetImageList`函数可以实现。
```cpp
CImageList imageList;
imageList.Create(16, 16, ILC_COLOR32, 5, 5);
// 加载图像资源
imageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1), RGB(0, 0, 0));
m_ListCtrl.SetImageList(&imageList, LVSIL_SMALL);
```
**4. 复选框支持**
要启用复选框,设置`LVS_CHECKBOXES`样式。`SetExtendedStyle`函数可用于设置扩展样式。
```cpp
m_ListCtrl.SetExtendedStyle(m_ListCtrl.GetExtendedStyle() | LVS_EX_CHECKBOXES);
```
**5. 事件处理**
`CListCtrl`提供了丰富的消息映射,如`NM_CLICK`(单击项)、`LVN_ITEMCHANGED`(项状态改变)等。在`ON_NOTIFY`中添加相应的处理函数。
```cpp
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTCTRL, OnItemChanged)
```
**6. 自定义视图**
通过重写`OnDrawItem`和`OnMeasureItem`函数,可以实现自定义绘制项,以创建更复杂的视图,例如带有渐变背景、自定义边框等。
**7. 数据绑定**
利用MFC的`CRecordView`或`CFormView`类,可以将数据库记录直接绑定到`CListCtrl`,实现数据的实时更新。
**8. 动态数据加载**
当数据量较大时,可以采用虚拟模式(`LVS_OWNERDATA`),只在需要时加载数据,提高性能。
**9. 拖放操作**
通过实现`OleDragDrop`和`OleGiveFeedback`等接口,可以实现拖放操作,允许用户重新排序项或从其他应用程序导入数据。
**10. 编程实践**
`ListCtrlDemo`示例项目提供了一个完整的`CListCtrl`使用实例,包括添加列、插入项、设置图像、处理事件等功能,是学习`CListCtrl`的绝佳起点。
`CListCtrl`是VC++中强大的数据展示工具,通过熟练掌握其各种功能和技巧,可以创建出用户友好的、功能丰富的界面。理解并运用这些知识点,将有助于提升你的MFC编程能力。
yufenghyc
- 粉丝: 29
- 资源: 3
最新资源
- 城镇老旧小区改造(加装电梯)考评内容和评价标准表.docx
- 城镇老旧小区改造及既有住宅加装电梯赋分权重.docx
- 底板隐蔽前监理检查记录.docx
- 出差审批单(表格模板).docx
- 第三方技术服务机构消防验收项目情况工作月汇报表.docx
- 电梯质量安全风险管控清单(安装(含修理).docx
- 飞机舱位代码表.docx
- 顶板隐蔽前监理检查记录表.docx
- 高危妊娠产前评分标准表.docx
- 高温中暑病例报告卡表格.docx
- 个体工商户营业执照颁发及归档记录表.doc
- 更换输液流程表.docx
- 公务接待审批单(表格模板).docx
- 古今地名对照表.docx
- 固定资产验收单、移交清单、处置清单.docx
- 骨关节损伤鉴定标准条款表.docx