📄 customcollectiondemodlg.cpp
字号:
// CustomCollectionDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CustomCollectionDemo.h"
#include "CustomCollectionDemoDlg.h"
#include ".\customcollectiondemodlg.h"
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CCustomCollectionDemoDlg dialog
void SizeAllColumns(CListCtrl& lstCtrl)
{
CHeaderCtrl* pHeader = lstCtrl.GetHeaderCtrl();
ASSERT(pHeader);
if (pHeader)
{
// Turn off redraw until the columns have all
// been resized
lstCtrl.SetRedraw(FALSE);
for (int iCurrCol = 0;
iCurrCol < pHeader->GetItemCount();
iCurrCol++)
{
lstCtrl.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE);
int nCurrWidth = lstCtrl.GetColumnWidth(iCurrCol);
lstCtrl.SetColumnWidth(iCurrCol,
LVSCW_AUTOSIZE_USEHEADER);
int nColHdrWidth = lstCtrl.GetColumnWidth(iCurrCol);
lstCtrl.SetColumnWidth(iCurrCol,
max(nCurrWidth, nColHdrWidth));
}
// Now that sizing is finished, turn redraw back on and
// invalidate so that the control is repainted
lstCtrl.SetRedraw(TRUE);
lstCtrl.Invalidate();
}
}
CCustomCollectionDemoDlg::CCustomCollectionDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCustomCollectionDemoDlg::IDD, pParent)
, m_strFullName(_T(""))
, m_iAnnualSalary(0)
, m_iExperienceInYears(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCustomCollectionDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strFullName);
DDX_Text(pDX, IDC_EDIT2, m_iAnnualSalary);
DDX_Text(pDX, IDC_EDIT3, m_iExperienceInYears);
DDX_Control(pDX, IDC_LIST1, m_lstProgrammers);
}
BEGIN_MESSAGE_MAP(CCustomCollectionDemoDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedAdd)
ON_NOTIFY(HDN_ITEMCLICK, 0, OnHdnItemclickList1)
ON_BN_CLICKED(IDCANCEL2, OnBnClickedCancel2)
END_MESSAGE_MAP()
// CCustomCollectionDemoDlg message handlers
void CCustomCollectionDemoDlg::OnBnClickedCancel2()
{
CAboutDlg().DoModal();
}
BOOL CCustomCollectionDemoDlg::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
// TODO: Add extra initialization here
#pragma push_macro("new")
#undef new
m_collProgrammers = new ProgrammerCollection();
m_lstProgrammers.InsertColumn(0, _T("Full Name"));
m_lstProgrammers.InsertColumn(1, _T("Experience"));
m_lstProgrammers.InsertColumn(2, _T("Salary"));
SizeAllColumns(m_lstProgrammers);
#pragma pop_macro("new")
// dummy data
m_strFullName = _T("Programmer A");
m_iExperienceInYears = 6;
m_iAnnualSalary = 50000;
UpdateData(FALSE);
OnBnClickedAdd();
m_strFullName = _T("Programmer B");
m_iExperienceInYears = 8;
m_iAnnualSalary = 80000;
UpdateData(FALSE);
OnBnClickedAdd();
m_strFullName = _T("Programmer C");
m_iExperienceInYears = 10;
m_iAnnualSalary = 60000;
UpdateData(FALSE);
OnBnClickedAdd();
m_strFullName = _T("");
m_iExperienceInYears = 0;
m_iAnnualSalary = 0;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CCustomCollectionDemoDlg::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 CCustomCollectionDemoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCustomCollectionDemoDlg::RefreshList()
{
m_lstProgrammers.DeleteAllItems();
int index = 0;
IEnumerator* en = m_collProgrammers->GetEnumerator();
while (en->MoveNext())
{
Programmer* programmer = dynamic_cast<Programmer*>(en->Current);
int idx = m_lstProgrammers.InsertItem(m_lstProgrammers.GetItemCount(),
(CString)programmer->FullName);
m_lstProgrammers.SetItemText(idx, 1, (CString)programmer->ExperienceInYears.ToString());
m_lstProgrammers.SetItemText(idx, 2, (CString)programmer->AnnualSalary.ToString());
}
SizeAllColumns(m_lstProgrammers);
}
void CCustomCollectionDemoDlg::OnBnClickedAdd()
{
#pragma push_macro("new")
#undef new
UpdateData(TRUE);
// Create new Programmer object
Programmer* p = new Programmer(m_strFullName,
m_iExperienceInYears,
m_iAnnualSalary);
// Add to collection
m_collProgrammers->Add(p);
// Ask collection where new item was inserted
int i = m_collProgrammers->IndexOf(p);
// Based on item's insertion point, insert
// item into list control at same index
int idx = m_lstProgrammers.InsertItem(i, m_strFullName);
CString strTemp;
strTemp.Format(_T("%ld"), m_iExperienceInYears);
m_lstProgrammers.SetItemText(idx, 1, strTemp);
strTemp.Format(_T("%ld"), m_iAnnualSalary);
m_lstProgrammers.SetItemText(idx, 2, strTemp);
SizeAllColumns(m_lstProgrammers);
// Init dialog
m_strFullName = _T("");
m_iExperienceInYears = 0;
m_iAnnualSalary = 0;
UpdateData(FALSE);
#pragma pop_macro("new")
}
void CCustomCollectionDemoDlg::OnHdnItemclickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
switch( phdr->iItem )
{
case 0:
m_collProgrammers->Sort(ProgrammerSortOption::ByName);
break;
case 1:
m_collProgrammers->Sort(ProgrammerSortOption::ByExperience);
break;
case 2:
m_collProgrammers->Sort(ProgrammerSortOption::BySalary);
break;
}
RefreshList();
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -