⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mylistdlg.cpp

📁 这个是我们学校用的VC++教案
💻 CPP
字号:
// MyListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyList.h"
#include "MyListDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyListDlg dialog

CMyListDlg::CMyListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyListDlg)
	m_nIcon = 0;
	m_strName = _T("");
	m_nPopul = 0;
	m_nArea = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyListDlg)
	DDX_Control(pDX, IDC_SPIN, m_Spin);
	DDX_Control(pDX, IDC_LISTBOX, m_ListBx);
	DDX_Control(pDX, IDC_LISTCTL, m_ListCtl);
	DDX_Text(pDX, IDC_EDITICON, m_nIcon);
	DDX_Text(pDX, IDC_EDITNAME, m_strName);
	DDX_Text(pDX, IDC_EDITPOPUL, m_nPopul);
	DDX_Text(pDX, IDC_EDITAREA, m_nArea);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyListDlg, CDialog)
	//{{AFX_MSG_MAP(CMyListDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_LBN_SELCHANGE(IDC_LISTBOX, OnSelchangeListbox)
	ON_BN_CLICKED(IDC_BUTNADD, OnButnadd)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTCTL, OnItemchangedListctl)
	ON_BN_CLICKED(IDC_BUTNDEL, OnButndel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyListDlg message handlers

BOOL CMyListDlg::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

	m_ImgSmall.Create(IDB_SMALL, 16, 1, RGB(255, 255, 255)); // 创建图像列表
	m_ImgLarge.Create(IDB_LARGE, 32, 1, RGB(255, 255, 255));
	m_ListCtl.SetImageList(&m_ImgSmall, LVSIL_SMALL); 	// 列表视图控件与图像列表关联
	m_ListCtl.SetImageList(&m_ImgLarge, LVSIL_NORMAL);
	// 以下设置报表方式每一列的结构
	LVCOLUMN column;
	column.mask=LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	column.fmt=LVCFMT_LEFT;       // 左对齐
	column.cx=70;       // 宽度
	column.iSubItem=0;        // 第1列
	column.pszText="名称";     // 标题
	m_ListCtl.InsertColumn(0, &column);	 // 设置第1列
	column.iSubItem=1;
	column.pszText="人口";
	m_ListCtl.InsertColumn(1, &column);  // 设置第2列
	column.iSubItem=2;
	column.pszText="面积";
	m_ListCtl.InsertColumn(2, &column);  // 设置第3列
	// 以下为列表视图控件添加一些项目	
	LVITEM item;
	item.mask=LVIF_TEXT|LVIF_IMAGE;
	item.iItem=0;			// 第1项
	item.iSubItem=0;        // 主项
	item.pszText="欧洲";
	item.iImage=0;  // 设置关联图像列表中指定图像的索引值
	m_ListCtl.InsertItem(&item);  // 添加第1项
	m_ListCtl.SetItemText(0, 1, "723000000");  // 设置第1项的第2列
	m_ListCtl.SetItemText(0, 2, "10160000");   // 设置第1项的第3列
	item.iItem=1;           // 准备添加第2项
	item.pszText="亚太经合";
	item.iImage=1;
	m_ListCtl.InsertItem(&item);
	m_ListCtl.SetItemText(1, 1, "2200000000");
	m_ListCtl.SetItemText(1, 2, "62300000");
	item.iItem=2;          // 准备添加第3项
	item.pszText="日本";
	item.iImage=2;
	m_ListCtl.InsertItem(&item);
	m_ListCtl.SetItemText(2, 1, "125000000");
	m_ListCtl.SetItemText(2, 2, "377800");
	// 以下进行其它控件的初始化
	m_ListBx.AddString("大图标");    // 为列表框添加选项
	m_ListBx.AddString("小图标");
	m_ListBx.AddString("列表");
	m_ListBx.AddString("报表");
	m_Spin.SetBuddy(GetDlgItem(IDC_EDITICON));  // 动态设置旋转按钮的关联控件
	m_Spin.SetRange(1, 3);   // 设置旋转按钮的最小值和最大值
	m_Spin.SetPos(3);   // 设置旋转按钮的当前位置
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyListDlg::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 CMyListDlg::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 CMyListDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyListDlg::SetListStyle(DWORD dwNewStyle)
{
	DWORD  dwOldStyle; 
	HWND  hWndLt=m_ListCtl.GetSafeHwnd();  // 获得列表视图控件的窗口句柄
	dwOldStyle=GetWindowLong(hWndLt, GWL_STYLE);  // 获得列表视图控件的窗口风格
	if((dwOldStyle & LVS_TYPEMASK)!=dwNewStyle)  // 判断当前的显示方式是不是所指定的方式
	{
		dwOldStyle &=~LVS_TYPEMASK;	  // 从旧的窗口风格中屏蔽不必要的信息
		dwNewStyle |=dwOldStyle;   // 保持窗口其它风格不变,仅改变显示方式
		SetWindowLong(hWndLt, GWL_STYLE, dwNewStyle);  // 改变列表控件窗口风格
	}
}

void CMyListDlg::OnSelchangeListbox()   // 当前选择项发生改变
{
	// TODO: Add your control notification handler code here
	int  nIndex=m_ListBx.GetCurSel( );
	switch (nIndex)
	{
	case  0:
		SetListStyle(LVS_ICON);   // 以大图标方式显示列表视图控件中的项目
		break;
	case  1:
		SetListStyle(LVS_SMALLICON); // 以小图标方式显示列表视图控件中的项目
		break;
	case  2:
		SetListStyle(LVS_LIST);    // 以列表方式显示列表视图控件中的项目
		break;
	case  3:
		SetListStyle(LVS_REPORT);   // 以报表方式显示列表视图控件中的项目
		break;
	}
}

void CMyListDlg::OnItemchangedListctl(NMHDR* pNMHDR, LRESULT* pResult) 
{   // 选择了另一个项目
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	int  nChoice=m_ListCtl.GetNextItem(-1, LVNI_SELECTED);  // 查找当前选择的项目索引
	if(nChoice!=-1)   // 存在所选择的项目
	{
		m_strName=m_ListCtl.GetItemText(nChoice, 0);     // 设置编辑框数据
		CString  strPopul=m_ListCtl.GetItemText(nChoice, 1);
		CString  strArea=m_ListCtl.GetItemText(nChoice, 2);
		m_nPopul=atoi(strPopul);     // 将字符转换为整数
		m_nArea=atoi(strArea);
		m_nIcon=0;
		UpdateData(FALSE);       // 在编辑框显示数据
	}
	
	*pResult = 0;
}

void CMyListDlg::OnButnadd()  //  添加一个项目
{
	// TODO: Add your control notification handler code here
	LVITEM  item;
	UpdateData(TRUE);      // 获取编辑框中的数据
	int  num=m_ListCtl.GetItemCount( );  // 获取列表视图控件的总项数
	item.mask=LVIF_TEXT|LVIF_IMAGE;
	item.iSubItem=0;  // 设置添加的项目
	item.iItem=num;
	char str[20];
	CString  strPopul,strArea;
	strcpy(str, m_strName);	// 把CString类型转换为char*类型
	item.pszText=str;
	item.iImage=m_nIcon-1;
	m_ListCtl.InsertItem(&item);     // 插入项目
	strPopul.Format("%u",m_nPopul);
	m_ListCtl.SetItemText(num, 1, strPopul);  // 设置子项目的文本内容
	strArea.Format("%u",m_nArea);
	m_ListCtl.SetItemText(num, 2, strArea);
}

void CMyListDlg::OnButndel()    // 删除列表视图控件窗口中的当前项
{
	// TODO: Add your control notification handler code here
	int nChoice=m_ListCtl.GetNextItem(-1, LVNI_SELECTED);  // 查找当前选择的项目
	if(nChoice!=-1)
	{
		m_ListCtl.DeleteItem(nChoice);	  // 删除项目。
		m_strName="";
		m_nPopul=0;
		m_nArea=0;
		UpdateData(FALSE);	   // 清空三个文本编辑框。
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -