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

📄 shoppingdlg.cpp

📁 此shopping购物程序小巧使用
💻 CPP
字号:
// shoppingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "shopping.h"
#include "shoppingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

 struct commodity
{
	char*	strType;
	char*	strName;
	int		nPrice;
}comm[]={
	 "日常用品","牙刷",2,
	 "日常用品","牙膏" ,3,
	 "日常用品","毛巾",5,
	 "日常用品","脸盆",12,

	"文具","铅笔",1,
	"文具","圆珠笔",2,
	"文具","钢笔",10,
	"文具","水彩笔",12,

	"球类","篮球",66,
	"球类","足球",120,
	"球类","乒乓球",3,
	"球类","羽毛球",15,
};


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()

/////////////////////////////////////////////////////////////////////////////
// CShoppingDlg dialog

CShoppingDlg::CShoppingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CShoppingDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CShoppingDlg)
	m_strType = _T("");
	m_lstrName = _T("");
	m_strSinglePrice = _T("");
	m_strTotalPrice = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CShoppingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShoppingDlg)
	DDX_Control(pDX, IDC_SALE_LIST2, m_lstSelGoods);
	DDX_Control(pDX, IDC_SALE_LIST, m_lstName);
	DDX_Control(pDX, IDC_SALE_TYPE, m_cmbType);
	DDX_CBString(pDX, IDC_SALE_TYPE, m_strType);
	DDX_LBString(pDX, IDC_SALE_LIST, m_lstrName);
	DDX_Text(pDX, IDC_SINGLEPRICE, m_strSinglePrice);
	DDX_Text(pDX, IDC_TOTALPRICE, m_strTotalPrice);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CShoppingDlg, CDialog)
	//{{AFX_MSG_MAP(CShoppingDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SHOP, OnShop)
	ON_LBN_SELCHANGE(IDC_SALE_LIST, OnSelchangeSaleList)
	ON_CBN_SELCHANGE(IDC_SALE_TYPE, OnSelchangeSaleType)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShoppingDlg message handlers

BOOL CShoppingDlg::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_strSinglePrice="单价:";
	m_strTotalPrice="共计:";
	total=0;
	//初始化组合框
	m_cmbType.AddString("日常用品");
	m_cmbType.AddString("文具");
	m_cmbType.AddString("球类");
    m_strType="日常用品";
    //初始化列表框
	for(int i=0;i<12;i++)
	{
		m_lstName.AddString(comm[i].strName);
	}
    //将变量的值传给控件
	UpdateData(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CShoppingDlg::OnShop() 
{
	//将控件的值传给相应的变量
	UpdateData();
	for(int i=0;i<12;i++)
	{   
		//在结构体对象数组中找到用户所选商品的价格
		if(m_lstrName==comm[i].strName)
		{
			//累计总价
			total+=comm[i].nPrice;
			break;
		}
	}	
	// 设置已选择商品
	m_lstSelGoods.AddString(m_lstrName);
	// 计算总价格
	char tbuf[80];
	sprintf(tbuf,"共计:%d元",total);
	m_strTotalPrice=tbuf;
	//显示总价
	UpdateData(FALSE);
	
}

void CShoppingDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CShoppingDlg::OnSelchangeSaleList() 
{
	//将控件的值传给相应的变量
	UpdateData();
	for(int i=0;i<12;i++)
	{   
		//获取用户选择商品的价格
		if(m_lstrName==comm[i].strName)
		{
			char tbuf[80];
			sprintf(tbuf,"单价:%d元",comm[i].nPrice);
			m_strSinglePrice=tbuf;
			//将商品价格显示出来
			UpdateData(FALSE);
			break;
		}
	}
}

void CShoppingDlg::OnSelchangeSaleType() 
{
	// TODO: Add your control notification handler code here
	int	nStart;
	//获取组合框当前选项
	int	nIndex=m_cmbType.GetCurSel();
   m_cmbType.GetLBText(nIndex,m_strType);
	
	if(m_strType=="日常用品")
	{
		nStart=0;
	}
	else if(m_strType=="文具")
	{
		nStart=4;
	}
	else
		nStart=8;
    //清除列表框原有内容
	m_lstName.ResetContent();
	//重置列表框当前内容
	for(int i=nStart;i<nStart+4;i++)
	{
		m_lstName.AddString(comm[i].strName);
	}	
}

⌨️ 快捷键说明

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