www.pudn.com > ListViewStradley_demo.zip > ListViewDlg.cpp


// ListViewDlg.cpp : implementation file 
// the dialog implementation 
 
#include "stdafx.h" 
#include "ListView.h" 
#include "ListViewDlg.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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CListViewDlg dialog 
 
CListViewDlg::CListViewDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CListViewDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CListViewDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} // CListViewDlg 
 
void  
CListViewDlg::DoDataExchange (CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CListViewDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} // DoDataExchange 
 
BEGIN_MESSAGE_MAP(CListViewDlg, CDialog) 
	//{{AFX_MSG_MAP(CListViewDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_DESTROY() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CListViewDlg message handlers 
 
BOOL  
CListViewDlg::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); 
		} 
    } 
 
    SetIcon(m_hIcon, TRUE);			// Set big icon 
    SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
 
	// we need to get a pointer to the list control and subclass it, so that  
	// we may catch its messages.  This may not be necessary--you may be able 
	// to have its messages passed up to the dialog.  I prefer this way. 
    CListCtrl* pList = (CListCtrl*)GetDlgItem (IDC_LIST_FIELDS); 
    if (!m_List.SubclassWindow (pList->m_hWnd)) 
		return FALSE; 
 
    initList (); 
 
    // add some dummy fields 
    int cFields = 10; 
    int iField; 
    CString FieldName; 
    CString FieldValue; 
    for (iField = 0; iField < cFields; iField ++) 
    { 
		FieldName.Format ("Field%d", iField); 
		FieldValue.Format ("Value%d", iField); 
		addField (iField, FieldName, FieldValue); 
    } 
 
	m_List.SetFocus (); 
	m_List.EditLabel (0); 
 
    return TRUE;  // return TRUE  unless you set the focus to a control 
} // OnInitDialog 
 
void  
CListViewDlg::OnSysCommand (UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} // OnSysCommand 
 
// 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  
CListViewDlg::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(); 
	} 
} // OnPaint 
 
HCURSOR  
CListViewDlg::OnQueryDragIcon () 
{ 
	return (HCURSOR) m_hIcon; 
} // OnQueryDragIcon 
 
void 
CListViewDlg::initList () 
{ 
    // add two columns 
    LV_COLUMN Column; 
    Column.mask	    = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH; 
    Column.cx	    = 260; 
    Column.pszText  = "Value"; 
    Column.cchTextMax = 10; 
    Column.iSubItem = 1; 
    m_List.InsertColumn (0, &Column); 
 
    Column.mask	    = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH; 
    Column.cx	    = 185; 
    Column.pszText  = "Field"; 
    Column.cchTextMax = 9; 
    Column.iSubItem = 0; 
    m_List.InsertColumn (1, &Column); 
 
    long ExStyle = LVS_EX_GRIDLINES; 
    m_List.SetExtendedStyle (ExStyle); 
 
	// here we are going to swap the column order so that the user can edit 
	// the right hand side data.  Like a label / edit box combonation. 
    int aiCol[2] = { 1, 0 }; 
    m_List.SetColumnOrderArray (2, aiCol); 
} // initList 
 
 
// add an item at row number iItem, with FieldName & FieldValue as the row data 
BOOL 
CListViewDlg::addField (int iItem, CString FieldName, CString FieldValue) 
{ 
    FieldName.TrimRight (); 
    FieldName.TrimLeft (); 
    FieldValue.TrimRight (); 
    FieldValue.TrimLeft (); 
     
    LV_ITEM FieldData; 
 
    FieldData.iItem = iItem; 
    FieldData.mask  = LVIF_TEXT; 
    FieldData.iSubItem = 0; 
    FieldData.pszText = (char*)(LPCSTR)FieldValue; 
    FieldData.cchTextMax = FieldValue.GetLength (); 
    if (m_List.InsertItem (&FieldData) != iItem) 
		return FALSE; 
 
    FieldData.iItem = iItem; 
    FieldData.mask  = LVIF_TEXT; 
    FieldData.iSubItem = 1; 
    FieldData.pszText = (char*)(LPCSTR)FieldName; 
    FieldData.cchTextMax = FieldName.GetLength () + 1; 
    if (m_List.SetItem (&FieldData) == FALSE) 
		return FALSE; 
 
    return TRUE; 
} // addField 
 
 
// unsubclass the list control 
void  
CListViewDlg::OnDestroy ()  
{ 
    m_List.UnsubclassWindow (); 
    CDialog::OnDestroy(); 
} // OnDestroy