📄 medicinesalemanages.cpp
字号:
// MedicineSaleManages.cpp : 定义应用程序的类行为。
//
#include "stdafx.h"
#include "MedicineSaleManages.h"
#include "MedicineSaleManagesDlg.h"
#include ".\medicinesalemanages.h"
#include "MakeRestorDatabase.h"
#include <adojet.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define ChunkSize 1024
// CMedicineSaleManagesApp
BEGIN_MESSAGE_MAP(CMedicineSaleManagesApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
// CMedicineSaleManagesApp 构造
CMedicineSaleManagesApp::CMedicineSaleManagesApp()
: connString(_T(""))
, userlock(FALSE)
, userlogin(FALSE)
, excel_type(_T(""))
, strSQLpublic(_T(""))
, make_sql(FALSE)
, cn_str(_T(""))
, en_str(_T(""))
, win_type(0)
, static_date(_T(""))
, boot(FALSE)
, m_savetime(FALSE)
{
connString=_T("Data Source=fyyysms;UID=sa;PWD=;");
}
// 唯一的一个 CMedicineSaleManagesApp 对象
CMedicineSaleManagesApp theApp;
// CMedicineSaleManagesApp 初始化
BOOL CMedicineSaleManagesApp::InitInstance()
{
::CreateMutex(NULL,TRUE, m_pszAppName);
if(GetLastError()==ERROR_ALREADY_EXISTS) //判断CreateMutex()的错误信息
{
::MessageBox(NULL,"程序已经启动了!","提示",MB_ICONINFORMATION);
return FALSE;
}
InitCommonControls();
CWinApp::InitInstance();
::CoInitialize( NULL );
if (!AfxOleInit())
{
AfxMessageBox("初始化 OLE 库出错!");
return FALSE;
}
if((::GetTickCount()/1000/60 % 60)<1)
::Sleep(60000);
if(!makesqldatabase())
{
make_sql=TRUE;
AfxMessageBox("系统将禁用[更新数据]功能!");
}
if(!makeaccessdatabase())
{
AfxMessageBox("请在恢复数据界面中用[连接数据库]或[还原数据库]进行连接!");
CMakeRestorDatabase database;
database.DoModal();
if(!database.make)
{
AfxMessageBox("连接失败,程序将退出!");
return FALSE;
}
}
boot=TRUE;
this->SetAutoBoot();
if(!make_sql)
{
m_pConn->Close();
m_pConn=NULL;
}
AfxEnableControlContainer();
SetRegistryKey(_T("开阳富源医药有限公司第四门市部(冯三经销点)医药销售管理系统"));
CMedicineSaleManagesDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置处理何时用“确定”来关闭
//对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用“取消”来关闭
//对话框的代码
}
return FALSE;
}
BOOL CMedicineSaleManagesApp::makeaccessdatabase(void)
{
m_pConn1.CreateInstance(__uuidof(Connection));
try
{
CString m_strConnection,strpath;
char filepath[_MAX_PATH];
::GetModuleFileName(NULL,filepath,_MAX_PATH);
strpath.Format("%s",filepath);
CString data=strpath.Left(strpath.ReverseFind('\\')+1)+_T("MedicineSaleManages.mdb");
CString password=_T("a1@4>b?D_i-e=z+h|2<:");
m_strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+data+";Persist Security Info=False;";
m_strConnection=m_strConnection+"Jet OLEDB:Database Password="+password+";";
m_pConn1->Open(_bstr_t((LPCTSTR)m_strConnection),_T(""),_T(""),NULL);
}
catch(_com_error e)
{
AfxMessageBox("连接数据库失败!");
return FALSE;
}
return TRUE;
}
BOOL CMedicineSaleManagesApp::makesqldatabase(void)
{
HRESULT hRes;
try
{
hRes=m_pConn.CreateInstance(_T("ADODB.Connection"));
m_pConn->ConnectionTimeout = 8;
hRes=m_pConn->Open(_bstr_t((LPCTSTR)connString),_T(""),_T(""),adModeUnknown);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format(_T("连接数据库失败:%s"),e.ErrorMessage());
AfxMessageBox(errormessage);
return FALSE;
}
return TRUE;
}
CString CMedicineSaleManagesApp::GetExcelDriver(void)
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
do
{
if (strstr(pszBuf, "Excel") != 0)
{
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}
void CMedicineSaleManagesApp::WinHelp(DWORD dwData, UINT nCmd)
{
}
int CMedicineSaleManagesApp::ExitInstance()
{
if(boot)
{
this->SetAutoBoot();
this->m_pConn1->Close();
this->m_pConn1=NULL;
}
this->CompressDatabase();
::CoUninitialize();
return CWinApp::ExitInstance();
}
void CMedicineSaleManagesApp::CompressDatabase(void)
{
static const TCHAR szSrc[] =_T("Provider=Microsoft.Jet.OLEDB.4.0;")_T("Data Source=MedicineSaleManages.mdb;")_T("Jet OLEDB:Engine Type=5;Jet OLEDB:Database Password=a1@4>b?D_i-e=z+h|2<:");
static const TCHAR szDst[] =_T("Provider=Microsoft.Jet.OLEDB.4.0;")_T("Data Source=MedicineSaleManages1.mdb;")_T("Jet OLEDB:Engine Type=5;Jet OLEDB:Database Password=a1@4>b?D_i-e=z+h|2<:");
CComPtr<IJetEngine>Engine;
if (SUCCEEDED(Engine.CoCreateInstance(L"JRO.JetEngine")))
Engine->CompactDatabase(CComBSTR(szSrc), CComBSTR(szDst));
::remove("MedicineSaleManages.mdb");
::rename("MedicineSaleManages1.mdb","MedicineSaleManages.mdb");
}
ULONGLONG CMedicineSaleManagesApp::MyGetDiskFreeSpace(LPCTSTR driver)
{
DWORD dwBytesPerSector = 0; // bytes per sector
DWORD dwSectorsPerCluster=0; // sectors per cluster
DWORD dwTotalClusters = 0; // total clusters
DWORD dwFreeClusters = 0; // free clusters
DWORD dwVolumeSerialNumber=0;// volume ser number
DWORD dwMaxNameLength=0; // max component length
DWORD dwFileSystemFlags=0; // file system flags
DWORD dwFreeSpace = 0; // free space
char szFileSysName[129]; // name of file system
char szLabel[129]; // name of volume
GetDiskFreeSpace(driver, &dwBytesPerSector,
&dwSectorsPerCluster, &dwFreeClusters, &dwTotalClusters);
GetVolumeInformation(driver, szLabel, sizeof( szLabel ) - 1,
&dwVolumeSerialNumber, &dwMaxNameLength,
&dwFileSystemFlags, szFileSysName,
sizeof( szFileSysName ) - 1 );
dwFreeSpace = dwBytesPerSector * dwSectorsPerCluster * dwFreeClusters;
return (ULONGLONG)dwFreeSpace;
}
void CMedicineSaleManagesApp::DbToFile(CString table_name,CString strPath)
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSQL="SELECT * FROM Template where name='";
strSQL+=table_name;
strSQL+="'";
m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()+1),
((CMedicineSaleManagesApp*)AfxGetApp())->m_pConn1.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
CFile f;
f.Open(strPath,CFile::modeWrite|CFile::modeCreate);
long lPhotoSize = m_pRecordset->Fields->Item["table_xls"]->ActualSize;
long lIsRead=0;
_variant_t varChunk;
BYTE buf[ChunkSize];
while(lPhotoSize>0)
{
lIsRead=lPhotoSize>=ChunkSize? ChunkSize:lPhotoSize;
//从字段data中获取一个数据包
varChunk = m_pRecordset->Fields->Item["table_xls"]->GetChunk(lIsRead);
for(long index=0;index<lIsRead;index++)
{
::SafeArrayGetElement(varChunk.parray,&index,buf+index);
}
//将数据包写入文件
f.Write(buf,lIsRead);
lPhotoSize-=lIsRead;
}
f.Close();
m_pRecordset->Close();
m_pRecordset=NULL;
}
void CMedicineSaleManagesApp::FileToDb(CString access_table,CString field1,CString field2,CString field1_data,CString strPath)
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSQL="SELECT * FROM ";
strSQL+=access_table;
m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()+1),
((CMedicineSaleManagesApp*)AfxGetApp())->m_pConn1.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
m_pRecordset->AddNew();
m_pRecordset->PutCollect(_bstr_t(field1), _variant_t(field1_data));
VARIANT varChunk;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
//VT_ARRAY | VT_UI1
CFile f(strPath,CFile::modeRead);
BYTE bVal[ChunkSize+1];
UINT uIsRead=0;
//Create a safe array to store the array of BYTES
while(1)
{
uIsRead=f.Read(bVal,ChunkSize);
if(uIsRead==0)break;
rgsabound[0].cElements =uIsRead;
rgsabound[0].lLbound = 0;
///创建SAFEARRAY对象
psa = SafeArrayCreate(VT_UI1,1,rgsabound);
for(long index=0;index<uIsRead;index++)
{
if(FAILED(SafeArrayPutElement(psa,&index,&bVal[index])))
::MessageBox(NULL,"错误。","提示",MB_OK | MB_ICONWARNING);
}
//为varChunk变量赋值
varChunk.vt = VT_ARRAY|VT_UI1;
varChunk.parray = psa;
//加入BLOB类型的数据
try
{
m_pRecordset->Fields->GetItem(_bstr_t(field2))->AppendChunk(varChunk);
}
catch (_com_error &e)
{
CString str=(char*)e.Description();
::MessageBox(NULL,str+"\n错误。","提示",MB_OK | MB_ICONWARNING);
}
::VariantClear(&varChunk);
::SafeArrayDestroyData( psa);
if(uIsRead<ChunkSize)
break;
}
m_pRecordset->Update();
f.Close();
::remove(strPath);
m_pRecordset->Close();
m_pRecordset=NULL;
}
BOOL CMedicineSaleManagesApp::Examination(CString money)// 检查单价的合法性
{
int n=money.GetLength();
char ch[]="0123456789.";
BOOL xy;
BOOL cy=FALSE;
int zoor=0;
for(int x=0;x<n;x++)
{
xy=TRUE;
char c=money.GetAt(x);
for(int y=0;y<strlen(ch);y++)
if(c==ch[y])
{
xy=FALSE;
if(c=='.')
zoor++;
}
if(xy)
cy=TRUE;
}
if(money.GetAt(0)=='0')
if(money.GetAt(1)!='.')
cy=TRUE;
if(cy || zoor>1)
return FALSE;
else
return TRUE;
}
void CMedicineSaleManagesApp::ShowInTaskbar( HWND hWnd, BOOL bShow )
{
// 在程序的起始处(例如 InitInstance 中)加入 ::CoInitialize( NULL );
HRESULT hr;
ITaskbarList* pTaskbarList;
hr = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList, ( void** )&pTaskbarList );
pTaskbarList->HrInit( );
if( bShow )
pTaskbarList->AddTab( hWnd );
else
pTaskbarList->DeleteTab( hWnd );
pTaskbarList->Release( );
// 在程序的末尾(例如 ExitInstance 中)加入::CoUninitialize( );
}
void CMedicineSaleManagesApp::GetInputSet(void)//获取用户的输入法设置
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
CString strSQL="SELECT * FROM USERS";
m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()+1),
((CMedicineSaleManagesApp*)AfxGetApp())->m_pConn1.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
_variant_t var;
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("cn");
if(var.vt!=NULL)
cn_str = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("en");
if(var.vt!=NULL)
en_str = (LPCSTR)_bstr_t(var);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pRecordset=NULL;
}
void CMedicineSaleManagesApp::SetInputMethod(BOOL type)//设置当前输入法,0为英文;1为中文
{
theApp.GetInputSet();
//查询获得的用户相应输入法在当前输入法中的编号
CInputLanguage ime;
CString imestr;
for(int x=0;x<ime.ime_x;x++)
{
imestr.Format("%s",ime.m_pILHead->szName);
if(type)
{
if(theApp.cn_str==imestr)
{
ime.SelectInputLanguage(ime.m_pILHead);
}
}
else
{
if(theApp.en_str==imestr)
{
ime.SelectInputLanguage(ime.m_pILHead);
}
}
ime.m_pILHead=ime.m_pILHead->pNext;
}
ime.Clear();
}
CString CMedicineSaleManagesApp::GetIntNumber(double number)
{
CString str;
str.Format("%f",number);
if(atoi((str.Left(str.Find('.',0)+1)+str.Mid(str.Find('.',0)+1,2)).Right(1))>=5)
str.Format("%f",ceil(number*10)/10);
else
if(atoi((str.Left(str.Find('.',0)+1)+str.Mid(str.Find('.',0)+1,2)).Right(1))>0)
str.Format("%f",floor(number*10)/10);
return str;
}
BOOL CMedicineSaleManagesApp::GetAuto(CString str)
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
CString strSQL="SELECT * FROM USERS";
m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()+1),
((CMedicineSaleManagesApp*)AfxGetApp())->m_pConn1.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return FALSE;
}
BOOL message=FALSE;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
_variant_t var;
var = m_pRecordset->GetCollect(_bstr_t(str));
if(var.boolVal)
message=TRUE;
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return FALSE;
}
m_pRecordset->Close();
m_pRecordset=NULL;
return message;
}
void CMedicineSaleManagesApp::SetAutoBoot(void)
{
char filename[64];
GetModuleFileName(0,filename,64);
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE,&hKey);
if(this->GetAuto("auto"))//读取是否自动启动
RegSetValueEx(hKey,"MedicineSaleManages",0,REG_SZ,(BYTE*)filename,64);
else
{
RegSetValueEx(hKey,"MedicineSaleManages",0,REG_SZ,(BYTE*)filename,64);
RegDeleteValue(hKey,"MedicineSaleManages");
}
RegCloseKey(hKey);
}
CString CMedicineSaleManagesApp::ReadData(CString table,CString str)
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
CString strSQL="SELECT * FROM ";
strSQL+=table;
m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()+1),
((CMedicineSaleManagesApp*)AfxGetApp())->m_pConn1.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
str=(LPCSTR)_bstr_t(m_pRecordset->GetCollect(_bstr_t(str)));
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
m_pRecordset->Close();
m_pRecordset=NULL;
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -