📄 lhdrdlg.cpp
字号:
// Exam1_1_11Dlg.cpp : implementation file
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "Exam1_1_11.h"
#include "LHdrDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CExam1_1_11Dlg dialog
CExam1_1_11Dlg::CExam1_1_11Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CExam1_1_11Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CExam1_1_11Dlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pImageHdrSmall = NULL;
m_pImageListSmall = NULL;
}
CExam1_1_11Dlg::~CExam1_1_11Dlg()
{
if( m_pImageHdrSmall != NULL)
delete m_pImageHdrSmall;
if( m_pImageListSmall != NULL)
delete m_pImageListSmall;
}
void CExam1_1_11Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CExam1_1_11Dlg)
DDX_Control(pDX, IDC_LISTVIEW1, m_listctrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CExam1_1_11Dlg, CDialog)
//{{AFX_MSG_MAP(CExam1_1_11Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDABOUT, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CExam1_1_11Dlg message handlers
BOOL CExam1_1_11Dlg::OnInitDialog()
{
CExam1_1_11App *pApp;
CRect rect;
CDialog::OnInitDialog();
UpdateData(TRUE);
pApp = (CExam1_1_11App *)AfxGetApp();
srand((unsigned) time(NULL)); // 产生随机数
// 为列表头创建图像列表
m_pImageHdrSmall = new CImageList();
ASSERT(m_pImageHdrSmall != NULL);
m_pImageHdrSmall->Create(16, 16, ILC_MASK, 2, 2);
m_pImageHdrSmall->Add(pApp->LoadIcon(IDI_HDRICON1));
m_pImageHdrSmall->Add(pApp->LoadIcon(IDI_HDRICON2));
// 为列表控件创建图像列表
m_pImageListSmall = new CImageList();
ASSERT(m_pImageListSmall != NULL); // serious allocation failure checking
m_pImageListSmall->Create(16, 16, TRUE, 4, 4);
m_pImageListSmall->Add(pApp->LoadIcon(IDI_ICONLIST1));
m_pImageListSmall->Add(pApp->LoadIcon(IDI_ICONLIST2));
m_pImageListSmall->Add(pApp->LoadIcon(IDI_ICONLIST3));
m_pImageListSmall->Add(pApp->LoadIcon(IDI_ICONLIST4));
FillListCtrl(); //初始化列表控件和表头
return FALSE;
}
// 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 CExam1_1_11Dlg::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();
}
}
HCURSOR CExam1_1_11Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CExam1_1_11Dlg::FillListCtrl()
{
CRect rect;
int iIcon, iItem, iSubItem, iActualItem;
LV_ITEM lvitem;
CString strItem1= _T("ITEM");
CString strItem2= _T("SUB_ITEM");
CString strIconDesc[4], strIconShortDesc[4];
LPTSTR pStrTemp1, pStrTemp2;
CExam1_1_11App *pApp;
strIconDesc[0]= _T("Blue Ellipse, Yellow Triangle, Red Rectangle");
strIconDesc[1]= _T("Yellow Ellipse, Red Triangle, Blue Rectangle");
strIconDesc[2]= _T("Red Ellipse, Blue Triangle, Yellow Rectangle");
strIconDesc[3]= _T("Red Ellipse, Yellow Triangle, Blue Rectangle");
strIconShortDesc[0]= _T("BE, YT, RR (Title)");
strIconShortDesc[1]= _T("YE, RT, BR (Title)");
strIconShortDesc[2]= _T("RE, BT, YR (Title)");
strIconShortDesc[3]= _T("RE, YT, BR (Title)");
pApp = (CExam1_1_11App *)AfxGetApp();
m_listctrl.SetImageList(m_pImageListSmall, LVSIL_SMALL);
// 给列表框插入两列,并且让表头显示图像
m_listctrl.GetWindowRect(&rect);
m_listctrl.InsertColumn(0, strItem1, LVCFMT_LEFT,
rect.Width() * 1/3, 0);
m_listctrl.InsertColumn(1, strItem2, LVCFMT_LEFT,
rect.Width() * 2/3, 1);
ModifyHeaderItems();
for (iItem = 0; iItem < 20; iItem++) // 在视图中插入子项
for (iSubItem = 0; iSubItem < 2; iSubItem++)
{
if (iSubItem == 0)
iIcon = rand() % 4; // 随机选择图标
lvitem.mask = LVIF_TEXT | (iSubItem == 0? LVIF_IMAGE : 0);
lvitem.iItem = (iSubItem == 0)? iItem : iActualItem;
lvitem.iSubItem = iSubItem;
// 计算表项文本长度
pStrTemp1= strIconShortDesc[iIcon].GetBuffer(strIconShortDesc[iIcon].GetLength());
pStrTemp2= strIconDesc[iIcon].GetBuffer(strIconDesc[iIcon].GetLength());
lvitem.pszText = iSubItem == 0? pStrTemp1 : pStrTemp2;
lvitem.iImage = iIcon;
if (iSubItem == 0)
iActualItem = m_listctrl.InsertItem(&lvitem); // 插入新表项
else
m_listctrl.SetItem(&lvitem); // 修改已存在的表项
}
}
void CExam1_1_11Dlg::ModifyHeaderItems()
{
HD_ITEM curItem;
// 得到列表头控件的指针
CHeaderCtrl* pHdrCtrl= NULL;
pHdrCtrl= m_listctrl.GetHeaderCtrl();
pHdrCtrl->SetImageList(m_pImageHdrSmall);
// 给每个表头添加图像
pHdrCtrl->GetItem(0, &curItem);
curItem.mask= HDI_IMAGE | HDI_FORMAT;
curItem.iImage= 0;
curItem.fmt= HDF_LEFT | HDF_IMAGE | HDF_STRING;
pHdrCtrl->SetItem(0, &curItem);
pHdrCtrl->GetItem(1, &curItem);
curItem.mask= HDI_IMAGE | HDI_FORMAT;
curItem.iImage= 1;
curItem.fmt= HDF_LEFT | HDF_IMAGE | HDF_STRING;
pHdrCtrl->SetItem(1, &curItem);
}
// this function is used when a requested style
// LVS_NOLABELWRAP, LVS_NOCOLUMNHEADER, and LVS_NOSORTHEADER
// forces the current control to be re-created with the new style.
// this function is used when a requested style
// can be applied to the existing control, and demonstrated.
void CExam1_1_11Dlg::ChangeListCtrlStyle(DWORD dwStyle, BOOL bSetBits)
{
CRect rect;
m_listctrl.GetWindowRect(&rect);
ScreenToClient(&rect);
ASSERT(dwStyle != 0); // watch out for LVS_foo DEFINITIONS which are 0.
if (bSetBits)
m_listctrl.ModifyStyle(0, dwStyle);
else
m_listctrl.ModifyStyle(dwStyle, 0);
InvalidateRect(rect);
UpdateData(FALSE); // send information back to the dialog
}
void CExam1_1_11Dlg::OnAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -