📄 listctrldemodlg.cpp
字号:
// ListCtrlDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ListCtrlDemo.h"
#include "ListCtrlDemoDlg.h"
#include "about.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CListCtrlDemoDlg dialog
CListCtrlDemoDlg::CListCtrlDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CListCtrlDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CListCtrlDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CListCtrlDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CListCtrlDemoDlg)
DDX_Control(pDX, IDC_LIST1, m_listCtrl);
DDX_Control(pDX, IDC_RBTN_VIEW_LARGE_ICON, m_rbtnLargeIcons);
DDX_Control(pDX, IDC_RBTN_VIEW_SMALL_ICON, m_rbtnSmallIcons);
DDX_Control(pDX, IDC_RBTN_VIEW_LIST, m_rbtnList);
DDX_Control(pDX, IDC_RBTN_VIEW_REPORT, m_rbtnReport);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CListCtrlDemoDlg, CDialog)
//{{AFX_MSG_MAP(CListCtrlDemoDlg)
ON_WM_SYSCOMMAND()
ON_BN_CLICKED(ID_APP_ABOUT, OnAbout)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RBTN_VIEW_LARGE_ICON, OnRbtnViewLargeIcon)
ON_BN_CLICKED(IDC_RBTN_VIEW_LIST, OnRbtnViewList)
ON_BN_CLICKED(IDC_RBTN_VIEW_REPORT, OnRbtnViewReport)
ON_BN_CLICKED(IDC_RBTN_VIEW_SMALL_ICON, OnRbtnViewSmallIcon)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListCtrlDemoDlg message handlers
BOOL CListCtrlDemoDlg::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
// large icon image list
m_imageListLargeIcons.Create(IDB_LARGE_IMAGES, 17, 1, RGB(255,255,255));
m_listCtrl.SetImageList(&m_imageListLargeIcons, LVSIL_NORMAL);
// small icon image list
m_imageListSmallIcons.Create(IDB_SMALL_IMAGES, 17, 1, RGB(255,255,255));
m_listCtrl.SetImageList(&m_imageListSmallIcons, LVSIL_SMALL);
static struct
{
LPTSTR szColText;
UINT uiFormat;
} columns[] = {
_T("Col 1"), LVCFMT_LEFT,
_T("Col 2"), LVCFMT_CENTER,
_T("Col 3"), LVCFMT_RIGHT
};
for (int i = 0; i < sizeof columns / sizeof columns [0]; i++)
{
m_listCtrl.InsertColumn(i, columns[i].szColText, columns[i].uiFormat);
}
m_rbtnReport.SetCheck(TRUE);
OnRbtnViewReport();
CHeaderCtrl* pHeader =
(CHeaderCtrl*)m_listCtrl.GetDlgItem(0);
ASSERT(pHeader);
if (pHeader)
{
// turn off redraw until the columns have all
// been resized
m_listCtrl.SetRedraw(FALSE);
// get the nbr of columns
int iNbrOfColumns = pHeader->GetItemCount();
int iNbrOfCols = GetColumnCount();
for (int iCurrCol = 0;
iCurrCol < iNbrOfCols;
iCurrCol++)
{
m_listCtrl.SetColumnWidth(iCurrCol,
LVSCW_AUTOSIZE);
int nCurrWidth =
m_listCtrl.GetColumnWidth(iCurrCol);
m_listCtrl.SetColumnWidth(iCurrCol,
LVSCW_AUTOSIZE_USEHEADER);
int nColHdrWidth =
m_listCtrl.GetColumnWidth(iCurrCol);
m_listCtrl.SetColumnWidth(iCurrCol,
max(nCurrWidth, nColHdrWidth));
}
// now that col sizing is finished,
// invalidate so that the control is repainted
m_listCtrl.SetRedraw(TRUE);
m_listCtrl.Invalidate();
}
static struct
{
LPTSTR szCol1;
LPTSTR szCol2;
LPTSTR szCol3;
} rows [] = {
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
};
for (int iCurrRow = 0; iCurrRow < sizeof rows / sizeof rows[0]; iCurrRow++)
{
m_listCtrl.InsertItem(iCurrRow, rows[iCurrRow].szCol1, 0);
m_listCtrl.SetItemText(iCurrRow, 1, rows[iCurrRow].szCol2);
m_listCtrl.SetItemText(iCurrRow, 2, rows[iCurrRow].szCol3);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CListCtrlDemoDlg::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 CListCtrlDemoDlg::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();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CListCtrlDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CListCtrlDemoDlg::OnAbout()
{
CAboutDlg().DoModal();
}
void CListCtrlDemoDlg::AddColumn(CString strColumnName, int nFormat /* LVCFMT_LEFT */, int iColumn /* = -1 */)
{
CClientDC dc(this);
if (-1 == iColumn)
{
iColumn = GetColumnCount();
}
CSize size = dc.GetTextExtent(strColumnName);
m_listCtrl.InsertColumn(iColumn, strColumnName, nFormat, size.cx, iColumn);
}
void CListCtrlDemoDlg::ResizeColumns()
{
m_listCtrl.SetRedraw(FALSE);
int iNbrOfCols = GetColumnCount();
for (int iCurrCol = 0; iCurrCol < iNbrOfCols; iCurrCol++)
{
m_listCtrl.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE);
int nCurrWidth = m_listCtrl.GetColumnWidth(iCurrCol);
m_listCtrl.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE_USEHEADER);
int nColHdrWidth = m_listCtrl.GetColumnWidth(iCurrCol);
m_listCtrl.SetColumnWidth(iCurrCol, max(nCurrWidth, nColHdrWidth));
}
m_listCtrl.SetRedraw(TRUE);
m_listCtrl.Invalidate();
}
int CListCtrlDemoDlg::GetColumnCount()
{
int iNbrOfColumns = -1;
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_listCtrl.GetDlgItem(0);
ASSERT(pHeader);
if (pHeader)
{
iNbrOfColumns = pHeader->GetItemCount();
}
return iNbrOfColumns;
}
void CListCtrlDemoDlg::OnRbtnViewLargeIcon()
{
m_listCtrl.SetView(LVS_ICON);
}
void CListCtrlDemoDlg::OnRbtnViewList()
{
m_listCtrl.SetView(LVS_LIST);
}
void CListCtrlDemoDlg::OnRbtnViewReport()
{
m_listCtrl.SetView(LVS_REPORT);
}
void CListCtrlDemoDlg::OnRbtnViewSmallIcon()
{
m_listCtrl.SetView(LVS_SMALLICON);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -