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

📄 exmplistdlg.cpp

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

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

/////////////////////////////////////////////////////////////////////////////
// CExmpListDlg dialog

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

void CExmpListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExmpListDlg)
	DDX_Control(pDX, IDC_LISTSTATE, m_ListBx);
	DDX_Text(pDX, IDC_EDITNAME, m_strName);
	DDX_Text(pDX, IDC_EDITCAPITAL, m_strCapital);
	DDX_Text(pDX, IDC_EDITAREA, m_nArea);
	DDV_MinMaxDWord(pDX, m_nArea, 0, 20000000);
	DDX_Text(pDX, IDC_EDITPOPULAT, m_nPopulat);
	DDV_MinMaxDWord(pDX, m_nPopulat, 0, 2000000000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CExmpListDlg, CDialog)
	//{{AFX_MSG_MAP(CExmpListDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTNADD, OnButnAdd)
	ON_BN_CLICKED(IDC_BUTNDEL, OnButnDel)
	ON_LBN_SELCHANGE(IDC_LISTSTATE, OnSelchangeListState)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExmpListDlg message handlers

BOOL CExmpListDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CExmpListDlg::OnButnAdd()    //  添加列表项
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);    // 获得控件中的数据
	if (m_strName.IsEmpty( ))  //   判断“国家”名称是否为空
	{
		MessageBox("国家名称不能为空!");
		return;
	}
	m_strName.TrimLeft( );      // 去掉m_strName左边的空格
	m_strName.TrimRight( );     // 去掉m_strName右边的空格
	if ((m_ListBx.FindStringExact(-1, m_strName))!=LB_ERR)
	{
		MessageBox("列表框中已有该项,不能再添加!");
		return;
	}
	int  nIndex=m_ListBx.AddString(m_strName);  // 向列表框添加国家名
	SState	stState;             // 将国家数据项与新增的列表项关联起来
	stState.strName=m_strName;
	stState.strCapital=m_strCapital;
	stState.nPopulat=m_nPopulat;
	stState.nArea=m_nArea;
	m_ListBx.SetItemDataPtr(nIndex, new SState(stState));   // 建立关联	
}

void CExmpListDlg::OnButnDel()   // 删除列表项
{
	// TODO: Add your control notification handler code here
	int  nIndex=m_ListBx.GetCurSel( );     // 获得当前列表项的索引
	if (nIndex!=LB_ERR)
	{		
		delete (SState*)m_ListBx.GetItemDataPtr(nIndex);  // 释放关联数据所占的内存空间
		m_ListBx.DeleteString(nIndex);     // 删除列表框当前选项
		m_strName=m_strCapital="";      // 设置编辑框控件数据
    	m_nPopulat=m_nArea=0;
    	UpdateData(FALSE);     // 在编辑框中显示数据
 	}
	else
    	MessageBox("没有选择列表项或列表框操作失败!");	
}

void CExmpListDlg::OnSelchangeListState()    // 当前选择项发生改变
{
	// TODO: Add your control notification handler code here
	int  nIndex=m_ListBx.GetCurSel( );
	if (nIndex!=LB_ERR)
	{
		SState  *pstSta=(SState*)m_ListBx.GetItemDataPtr(nIndex);  // 获得关联数据
     	m_strName=pstSta->strName;          // 设置控件成员变量
		m_strCapital=pstSta->strCapital;
		m_nPopulat=pstSta->nPopulat;
		m_nArea=pstSta->nArea;
       	UpdateData(FALSE);        // 显示数据
 	}	
}

void CExmpListDlg::OnDestroy()   // 关闭对话框
{
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
	for (int  nIndex=m_ListBx.GetCount( )-1;  nIndex>=0;  nIndex--)
	{
		// 删除所有与列表项相关联的数据,释放内存
      	delete (SState*)m_ListBx.GetItemDataPtr(nIndex);
	}		
}

⌨️ 快捷键说明

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