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

📄 dbasedlg.cpp

📁 物流综合管理系统
💻 CPP
字号:
// DBaseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyProject.h"
#include "DBaseDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "ExternDllHeader.h"
/////////////////////////////////////////////////////////////////////////////
// CDBaseDlg dialog
bool IsTime(CString sDate);


CDBaseDlg::CDBaseDlg(RxGrid* Grid,CString sIdentify, int nNumberStyle,CWnd* pParent /*=NULL*/)
	: CDialog(CDBaseDlg::IDD, pParent)
{
	m_pGrid=Grid;
	m_Identify=sIdentify;
	m_NumberStyle=nNumberStyle;
	//{{AFX_DATA_INIT(CDBaseDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDBaseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDBaseDlg)
	DDX_Control(pDX, IDC_BUTNEW, m_ButCommand[0]);
	DDX_Control(pDX, IDC_BUTCHANGE, m_ButCommand[1]);
	DDX_Control(pDX, IDC_BUTCOPY, m_ButCommand[2]);
	DDX_Control(pDX, IDC_BUTDELE, m_ButCommand[3]);
	DDX_Control(pDX, IDC_BUTSAVE, m_ButCommand[4]);
	DDX_Control(pDX, IDC_BUTUNDO, m_ButCommand[5]);
	DDX_Control(pDX, IDC_BUTEXIT, m_ButCommand[6]);
	DDX_Control(pDX, IDC_STATITLE, m_StaTitle);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDBaseDlg, CDialog)
	//{{AFX_MSG_MAP(CDBaseDlg)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTEXIT, OnButexit)
	ON_BN_CLICKED(IDC_BUTUNDO, OnButundo)
	ON_BN_CLICKED(IDC_BUTSAVE, OnButsave)
	ON_BN_CLICKED(IDC_BUTNEW, OnButnew)
	ON_BN_CLICKED(IDC_BUTDELE, OnButdele)
	ON_BN_CLICKED(IDC_BUTCOPY, OnButcopy)
	ON_BN_CLICKED(IDC_BUTCHANGE, OnButchange)



	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDBaseDlg message handlers

void CDBaseDlg::OnOK() 
{
	// TODO: Add extra validation here
	//CDialog::OnOK();
}
void CDBaseDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rcClient;
	this->GetClientRect(&rcClient);
	rcClient.top=rcClient.top+8;
	rcClient.bottom=rcClient.bottom-8;
	rcClient.left=rcClient.left+8;
	rcClient.right=rcClient.right-8;
	COLORREF SystemColor=::GetSysColor(COLOR_3DFACE);
	
	CBrush brush,*pbrush;
	brush.CreateSolidBrush(SystemColor);
	pbrush=dc.SelectObject(&brush);
	dc.Rectangle(rcClient);
	dc.SelectObject(pbrush);
	brush.DeleteObject();	
	dc.MoveTo(8,68);
	dc.LineTo(rcClient.Width()+8,68);
}

BOOL CDBaseDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	//根据表格信息创建窗口
	const int nCol=m_pGrid->GetCols();
	m_ColCount=nCol;
	//取出表头
	LVCOLUMN column;
	CString sColCaption;
	column.mask=LVCF_TEXT;
	column.pszText=sColCaption.GetBuffer(sColCaption.GetLength());
	column.cchTextMax=20;

	//根据列数分配在表单上的位置
	//取出屏幕分辨率
	int xPix=::GetSystemMetrics(SM_CXSCREEN);
	int yPix=::GetSystemMetrics(SM_CYSCREEN);
	
	int nWidth,nHeight;
	
	nWidth=xPix/4;
	nHeight=(yPix-200)/(12+5);

	CRect rcSta,rcEdit;
	CString ColCaption;
	//分配空间
	pSta=new CStatic[nCol];
	pEdt=new CBaseEdit[nCol];
	
	rcSta.left=12;
	rcSta.top=70+10;
	rcSta.right=rcSta.left+(nWidth*2/5);
	rcSta.bottom=rcSta.top+nHeight;
	rcEdit.left=rcSta.right;
	rcEdit.right=rcEdit.left+(nWidth*1/2);
	rcEdit.top=rcSta.top;
	rcEdit.bottom=rcSta.bottom;

	if(nCol<=13)
		rcEdit.right=490;
	for(int i=0;i<nCol;i++)
	{	
		//取出表头
		m_pGrid->GetColumn(i,&column);
		ColCaption.Format("%s:",column.pszText);
		//创建标签
		pSta[i].Create(ColCaption,WS_CHILD,rcSta,this,WM_USER+i);
		pSta[i].ShowWindow(SW_SHOW);
		//计算下一个标签位置
		if((i+1)%13==0)
		{
			rcSta.top=70+10;
			rcSta.bottom=rcSta.top+nHeight;
			rcSta.left=rcSta.left+nWidth-5;
			rcSta.right=rcSta.left+(nWidth*1/2);
		}
		else
		{
			rcSta.top=rcSta.bottom+5;
			rcSta.bottom=rcSta.top+nHeight;
		}
		pEdt[i].Create(WS_CHILD|WS_BORDER|WS_TABSTOP|ES_AUTOHSCROLL,rcEdit,this,WM_USER+100+i);
		pEdt[i].ShowWindow(SW_SHOW);
		pEdt[i].SetDlgCtrlID(i);
		pEdt[i].SetReadOnly();		//计算下一个文本框的位置
		if((i+1)%13==0)
		{
			rcEdit.top=70+10;
			rcEdit.bottom=rcEdit.top+nHeight;
			rcEdit.left=rcEdit.left+nWidth-5;
			rcEdit.right=rcEdit.left+(nWidth*1/2);
		}
		else
		{
			rcEdit.top=rcEdit.bottom+5;
			rcEdit.bottom=rcEdit.top+nHeight;
		}
	}
	//重新调整窗口大小
	CRect rcWnd,rcNewWindow;
	rcWnd.top=0;
	rcWnd.left=0;
	rcWnd.bottom=rcEdit.bottom+46;
	rcWnd.right=516;
	
	
	//居中显示
	rcNewWindow.left=xPix/2-rcWnd.Width()/2;
	rcNewWindow.right=rcNewWindow.left+rcWnd.Width();
	rcNewWindow.top=xPix/2-rcWnd.Height()/2-70;
	rcNewWindow.bottom=rcNewWindow.top+rcWnd.Height();
	if(nCol<14)
	{
		this->MoveWindow(rcNewWindow);
		MoveButton(13,rcEdit.top);
	}
	else
	{
		this->ShowWindow(SW_SHOWMAXIMIZED);
		MoveButton(xPix-500,yPix-120);

	}
	
	//设置窗口标题
	CString sCaption;
	this->GetParent()->GetWindowText(sCaption);
	sCaption=sCaption.Left(sCaption.GetLength()-4);
	this->SetWindowText(sCaption+"维护");
	m_DataBaseName=sCaption+"表";
	//设置字符某些只允许输入数字
	RxRecordset rst;
	rst.Open(m_DataBaseName);
	for(i=0;i<rst.GetFieldCount();i++)
	{
		if(rst.GetFieldType(i)=="数值型")
			pEdt[i].IsMoneyOnly(true);
		if(rst.GetFieldType(i)=="逻辑型")
			pEdt[i].IsBoolOnly(true);
	}
	this->Invalidate();
	this->GetClientRect(&rcNewWindow);
	//调整标题区域大小
	this->m_StaTitle.MoveWindow(16,16,rcNewWindow.right-32,38,true);
	this->m_StaTitle.SetWindowText(sCaption+"维护");
	Display();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CDBaseDlg::OnButnew() 
{
	Clear();
	OnButcopy();
}

void CDBaseDlg::OnButchange() 
{
	m_IsAdd=false;
	Enabled(true);	
}
void CDBaseDlg::OnButdele() 
{
	if(MessageBox("确定要删除这条记录吗?","系统提示",MB_OKCANCEL|MB_ICONQUESTION)!=1)
		return;
	CString sSQL,sID,sValue;
	pSta[0].GetWindowText(sID);
	sID=sID.Left(sID.GetLength()-1);
	pEdt[0].GetWindowText(sValue);
	RxRecordset rst;
	rst.Open(m_DataBaseName);
	CString sType=rst.GetFieldType(0);
	if(sType="字符型")
		sSQL.Format("DELETE FROM %s WHERE %s='%s'",m_DataBaseName,sID,sValue);
	else
		sSQL.Format("DELETE FROM %s WHERE %s=%s",m_DataBaseName,sID,sValue);
	rst.Open(sSQL,adCmdText);
	this->OnCancel();
}

void CDBaseDlg::OnButcopy() 
{
	CString sSmallCaption,sCaption;
	m_IsAdd=true;
	Enabled(true);
	CString NewNumber;
	RxRecordset rst;
	sCaption=rst.GetFieldName(0);
	rst.Open(m_DataBaseName);
	CString sType=rst.GetFieldType(0);
	
	if(sType=="字符型")
	{
		sSmallCaption=CharToLetterCode(m_DataBaseName);
		sSmallCaption=sSmallCaption.Left(2);
		NewNumber=ado.AutoNumber(m_DataBaseName,m_Identify,sSmallCaption,m_NumberStyle);
	}
	else
		NewNumber=ado.AutoNumber(m_DataBaseName,m_Identify,"",m_NumberStyle);
	pEdt[0].SetWindowText(NewNumber);
	pEdt[1].SetFocus();
}
void CDBaseDlg::OnButundo() 
{
	if(MessageBox("确定要撤销操作吗?","系统提示",MB_OKCANCEL|MB_ICONQUESTION)!=1)
		return;
	Clear();
	Display();
	Enabled(false);
}
void CDBaseDlg::OnButsave() 
{
	if(MessageBox("确定要保存记录吗?","系统提示",MB_OKCANCEL|MB_ICONQUESTION)!=1)
		return;
	RxRecordset rst;
	rst.Open(m_DataBaseName);
	//将文本存入数组
	CString sValue[50],sCaption[50],sType[50];
	for(int i=0;i<m_ColCount;i++)
	{
		sType[i]=rst.GetFieldType(i);
		pEdt[i].GetWindowText(sValue[i]);
		pSta[i].GetWindowText(sCaption[i]);
		sCaption[i]=sCaption[i].Left(sCaption[i].GetLength()-1);
		if(sValue[i].IsEmpty()==true && rst.IsNull(i)==true)
		{
			MessageBox("『"+sCaption[i]+"』 字段不允许为空!","系统提示",MB_OK|MB_ICONSTOP);
			pEdt[i].SetFocus();
			return;
		}
		if(rst.GetFieldType(i)=="日期型")
		{
			if(IsTime(sValue[i])==false)
			{
				MessageBox("请输入正确的日期格式,如“1983-06-16”、“83/6/16”等","系统提示",MB_OK|MB_ICONSTOP);
				pEdt[i].SetFocus();
				return;
			}
		}
	}

	//组成新字符串

	CString sValueString,str,sFiledString;
	if(m_IsAdd==true)
	{
		sValueString="";
		sFiledString="";
		for(i=0;i<m_ColCount;i++)
		{
			str="";
			str=sValueString;
			if(sType[i]=="数值型")
				sValueString.Format("%s,%s",str,sValue[i]);
			else
				sValueString.Format("%s,'%s'",str,sValue[i]);
			str="";
			str=sFiledString;
			sFiledString.Format("%s,%s",str,sCaption[i]);	
		}
	}
	else
	{
		sValueString="";
		for(i=1;i<m_ColCount;i++)
		{
			str="";
			str=sValueString;
			if(sType[i]=="数值型")
				sValueString.Format("%s,%s=%s",str,sCaption[i],sValue[i]);
			else
				sValueString.Format("%s,%s='%s'",str,sCaption[i],sValue[i]);
		}
	}

	sFiledString=sFiledString.Right(sFiledString.GetLength()-1);
	CString sSQL;
	if(m_IsAdd==true)
		sSQL.Format("INSERT INTO %s (%s) VALUES(%s)",m_DataBaseName,sFiledString,sValueString.Right(sValueString.GetLength()-1));
	else
	{
		if(sType[0]=="数值型")
			sSQL.Format("UPDATE %s SET %s WHERE %s=%s",m_DataBaseName,sValueString.Right(sValueString.GetLength()-1),sCaption[0],sValue[0]);
		else
			sSQL.Format("UPDATE %s SET %s WHERE %s='%s'",m_DataBaseName,sValueString.Right(sValueString.GetLength()-1),sCaption[0],sValue[0]);
	}
	if(rst.Open(sSQL,adCmdText)==false)
	{
		MessageBox("数据保存失败!","系统提示",MB_OK|MB_ICONSTOP);
		pEdt[1].SetFocus();
		return;
	}
	this->Enabled(false);
	this->m_ButCommand[1].SetFocus();
}
void CDBaseDlg::OnButexit() 
{
	this->OnCancel();
}

void CDBaseDlg::MoveButton(int x, int y)
{
	CRect rcNew,rcOld,rcMem;
	m_ButCommand[0].GetClientRect(rcOld);
	rcNew.left=x;
	rcNew.top=y;
	rcNew.right=rcNew.left+rcOld.Width()+10;
	rcNew.bottom=rcNew.top+rcOld.Height()+5;
	rcMem=rcNew;
	for(int m=0;m<7;m++)
	{
		m_ButCommand[m].MoveWindow(&rcNew,true);
		rcNew.left=rcMem.right+3;
		rcNew.right=rcNew.left+rcMem.Width();
		rcMem=rcNew;
	}
}

void CDBaseDlg::Enabled(bool bEnabled)
{
	for(int i=1;i<m_ColCount;i++)
		pEdt[i].SetReadOnly(!bEnabled);
	m_ButCommand[0].EnableWindow(!bEnabled);
	m_ButCommand[1].EnableWindow(!bEnabled);
	m_ButCommand[2].EnableWindow(!bEnabled);
	m_ButCommand[3].EnableWindow(!bEnabled);
	m_ButCommand[4].EnableWindow(bEnabled);
	m_ButCommand[5].EnableWindow(bEnabled);
	m_ButCommand[6].EnableWindow(!bEnabled);
}

void CDBaseDlg::Clear()
{
	for(int m=0;m<m_ColCount;m++)
	{
		pEdt[m].SetWindowText("");
	}
}

void CDBaseDlg::Display()
{
	CString sItemValue;
	int nSelectMark=m_pGrid->GetSelectionMark();
	if(nSelectMark==-1)
	{
		m_ButCommand[4].EnableWindow(true);	
		return;
	}
	for(int i=0;i<m_ColCount;i++)
	{
		sItemValue=m_pGrid->GetItemText(nSelectMark,i);
		pEdt[i].SetWindowText(sItemValue);
	}
}	
void CDBaseDlg::OnCancel() 
{
	if(m_ButCommand[0].IsWindowEnabled()==false)
		if(MessageBox("真的要不保存就退出吗?","系统提示",MB_OKCANCEL|MB_ICONQUESTION)!=1)
			return;
	::DeleteObject(pSta);
	::DeleteObject(pEdt);
	CDialog::OnCancel();

}



bool IsTime(CString sDate)
{
	int nYear,nMonth,nDay,iFChar,iSChar;
	CString sYear,sMonth,sDay;
	//取出“-”的位置
	iFChar=sDate.Find("-",0);
	if(iFChar==-1)
	{
		iFChar=sDate.Find("/",0);
		if(iFChar==-1)
			return false;
		else
		{
			iSChar=sDate.Find("/",iFChar+1);
			if(iSChar==-1)
				return false;
		}
	}
	else
	{
		iSChar=sDate.Find("-",iFChar+1);
		if(iSChar==-1)
			return false;
	}

	sYear=sDate.Left(iFChar);
	sMonth=sDate.Mid(iFChar+1,iSChar-(iFChar+1));
	sDay=sDate.Mid(iSChar+1);	
	if(sYear.GetLength()>4 || sMonth.GetLength()>2 || sDay.GetLength()>2)
		return false;
	nYear=atoi(sYear);
	nMonth=atoi(sMonth);
	nDay=atoi(sDay);
	if(nYear==0||nMonth==0||nDay==0|| nMonth>12 || nDay>31)
		return false;
	return true;
}
int Autoplace(CString sStoreID,bool IsRow)
{
	RxRecordset aRst;
	CString sSQL,sRow,sNum;
	sSQL.Format("SELECT * FROM 仓库信息表 WHERE 编号=%s",sStoreID);
	aRst.Open(sSQL,adCmdText);
	if(aRst.GetRecordCount()<1)
		return 0;
	else	//取出仓库的排数和号数
	{
		sRow=aRst.GetFieldValue("仓库排数");
		sNum=aRst.GetFieldValue("每排号数");
	}
	int nNum=1,nRow=1;
	while(true)
	{	
		if(nNum==atoi(sNum))
		{
			nRow++;
			nNum=1;
		}
		sSQL.Format("SELECT * FROM 库存信息表 WHERE 货位_排=%d AND 货位_号=%d  AND 仓库编号=%s",nRow,nNum,sStoreID);
		aRst.Open(sSQL,adCmdText);
		if(aRst.GetRecordCount()<1)
			break;
		nNum++;
	} 
	if(IsRow==true)
		return nRow;
	else
		return nNum;	
}

⌨️ 快捷键说明

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