Click here to Skip to main content
15,895,557 members
Articles / Desktop Programming / MFC

A Simple Reusable List Control

Rate me:
Please Sign up or sign in to vote.
3.94/5 (20 votes)
29 Nov 20052 min read 76.2K   5K   36  
This artcile describes a simple reusable list control
// CustomListControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CustomListControl.h"
#include "CustomListControlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CCustomListControlDlg dialog



CCustomListControlDlg::CCustomListControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCustomListControlDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCustomListControlDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_CUSTOM, mListCtrlMyList);
}

BEGIN_MESSAGE_MAP(CCustomListControlDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CCustomListControlDlg message handlers

BOOL CCustomListControlDlg::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

	/***************************DEMO********************************************/

    mListCtrlMyList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
    // InsertColumns
    mListCtrlMyList.InsertColumn(0, _T("COLUMN1"), LVCFMT_LEFT, 120);
    mListCtrlMyList.InsertColumn(1, _T("COLUMN2"), LVCFMT_LEFT, 120);
    mListCtrlMyList.InsertColumn(2, _T("COLUMN3"), LVCFMT_LEFT, 120);
    mListCtrlMyList.InsertColumn(3, _T("COLUMN4"), LVCFMT_LEFT, 120);

    mListCtrlMyList.InsertItem(0, NULL);
    mListCtrlMyList.InsertItem(2, NULL);
    mListCtrlMyList.InsertItem(3, NULL);
    mListCtrlMyList.InsertItem(4, NULL);
    mListCtrlMyList.InsertItem(5, NULL);
    mListCtrlMyList.InsertItem(6, NULL);
    mListCtrlMyList.InsertItem(7, NULL);
    
    // Make cell (3,4) editable
    mListCtrlMyList.SetEditBox(1, 3);
    mListCtrlMyList.SetItemText(1, 3, L"Editable Cell");

    mListCtrlMyList.SetEditBox(3, 2);
    mListCtrlMyList.SetItemText(3, 2, L"Editable Cell");


    // Set combo box in cell (2,3)
    CStringArray acsComboStrings;   
    acsComboStrings.Add(L"String1");
    acsComboStrings.Add(L"String2");
    acsComboStrings.Add(L"String3");
    acsComboStrings.Add(L"String4");
    mListCtrlMyList.SetComboBox(1,
                                2,
                                acsComboStrings);
    mListCtrlMyList.SetItemText(1, 2, L"Here is a combo box");

    // Add text color and background color for item
    mListCtrlMyList.AddColoredRow(0,
                                  RGB(30,34,145),
                                  RGB(156,53,45));
    // Add a text to see the result
    mListCtrlMyList.SetItemText(0, 0, L"Color!!");
    
    // Add text color and background color for item
    mListCtrlMyList.AddColoredRow(2,
                                  RGB(255,0,255),
                                  RGB(56,253,45));
    // Add a text to see the result
    mListCtrlMyList.SetItemText(2, 1, L"Color!!");
    
    // Add text color and background color for item
    mListCtrlMyList.AddColoredRow(4,
                                  RGB(230,34,55),
                                  RGB(156,253,145));
    // Add a text to see the result
    mListCtrlMyList.SetItemText(4, 3, L"Color!!");


    /**************************************************************************/
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCustomListControlDlg::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 CCustomListControlDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCustomListControlDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Hi,this is Shine from India, working as a software engineer. I am working mainly with C++ and C#. I like to create reusable stuffs.

Comments and Discussions