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

📄 superwords.cpp

📁 同学通讯录系统,利用MFC编写,利用了数据库的技术
💻 CPP
字号:
// SuperWords.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "SuperWords.h"
#include "SuperWordsDlg.h"
#include "LoginDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSuperWordsApp

BEGIN_MESSAGE_MAP(CSuperWordsApp, CWinApp)
	//{{AFX_MSG_MAP(CSuperWordsApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSuperWordsApp construction

CSuperWordsApp::CSuperWordsApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CSuperWordsApp object

CSuperWordsApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CSuperWordsApp initialization

BOOL CSuperWordsApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	SetRegistryKey(_T("HMN-SOFT"));
	if(!InitODBC())	
		return FALSE;
	CLoginDlg * log = new CLoginDlg;
	if(log->DoModal() != IDOK)
		return FALSE;
	DWORD dwUser;
	dwUser = log->GetUserInfo();
	delete log;
	CSuperWordsDlg dlg;
	m_pMainWnd = &dlg;
	dlg.DoModal();
	return FALSE;
}

void CSuperWordsApp::GetCurDir(TCHAR *szBuffer, TCHAR *szTempBuf)
{
	CString szTemp;
	szTemp.Empty();
	szTemp=szBuffer;
	char ch='\\';
    char *pdest;
	int n=0;
    memset(szTempBuf,0,sizeof(szTempBuf));
   	pdest = strrchr( szBuffer, ch );
	n = pdest - (char *)szBuffer + 1;
	CString szReturn;
	szReturn.Empty();
	szReturn = szTemp.Left(n);
	memset(szTempBuf,0,sizeof(szTempBuf));
	lstrcpy(szTempBuf,szReturn);

}

BOOL CSuperWordsApp::InitODBC()
{
	char strCurfile[256];
	char * p;
	CFileFind file;
	::GetModuleFileName(m_hInstance,strCurfile,sizeof(strCurfile));
	p = strCurfile;
	while(strchr(p,'\\'))
	{
		p = strchr(p,'\\');
		p++;
	}
	*p = '\0';
	strcat(strCurfile,"BookData.mdb");
	if(!file.FindFile(strCurfile))
	{
		AfxMessageBox("找不到BookData.mdb数据库文件,请保证该文件和本程序在同一个目录中!",MB_OK|MB_ICONSTOP);
		return FALSE;
	}
	if(GetProfileInt("数据源","IsFirstRun",0))
	{
		CString strKeyDir = GetProfileString("数据源","CurDir");
		if(strKeyDir.Compare(strCurfile))
		{
			WriteProfileString("数据源","CurDir",strCurfile);	
			return(LoadODBC(CString("CarsDataBase"),CString(strCurfile),CString("数据源")));
		}
	}
	else
	{
		USERINFO sysuser;
		strcpy(sysuser.name,"System");
		sysuser.oper = 1;
		strcpy(sysuser.password,"system");
		AfxGetApp()->WriteProfileBinary("用户","User0000",(LPBYTE)&sysuser,sizeof(sysuser));
		AfxGetApp()->WriteProfileInt("用户","LastID",1);
		WriteProfileInt("数据源","IsFirstRun",1);
		WriteProfileString("数据源","CurDir",strCurfile);
		return(LoadODBC(CString("CarsDataBase"),CString(strCurfile),CString("数据源")));
	}
	return TRUE;
}
BOOL CSuperWordsApp::LoadODBC(CString strSourceName,CString strSourceDb, CString strDescription)
{
	//存放打开的注册表键 
	HKEY hKey; 
	DWORD dw; 
	//存放注册表 API函数执行的返回值 
	LONG lReturn; 
	//存放要打开的子键 
	CString strSubKey; 
	//检测是否安装了 MS Access ODBC driver:odbcjt32.dll 
	//获得 Windows系统目录 
	char sysDir[MAX_PATH]; 
	char drvName[]="\\odbcjt32.dll" ; 
	::GetSystemDirectory(sysDir,MAX_PATH); 
	strcat(sysDir,drvName); 
	CFileFind findFile; 
	if(!findFile.FindFile(sysDir)) 
	{
		AfxMessageBox("找不到MS Access的ODBC驱动程序odbcjt32.dll,加载数据源失败!",
			MB_OK|MB_ICONSTOP); 
		return FALSE; 
	}
	strSubKey="SOFTWARE\\ODBC\\ODBC.INI\\"+strSourceName;
	//创建ODBC数据源在注册表中的子键 
	lReturn=::RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCTSTR)strSubKey,0,NULL,
		REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,&dw); 
	if(lReturn != ERROR_SUCCESS) 
		return false; 
	//设置数据源的各项参数 
	CString strDbq=strSourceDb;
	CString strDriver=sysDir;
	DWORD dwDriverId=25;
	CString strFil="MS Access;";
	CString strPwd=strSourceName; 
	DWORD dwSafeTransactions=0;
	CString strUid=strSourceName;
	::RegSetValueEx(hKey,"DBQ",0L,REG_SZ,(const BYTE*)((LPCTSTR) strDbq),strDbq.GetLength());
	::RegSetValueEx(hKey,"Description",0L,REG_SZ,(const BYTE*)((LPCTSTR)strDescription),strDescription.GetLength());
	::RegSetValueEx(hKey,"Driver",0L,REG_SZ,(const BYTE*)((LPCTSTR)strDriver),strDriver.GetLength());
	::RegSetValueEx(hKey,"DriverId",0L,REG_DWORD,(const BYTE*)(&dwDriverId),sizeof(dw));
	::RegSetValueEx(hKey,"FIL",0L,REG_SZ,(const BYTE*)((LPCTSTR)strFil),strFil.GetLength()); 
	::RegSetValueEx(hKey,"PWD",0L,REG_SZ,(const BYTE*)((LPCTSTR)strPwd),strPwd.GetLength ());
	::RegSetValueEx(hKey,"SafeTransactions",0L,REG_DWORD,(const BYTE*)(&dwSafeTransactions),sizeof(dw));
	::RegSetValueEx(hKey,"UID",0L,REG_SZ,(const BYTE*)((LPCTSTR)strUid),strUid.GetLength());
	::RegCloseKey(hKey); 
	//创建 ODBC数据源的 Jet子键 
	strSubKey +="\\Engines\\Jet" ; 
	lReturn=::RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCTSTR)strSubKey,0,
		NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,&dw);
	if(lReturn != ERROR_SUCCESS)
		return false; 
	//设置该子键下的各项参数 
	CString strImplict="" ; 
	CString strUserCommit="Yes" ; 
	DWORD dwPageTimeout=5;
	DWORD dwThreads=3; 
	DWORD dwMaxBufferSize=2048;
	::RegSetValueEx(hKey,"ImplictCommitSync",0L,REG_SZ,(const BYTE*)((LPCTSTR)strImplict),strImplict.GetLength()+1); 
	::RegSetValueEx(hKey,"MaxBufferSize",0L,REG_DWORD,(const BYTE*)(&dwMaxBufferSize),sizeof(dw)); 
	::RegSetValueEx(hKey,"PageTimeout",0L,REG_DWORD,(const BYTE*)(&dwPageTimeout),sizeof(dw)); 
	::RegSetValueEx(hKey,"Threads",0L,REG_DWORD,(const BYTE*)(&dwThreads),sizeof(dw)); 
	::RegSetValueEx(hKey,"UserCommitSync" ,0L,REG_SZ,(const BYTE*)((LPCTSTR)strUserCommit),strUserCommit.GetLength()); 
	::RegCloseKey(hKey); 
	//设置 ODBC数据库引擎名称 
	lReturn=::RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources" ,0L,KEY_WRITE,&hKey); 
	if(lReturn !=ERROR_SUCCESS) 
		return false; 
	CString strDbType="Microsoft Access Driver (*.mdb)";
	::RegSetValueEx(hKey,strSourceName,0L,REG_SZ,(const BYTE*)((LPCTSTR)strDbType),strDbType.GetLength());
	::RegCloseKey(hKey);
	return true; 
}

⌨️ 快捷键说明

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