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

📄 ex_cityzonedlg.cpp

📁 Visual C++应用教程-源代码 本书在复习C++基础知识后
💻 CPP
字号:
// Ex_CityZoneDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CEx_CityZoneDlg dialog

CEx_CityZoneDlg::CEx_CityZoneDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEx_CityZoneDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEx_CityZoneDlg)
	m_strCity = _T("");
	m_strZip = _T("");
	m_strZone = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEx_CityZoneDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEx_CityZoneDlg)
	DDX_Control(pDX, IDC_COMBO1, m_ComboBox);
	DDX_CBString(pDX, IDC_COMBO1, m_strCity);
	DDV_MaxChars(pDX, m_strCity, 20);
	DDX_Text(pDX, IDC_EDIT_ZIP, m_strZip);
	DDV_MaxChars(pDX, m_strZip, 6);
	DDX_Text(pDX, IDC_EDIT_ZONE, m_strZone);
	DDV_MaxChars(pDX, m_strZone, 10);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEx_CityZoneDlg, CDialog)
	//{{AFX_MSG_MAP(CEx_CityZoneDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_CHANGE, OnButtonChange)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx_CityZoneDlg message handlers

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

BOOL CEx_CityZoneDlg::IsValidate()
{
	UpdateData();	m_strCity.TrimLeft();
	if (m_strCity.IsEmpty())	{
		MessageBox("城市名输入无效!");		return FALSE;
	}
	m_strZip.TrimLeft();
	if (m_strZip.IsEmpty())	{
		MessageBox("邮政编码输入无效!");		return FALSE;
	}
	m_strZone.TrimLeft();
	if (m_strZone.IsEmpty())	{
		MessageBox("区号输入无效!");			return FALSE;
	}
	return TRUE;
}

void CEx_CityZoneDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	if (!IsValidate()) return;
	int nIndex = m_ComboBox.FindStringExact( -1, m_strCity );
	if (nIndex != CB_ERR )	
	{
		MessageBox("该城市已添加!");		return;
	}
	CString strData;
	strData.Format("%s,%s", m_strZip, m_strZone);	// 将邮政编码和区号合并为一个字符串
	m_ComboBox.SetItemDataPtr( nIndex, new CString(strData) );	
}

void CEx_CityZoneDlg::OnButtonChange() 
{
	// TODO: Add your control notification handler code here
	if (!IsValidate()) return;
	int nIndex = m_ComboBox.FindStringExact( -1, m_strCity );
	if (nIndex != CB_ERR )	{
		delete (CString*)m_ComboBox.GetItemDataPtr( nIndex );
		CString strData;
		strData.Format("%s,%s", m_strZip, m_strZone);
		m_ComboBox.SetItemDataPtr( nIndex, new CString(strData) );	
	}
}

void CEx_CityZoneDlg::OnSelchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	int nIndex = m_ComboBox.GetCurSel();
	if (nIndex != CB_ERR )	{
		m_ComboBox.GetLBText( nIndex, m_strCity );
		CString strData = *(CString*)m_ComboBox.GetItemDataPtr( nIndex );
		// 分解字符串
		int n = strData.Find(',');
		m_strZip = strData.Left( n );		// 前面的n个字符
		m_strZone = strData.Mid( n+1 );		// 从中间第n+1字符到未尾的字符串							UpdateData( FALSE );
	}
}

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

⌨️ 快捷键说明

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