www.pudn.com > tabctr.rar > Exam10_25Dlg.cpp
// Exam10_25Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Exam10_25.h"
#include "Exam10_25Dlg.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()
/////////////////////////////////////////////////////////////////////////////
// CExam10_25Dlg dialog
CExam10_25Dlg::CExam10_25Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CExam10_25Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CExam10_25Dlg)
// 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 CExam10_25Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CExam10_25Dlg)
DDX_Control(pDX, IDC_LIST_CTRL_MODE, m_TabListMode);
DDX_Control(pDX, IDC_LIST_CTRL, m_ListCtrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CExam10_25Dlg, CDialog)
//{{AFX_MSG_MAP(CExam10_25Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(TCN_SELCHANGE, IDC_LIST_CTRL_MODE, OnSelchangeListCtrlMode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CExam10_25Dlg message handlers
BOOL CExam10_25Dlg::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
// TODO: Add extra initialization here
DWORD dwStyle = m_ListCtrl.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;
m_ListCtrl.SetExtendedStyle(dwStyle);
// initialize the standard and custom hot cursors
// m_hCustomHotCursor= AfxGetApp()->LoadCursor(IDC_HOTCURSOR);
// m_hHotCursor= m_cListCtrl.GetHotCursor();
// m_bHotCursor = FALSE;
// Setup the tab header
InitTabCtrl();
// Setup the column headings
InitListCtrlCols();
// Create the image list that is attached to the list control
InitImageList();
// Insert the default dummy items
InsertItems();
return TRUE; // return TRUE unless you set the focus to a control
}
void CExam10_25Dlg::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 CExam10_25Dlg::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 CExam10_25Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CExam10_25Dlg::InitImageList()
{
HIMAGELIST hList = ImageList_Create(32,32, ILC_COLOR8 |ILC_MASK , 8, 1);
m_cImageListNormal.Attach(hList);
hList = ImageList_Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 1);
m_cImageListSmall.Attach(hList);
// Load the large icons
CBitmap cBmp;
cBmp.LoadBitmap(IDB_IMAGES_NORMAL);
m_cImageListNormal.Add(&cBmp, RGB(255,0, 255));
cBmp.DeleteObject();
// Load the small icons
cBmp.LoadBitmap(IDB_IMAGES_SMALL);
m_cImageListSmall.Add(&cBmp, RGB(255,0, 255));
// Attach them
m_ListCtrl.SetImageList(&m_cImageListNormal, LVSIL_NORMAL);
m_ListCtrl.SetImageList(&m_cImageListSmall, LVSIL_SMALL);
}
void CExam10_25Dlg::InitListCtrlCols()
{
CRect rect;
m_ListCtrl.GetClientRect(&rect);
int nColInterval = rect.Width()/5;
m_ListCtrl.InsertColumn(0, _T("Item Name"), LVCFMT_LEFT, nColInterval*3);
m_ListCtrl.InsertColumn(1, _T("Value"), LVCFMT_LEFT, nColInterval);
m_ListCtrl.InsertColumn(2, _T("Time"), LVCFMT_LEFT, rect.Width()-4*nColInterval);
}
void CExam10_25Dlg::InsertItems()
{
// Delete the current contents
m_ListCtrl.DeleteAllItems();
// Use the LV_ITEM structure to insert the items
LVITEM lvi;
CString strItem;
for (int i = 0; i < 100; i++)
{
// Insert the first item
lvi.mask = LVIF_IMAGE | LVIF_TEXT;
strItem.Format(_T("Item %i"), i);
lvi.iItem = i;
lvi.iSubItem = 0;
lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
lvi.iImage = i%8; // There are 8 images in the image list
m_ListCtrl.InsertItem(&lvi);
// Set subitem 1
strItem.Format(_T("%d"), 10*i);
lvi.iSubItem =1;
lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
m_ListCtrl.SetItem(&lvi);
// Set subitem 2
// strItem.Format(_T("%s"), COleDateTime::GetCurrentTime().Format(_T("Created: %I:%M:%S %p, %m/%d/%Y")));
strItem.Format(_T("hehe"));
lvi.iSubItem =2;
lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
m_ListCtrl.SetItem(&lvi);
}
}
void CExam10_25Dlg::InitTabCtrl()
{
m_TabListMode.InsertItem(0, _T("Icon"));
m_TabListMode.InsertItem(1, _T("Small Icon"));
m_TabListMode.InsertItem(2, _T("List"));
m_TabListMode.InsertItem(3, _T("Details"));
}
void CExam10_25Dlg::OnSelchangeListCtrlMode(NMHDR* pNMHDR, LRESULT* pResult)
{
// Get the current data and don't redraw while we decide what to do.
UpdateData(TRUE);
m_ListCtrl.SetRedraw(FALSE);
// Remove whatever style is there currently
m_ListCtrl.ModifyStyle(LVS_ICON | LVS_LIST | LVS_REPORT | LVS_SMALLICON ,0);
// Switch the tab mode
switch (m_TabListMode.GetCurSel())
{
case 0: // Icon mode
m_ListCtrl.ModifyStyle(0, LVS_ICON);
break;
case 1: // Small icons
m_ListCtrl.ModifyStyle(0, LVS_SMALLICON);
break;
case 2: // List mode
m_ListCtrl.ModifyStyle(0, LVS_LIST);
break;
case 3: // Details mode
m_ListCtrl.ModifyStyle(0, LVS_REPORT);
break;
}
// Fill the list control
InsertItems();
m_ListCtrl.SetRedraw(TRUE);
*pResult = 0;
}