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

📄 products.cpp

📁 一个很好的VC源代码
💻 CPP
字号:
// Product.cpp : implementation file
//

#include "stdafx.h"
#include "aoManage.h"
#include "ProductS.h"
#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CProductS dialog


CProductS::CProductS(CWnd* pParent /*=NULL*/)
	: CParentDlg(CProductS::IDD, pParent)
{
	//{{AFX_DATA_INIT(CProductS)
	m_nRadio = 0;
	m_strSerial = _T("");
	m_strPName = _T("");
	m_strPCode = _T("");
	m_strUnit = _T("");
	m_strDemo = _T("");
	//}}AFX_DATA_INIT
	m_pProduct=new CDaoRecordset(&((CMainFrame*)AfxGetMainWnd())->m_db);
	m_bAlter=FALSE;
}


void CProductS::DoDataExchange(CDataExchange* pDX)
{
	CParentDlg::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProductS)
	DDX_Radio(pDX, IDC_RADIO1, m_nRadio);
	DDX_Text(pDX, IDC_EDIT1, m_strSerial);
	DDX_Text(pDX, IDC_EDIT2, m_strPName);
	DDX_Text(pDX, IDC_EDIT7, m_strPCode);
	DDX_Text(pDX, IDC_EDIT8, m_strUnit);
	DDX_Text(pDX, IDC_EDIT9, m_strDemo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CProductS, CParentDlg)
	//{{AFX_MSG_MAP(CProductS)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_WM_CLOSE()
	ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProductS message handlers

BOOL CProductS::OnInitDialog() 
{
	CParentDlg::OnInitDialog();
	
	m_pProduct->Open(AFX_DAO_USE_DEFAULT_TYPE,"select * from product");
	if (m_strPCode.Left(1)=="3")
		m_nRadio=1;
	m_pProduct->FindFirst("pcode='"+m_strPCode+"'");
	COleVariant vVal;
	m_pProduct->GetFieldValue("pname",vVal);
	m_strPName=(LPCTSTR)vVal.bstrVal;
	m_pProduct->GetFieldValue("serial",vVal);
	m_strSerial=(LPCTSTR)vVal.bstrVal;
	m_pProduct->GetFieldValue("unit",vVal);
	m_strUnit=(LPCTSTR)vVal.bstrVal;
	m_pProduct->GetFieldValue("demo",vVal);
	m_strDemo=(LPCTSTR)vVal.bstrVal;
	m_strCodeOld=m_strPCode;
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CProductS::OnButton3() 
{
	if (MessageBox("你确实要修改吗?",NULL,MB_OKCANCEL)==IDCANCEL)
		return;
	UpdateData(TRUE);
	if (!m_strSerial.GetLength()||!m_strPCode.GetLength()||!m_strPName.GetLength())
	{
		MessageBox("序号,品号或品名不能为空,请重新输入");
		return;
	}
	if (strcmp(m_strPCode,m_strCodeOld))
	{
		if (m_pProduct->FindFirst("pcode='"+m_strPCode+"'"))
		{
			MessageBox("此品号已存在,请重新选择");
			return;
		}
		else
		{
			m_pProduct->FindFirst("pcode='"+m_strCodeOld+"'");
			m_bAlter=TRUE;
		}
	}
	m_pProduct->Edit();
	m_pProduct->SetFieldValue("serial",(LPCTSTR)m_strSerial);
	m_pProduct->SetFieldValue("pname",(LPCTSTR)m_strPName);
	m_pProduct->SetFieldValue("pcode",(LPCTSTR)m_strPCode);
	m_pProduct->SetFieldValue("unit",(LPCTSTR)m_strUnit);
	m_pProduct->SetFieldValue("demo",(LPCTSTR)m_strDemo);
	m_pProduct->Update();
	MessageBox("记录已成功修改");
	if (m_bAlter)
	{
		CDaoDatabase *pDb=&((CMainFrame*)AfxGetMainWnd())->m_db;
		pDb->Execute("select * into "+m_strPCode+" from "+m_strCodeOld);
		pDb->Execute("drop table "+m_strCodeOld);
		//pDb->Execute("create table "+m_strPCode+" (pcode char (9))");
		m_bAlter=FALSE;
	}
	OnOK();
}

void CProductS::OnButton4() 
{
	OnCancel();
}

void CProductS::OnClose() 
{
	if (m_pProduct->IsOpen())
		m_pProduct->Close();
	CParentDlg::OnClose();
}

void CProductS::OnKillfocusEdit1() 
{
	UpdateData(TRUE);
	if (!m_nRadio)
		m_strPCode="2"+m_strSerial+"0000";
	else
		m_strPCode="3"+m_strSerial+"0000";
	UpdateData(FALSE);
}

void CProductS::OnRadio1() 
{
	UpdateData(TRUE);
	FILE *f=fopen("data\\serial.txt","r");
	char *lpBuf=new char[5];
	while (fgetc(f)!=':');
	while (fgetc(f)!=':');
	fread(lpBuf,4,1,f);
	lpBuf[4]='\0';
	fclose(f);
	m_strSerial=lpBuf;
	UpdateData(FALSE);
}

void CProductS::OnRadio2() 
{
	UpdateData(TRUE);
	FILE *f=fopen("data\\serial.txt","r");
	char *lpBuf=new char[5];
	while (fgetc(f)!=':');
	while (fgetc(f)!=':');
	while (fgetc(f)!=':');
	fread(lpBuf,4,1,f);
	lpBuf[4]='\0';
	fclose(f);
	m_strSerial=lpBuf;
	UpdateData(FALSE);
}

void CProductS::OnButton8() 
{
	if (MessageBox("确实要删除吗?",NULL,MB_OKCANCEL)==IDOK)
	{
		m_pProduct->Delete();
		MessageBox("记录已成功删除");
		CDaoDatabase *pDb=&((CMainFrame*)AfxGetMainWnd())->m_db;
		pDb->Execute("delete from "+m_strPCode);
		OnOK();
	}		
}

⌨️ 快捷键说明

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