Click here to Skip to main content
15,885,631 members
Articles / Desktop Programming / WTL

WTL Virtual Listview Control

Rate me:
Please Sign up or sign in to vote.
4.75/5 (24 votes)
28 Jun 2011CPOL3 min read 53.2K   4.2K   45  
A data-bound extension of the Windows listview control for WTL.
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
		public CMessageFilter, public CIdleHandler
{
public:
	enum { IDD = IDD_MAINDLG };
	CWTLVirtualList<Ctitles> m_view;

	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
//		return CWindow::IsDialogMessage(pMsg);

		// Changed so escape key is handled by edit not by dialog
		pMsg;
		return FALSE;
	}

	virtual BOOL OnIdle()
	{
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CMainDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		COMMAND_ID_HANDLER(ID_DATA_NEW, OnDataNew)
		COMMAND_ID_HANDLER(ID_DATA_COPY, OnDataCopy)
		COMMAND_ID_HANDLER(ID_DATA_DELETE, OnDataDelete)
		COMMAND_ID_HANDLER(ID_DATA_DELETEALL, OnDataDeleteAll)
		COMMAND_ID_HANDLER(ID_MOVE_FIRST, OnMoveFirst)
		COMMAND_ID_HANDLER(ID_MOVE_LAST, OnMoveLast)
		COMMAND_ID_HANDLER(ID_MOVE_PREVIOUS, OnMovePrevious)
		COMMAND_ID_HANDLER(ID_MOVE_NEXT, OnMoveNext)
		REFLECT_NOTIFICATIONS()
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// center the dialog on the screen
		CenterWindow();

		// set icons
		HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
			IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
		SetIcon(hIcon, TRUE);
		HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
			IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
		SetIcon(hIconSmall, FALSE);

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);

		UIAddChildWindowContainer(m_hWnd);

		// Initialize the virtual list view
		m_view.Init(GetDlgItem(IDC_LIST1));

		// Set first visible column width. Needed because the default
		// width is set to the control's width on the dialog
		int col = 0;
		if (!m_view.GetShowBookmarks()) col = 1;
		m_view.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);

		// Set the background color of alternating rows
		m_view.SetBarColor(BLUEBAR);

		return TRUE;
	}

	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// unregister message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->RemoveMessageFilter(this);
		pLoop->RemoveIdleHandler(this);

		return 0;
	}

	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CAboutDlg dlg;
		dlg.DoModal();
		return 0;
	}

	LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: Add validation code 
		CloseDialog(wID);
		return 0;
	}

	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CloseDialog(wID);
		return 0;
	}

	LRESULT OnDataNew(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// Example of how to insert a new blank row. Make sure to initialize any
		// key values and non-null fields before Insert is called. After that, non-
		// mandatory values can be manually entered in the listview
		
		m_view.m_data.ClearRecordMemory(); // Set all variables to 0

		TCHAR* TitleID = _T("ZZ9998"); // Non-null key value is required
		_tcsncpy_s(m_view.m_data.m_title_id, _tcslen(TitleID) + 1, TitleID, _TRUNCATE);

		DBTIMESTAMP pubdate = { 0 }; // Non-null date value is required
		pubdate.year = 2010;
		pubdate.month = 12;
		pubdate.day = 31;
		m_view.m_data.m_pubdate = pubdate;

		return m_view.OnDataNew(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnDataCopy(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// Example of how to copy an existing selected row by not initializing the
		// active record memory to 0s. TitleID is a required value and must be
		// supplied for the insert to succeed
		TCHAR* TitleID = _T("ZZ9999");
		_tcsncpy_s(m_view.m_data.m_title_id, _tcslen(TitleID) + 1, TitleID, _TRUNCATE);

		return m_view.OnDataNew(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnDataDelete(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		return m_view.OnDataDelete(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnDataDeleteAll(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		return m_view.OnDataDeleteAll(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnMoveFirst(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		return m_view.OnMoveFirst(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnMoveLast(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		return m_view.OnMoveLast(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnMovePrevious(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		return m_view.OnMovePrevious(wNotifyCode, wID, hWndCtl, bHandled);
	}

	LRESULT OnMoveNext(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		return m_view.OnMoveNext(wNotifyCode, wID, hWndCtl, bHandled);
	}

	void CloseDialog(int nVal)
	{
		DestroyWindow();
		::PostQuitMessage(nVal);
	}
};

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Choycer
United States United States
Ed has over 40 years experience in computer technology and a bachelor's degree in Business Administration. He's currently a marketing technology consultant. During his career, he's led software development departments and created software still in use in the communications and healthcare industries. Ed is a veteran of the United States Army. He lives in Arizona in the United States.

Find Ed on Linkedin.

This material is copyright 2019 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

Comments and Discussions