// MFCday003Dlg.h : 头文件 // #pragma once #include "MylistCtrl.h" #include "afxcmn.h" // CMFCday003Dlg 对话框 class CMFCday003Dlg : public CDialogEx { // 构造 public: CMFCday003Dlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_MFCDAY003_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: afx_msg void OnEnChangeEdit1(); afx_msg void OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult); CMylistCtrl m_MylistCtrl; CTreeCtrl m_treeCtrl; CImageList m_ImageList; };
// MFCday003Dlg.cpp : 实现文件 // #include "stdafx.h" #include "MFCday003.h" #include "MFCday003Dlg.h" #include "afxdialogex.h" #include"MylistCtrl.h" #ifdef _DEBUG #define new DEBUG_NEW #endif BOOL CALLBACK EnumProc(HWND hWnd, LPARAM param); // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CMFCday003Dlg 对话框 CMFCday003Dlg::CMFCday003Dlg(CWnd* pParent /*=NULL*/) : CDialogEx(CMFCday003Dlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMFCday003Dlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_LIST, m_MylistCtrl); DDX_Control(pDX, IDC_TREE1, m_treeCtrl); } BEGIN_MESSAGE_MAP(CMFCday003Dlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CMFCday003Dlg::OnLvnItemchangedList1) END_MESSAGE_MAP() // CMFCday003Dlg 消息处理程序 BOOL CMFCday003Dlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 m_MylistCtrl.MyInserteColumn(3, 0, L"姓名", 100, 1, L"籍贯", 100, 2, L"性别", 100); // TODO: 在此添加额外的初始化代码 m_MylistCtrl.MyInserteItem(9, 0, L"小1", 0, 0, L"小2", 1, 0, L"小3", 2, 1, L"", 0, 1, L"小5", 1, 1, L"小6", 2, 2, L"小7", 0, 2, L"小8", 1, 2, L"小9", 2); m_ImageList.Create(32, 32, ILC_COLOR24, 0, 1); EnumWindows(EnumProc, (LPARAM)&m_ImageList); m_treeCtrl.SetImageList(&m_ImageList, LVSIL_NORMAL); TVINSERTSTRUCT tvinfo = { TVI_ROOT, TVI_LAST }; TVITEMEX tvex = {}; tvex.mask = TVIF_IMAGE | TVIF_TEXT; tvex.pszText = TEXT("桌面"); tvex.iImage = 1; tvinfo.itemex = tvex; HTREEITEM hTree = m_treeCtrl.InsertItem(&tvinfo); tvinfo.hParent = hTree; tvex.pszText = TEXT("北京"); HTREEITEM hSubTree = m_treeCtrl.InsertItem(&tvinfo); m_treeCtrl.InsertItem(TEXT("朝阳"), 2,0,hSubTree, TVI_LAST); m_treeCtrl.InsertItem(TEXT("通州"),1 ,0,hSubTree, TVI_LAST); m_treeCtrl.InsertItem(TEXT("天津"), 1,0,hTree, TVI_LAST); m_treeCtrl.InsertItem(TEXT("上海"), 1,0,hTree, TVI_LAST); m_treeCtrl.InsertItem(TEXT("重庆"),1,0, hTree, TVI_LAST); hTree = m_treeCtrl.InsertItem(TEXT("我的电脑"), TVI_ROOT, TVI_LAST); m_treeCtrl.InsertItem(TEXT("纽约"), hTree, TVI_LAST); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } BOOL CALLBACK EnumProc(HWND hWnd, LPARAM param) { CImageList * ImageList = (CImageList *)param; HICON hIcon = (HICON)GetClassLong(hWnd, GCL_HICON); if (hIcon != NULL) { ImageList->Add(hIcon); } return TRUE; } void CMFCday003Dlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CMFCday003Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CMFCday003Dlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CMFCday003Dlg::OnEnChangeEdit1() { // TODO: 如果该控件是 RICHEDIT 控件,它将不 // 发送此通知,除非重写 CDialogEx::OnInitDialog() // 函数并调用 CRichEditCtrl().SetEventMask(), // 同时将 ENM_CHANGE 标志“或”运算到掩码中。 // TODO: 在此添加控件通知处理程序代码 } void CMFCday003Dlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 *pResult = 0; }