⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listctrltestdlg.cpp

📁 EVC写的ListCtrl扩展控件
💻 CPP
字号:
// ListCtrlTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ListCtrlTest.h"
#include "ListCtrlTestDlg.h"

#include "NewItemDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListCtrlTestDlg dialog

CListCtrlTestDlg::CListCtrlTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CListCtrlTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CListCtrlTestDlg)
	m_strSelItem = _T("Current selected item: -");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CListCtrlTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CListCtrlTestDlg)
	DDX_Control(pDX, IDC_LIST1, m_ctrlList);
	DDX_Text(pDX, IDC_STATIC1, m_strSelItem);
	DDX_Control(pDX, IDC_ADD, m_btnAdd);
    DDX_Control(pDX, IDC_DEL, m_btnDel);
    DDX_Control(pDX, IDC_UP, m_btnUp);
    DDX_Control(pDX, IDC_DOWN, m_btnDown);
    DDX_Control(pDX, IDC_TOP, m_btnTop);
    DDX_Control(pDX, IDC_BOTTOM, m_btnBottom);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CListCtrlTestDlg, CDialog)
	//{{AFX_MSG_MAP(CListCtrlTestDlg)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemChangedList1)
    ON_WM_LBUTTONDOWN()
    ON_NOTIFY_REFLECT(NM_RECOGNIZEGESTURE, OnRecognizeGesture)
    ON_WM_CTLCOLOR()
    ON_BN_CLICKED(IDC_ADD, OnBtnAdd)
    ON_BN_CLICKED(IDC_DEL, OnBtnDel)
    ON_BN_CLICKED(IDC_UP, OnBtnUp)
    ON_BN_CLICKED(IDC_DOWN, OnBtnDown)
    ON_BN_CLICKED(IDC_TOP, OnBtnTop)
    ON_BN_CLICKED(IDC_BOTTOM, OnBtnBottom)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListCtrlTestDlg message handlers

BOOL CListCtrlTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

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

	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here

    // Calculate a color effect for hilighting the button
    COLORREF crBtnColor = ::GetSysColor(COLOR_BTNFACE) + RGB(30, 30, 30);

	// Add button
	m_btnAdd.SetFont(GetFont());
	m_btnAdd.SetIcon(IDI_ADD, CSize(16, 16));
	m_btnAdd.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, crBtnColor);
	m_btnAdd.SetRounded(TRUE);

	// Delete button
	m_btnDel.SetFont(GetFont());
	m_btnDel.SetIcon(IDI_DEL, CSize(16, 16), IDI_DEL, CSize(16, 16),
		IDI_DEL_DISABLED, CSize(16, 16));
	m_btnDel.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, crBtnColor);
	m_btnDel.SetRounded(TRUE);

	// Up button
	m_btnUp.SetFont(GetFont());
	m_btnUp.SetIcon(IDI_UP, CSize(16, 16), IDI_UP, CSize(16, 16),
		IDI_UP_DISABLED, CSize(16, 16));
	m_btnUp.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, crBtnColor);
	m_btnUp.SetRounded(TRUE);

	// Down button
	m_btnDown.SetFont(GetFont());
	m_btnDown.SetIcon(IDI_DOWN, CSize(16, 16), IDI_DOWN, CSize(16, 16),
		IDI_DOWN_DISABLED, CSize(16, 16));
	m_btnDown.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, crBtnColor);
	m_btnDown.SetRounded(TRUE);

	// Top button
	m_btnTop.SetFont(GetFont());
	m_btnTop.SetIcon(IDI_TOP, CSize(16, 16), IDI_TOP, CSize(16, 16),
		IDI_TOP_DISABLED, CSize(16, 16));
	m_btnTop.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, crBtnColor);
	m_btnTop.SetRounded(TRUE);

	// Bottom button
	m_btnBottom.SetFont(GetFont());
	m_btnBottom.SetIcon(IDI_BOTTOM, CSize(16, 16), IDI_BOTTOM, CSize(16, 16),
		IDI_BOTTOM_DISABLED, CSize(16, 16));
	m_btnBottom.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, crBtnColor);
	m_btnBottom.SetRounded(TRUE);

    // Create a image list and assign it to the list control
	VERIFY(m_imageList.Create(IDB_BITMAP1, 16, 1, RGB(255, 0, 255)));
	m_ctrlList.SetImageList(&m_imageList, LVSIL_SMALL);

    // Insert some sample items
    for (int n = 0; n < 10; n++)
    {
        CString str;
        str.Format(_T("Item %d"), n);
        m_ctrlList.InsertItem(n, str, n % 8);
    }

  	// Set the focus to the first item in the list control
	if (m_ctrlList.GetItemCount() > 0)
	{
	    m_ctrlList.SetCurSelItem(0);
        ASSERT(m_ctrlList.GetCurSelItem() == 0);
	}

	// Update the list control buttons
    UpdateListCtrlBtns();

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

void CListCtrlTestDlg::UpdateListCtrlBtns()
{
    // Get the current selected item of the list control
    int nIndex = m_ctrlList.GetCurSelItem();

    // Enable / disable the buttons
    m_btnAdd.EnableWindow(TRUE);
    m_btnDel.EnableWindow(nIndex >= 0 && nIndex < m_ctrlList.GetItemCount());
    m_btnUp.EnableWindow(nIndex >= 1 && nIndex < m_ctrlList.GetItemCount());
    m_btnDown.EnableWindow(nIndex >= 0 && nIndex < m_ctrlList.GetItemCount() - 1);
    m_btnTop.EnableWindow(nIndex >= 1 && nIndex < m_ctrlList.GetItemCount());
    m_btnBottom.EnableWindow(nIndex >= 0 && nIndex < m_ctrlList.GetItemCount() - 1);
}

void CListCtrlTestDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    Default(); // call default message handler
}

void CListCtrlTestDlg::OnRecognizeGesture(NMHDR* pNMHDR, LRESULT* pResult)
{
    *pResult = TRUE; // cancel the red dot animation
}

HBRUSH CListCtrlTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO: Change any attributes of the DC here

	// TODO: Return a different brush if the default is not desired

    if (nCtlColor == CTLCOLOR_STATIC)
    {
        pDC->SetBkMode(TRANSPARENT);

        hbr = CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
    }
    else if (pWnd == this)
    {
        hbr = CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
    }

	return hbr;
}

void CListCtrlTestDlg::OnItemChangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR;

	// TODO: Add your control notification handler code here

    UpdateListCtrlBtns(); // Update the list control buttons

	m_strSelItem.Format(_T("Current selected item: %d"), m_ctrlList.GetCurSelItem());
	UpdateData(FALSE);

	*pResult = 0;
}

void CListCtrlTestDlg::OnBtnAdd()
{
    // TODO: Add your control notification handler code here

    CNewItemDlg dlg(m_imageList.GetImageCount() - 1);

    if (dlg.DoModal() == IDOK)
    {
        int n = m_ctrlList.GetItemCount();

        // Add a new item to the list control
        m_ctrlList.InsertItem(n, dlg.m_strCaption, dlg.m_nImage);

        // Set the current selected item
        m_ctrlList.SetCurSelItem(n);
        ASSERT(m_ctrlList.GetCurSelItem() == n);
    }

    UpdateListCtrlBtns();
	m_ctrlList.SetFocus();
}

void CListCtrlTestDlg::OnBtnDel()
{
	// TODO: Add your control notification handler code here

    int nIndex = m_ctrlList.GetCurSelItem();

    if (nIndex >= 0 && nIndex < m_ctrlList.GetItemCount())
    {
        if (AfxMessageBox(_T("Delete item?"),
		    MB_YESNO | MB_ICONQUESTION) == IDYES)
		{
		    // Delete the item in the list control
		    m_ctrlList.DeleteItem(nIndex);

		    // Set the focus to the next list item
            if (nIndex < m_ctrlList.GetItemCount())
			{
                m_ctrlList.SetCurSelItem(nIndex);
                ASSERT(m_ctrlList.GetCurSelItem() == nIndex);
			}
		    else if (m_ctrlList.GetItemCount() > 0)
			{
                nIndex = m_ctrlList.GetItemCount() - 1;

                m_ctrlList.SetCurSelItem(nIndex);
                ASSERT(m_ctrlList.GetCurSelItem() == nIndex);
			}
		}
	}

	UpdateListCtrlBtns();
	m_ctrlList.SetFocus();
}

void CListCtrlTestDlg::OnBtnUp()
{
	// TODO: Add your control notification handler code here

    int nIndex = m_ctrlList.GetCurSelItem();

    if (nIndex >= 1 && nIndex < m_ctrlList.GetItemCount())
    {
        // Move the list item up by one position
        m_ctrlList.MoveItemUp(nIndex);
	}

	UpdateListCtrlBtns();
	m_ctrlList.SetFocus();
}

void CListCtrlTestDlg::OnBtnDown()
{
	// TODO: Add your control notification handler code here

    int nIndex = m_ctrlList.GetCurSelItem();

    if (nIndex >= 0 && nIndex < m_ctrlList.GetItemCount() - 1)
    {
        // Move the list item down by one position
        m_ctrlList.MoveItemDown(nIndex);
	}

	UpdateListCtrlBtns();
	m_ctrlList.SetFocus();
}

void CListCtrlTestDlg::OnBtnTop()
{
	// TODO: Add your control notification handler code here

    int nIndex = m_ctrlList.GetCurSelItem();

    if (nIndex >= 1 && nIndex < m_ctrlList.GetItemCount())
    {
        // Move the item to the top of the list
        m_ctrlList.MoveItemTop(nIndex);
	}

	UpdateListCtrlBtns();
	m_ctrlList.SetFocus();
}

void CListCtrlTestDlg::OnBtnBottom()
{
	// TODO: Add your control notification handler code here

    int nIndex = m_ctrlList.GetCurSelItem();

    if (nIndex >= 0 && nIndex < m_ctrlList.GetItemCount() - 1)
    {
        // Move the item to the bottom of the list
        m_ctrlList.MoveItemBottom(nIndex);
	}

	UpdateListCtrlBtns();
	m_ctrlList.SetFocus();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -